Bash List Files

Learn the ls command and its most useful flags, -l, -a, and -h, to inspect directory contents in detail.

The ls Command

ls lists the contents of a directory. Run with no arguments, it shows the files and subdirectories inside your current working directory, sorted alphabetically, and it hides so-called dotfiles (names starting with a period) by default.

Basic Listing

$ ls
Documents  Downloads  Pictures  notes.txt  script.sh

Long Listing Format

The -l flag switches to long format, showing one entry per line along with permissions, the number of hard links, the owner, the group, the size in bytes, and the last modification date.

Using ls -l

$ ls -l
total 16
drwxr-xr-x 2 alex alex 4096 Jul 12 09:10 Documents
drwxr-xr-x 2 alex alex 4096 Jul 10 18:22 Downloads
-rw-r--r-- 1 alex alex  312 Jul 15 21:03 notes.txt
-rwxr-xr-x 1 alex alex   87 Jul 16 08:47 script.sh

Hidden Files and Human-Readable Sizes

The -a flag reveals every entry, including hidden dotfiles such as .bashrc and .gitignore. The -h flag, used alongside -l, converts raw byte counts into human-readable units like K, M, and G instead of long strings of digits.

Combining -l, -a, and -h

$ ls -lah
total 44K
drwxr-xr-x  6 alex alex 4.0K Jul 16 08:47 .
drwxr-xr-x  3 root root 4.0K Jun 01 12:00 ..
-rw-------  1 alex alex  312 Jul 15 21:03 .bash_history
drwxr-xr-x  2 alex alex 4.0K Jul 12 09:10 Documents
-rwxr-xr-x  1 alex alex   87 Jul 16 08:47 script.sh
  • ls -la — combine hidden files with long format
  • ls -lh — combine long format with human-readable sizes
  • ls -lt — sort entries by modification time, newest first
  • ls -lS — sort entries by size, largest first
  • ls -R — list a directory and all of its subdirectories recursively
Note: When you use -a, the listing always includes . and .. themselves, representing the current and parent directories. These are not hidden configuration files, just the two entries every directory contains.
FlagEffect
-lLong format: permissions, owner, group, size, and date
-aShow all entries, including hidden dotfiles
-hHuman-readable sizes, used together with -l
-tSort by modification time, newest first
-RList subdirectories recursively
-SSort by file size, largest first

Exercise: Bash Navigation

What does the "pwd" command display?