Linux commands: ls

By

Published · Updated

Tagged: CLI

How to use the ls command on Linux and macOS: list hidden files, read the -l output, show human-readable sizes, sort by date or size, list recursively, and the differences between GNU and macOS ls.

Inside a folder you can list all the files that the folder contains using the ls command:

ls

If you add a folder name or path, it will print that folder contents:

ls /bin

Terminal output of ls /bin listing the commands in columns, like bash, cat, chmod, cp, ls and zsh

You can pass more than one path, and also files or glob patterns:

ls /bin /usr/bin
ls *.txt

ls accepts a lot of options. One of my favorite options combinations is -al. Try it:

ls -al /bin

Terminal output of ls -al /bin showing detailed file listing with permissions, ownership, sizes, and timestamps

compared to the plain ls, this returns much more information.

You have, from left to right:

This set of data is generated by the l option. The a option instead also shows the hidden files.

Hidden files are files that start with a dot (.).

This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Reading the long format

Here’s the output of ls -la in a test folder I made on my Mac:

total 3928
drwxr-xr-x@ 8 flaviocopes  wheel      256 Nov 28 07:01 .
drwxr-xr-x@ 4 flaviocopes  wheel      128 Nov 28 07:01 ..
-rw-r--r--@ 1 flaviocopes  wheel        0 Nov 28 07:01 .env
-rw-r--r--@ 1 flaviocopes  wheel  2000000 Nov 28 07:01 big.bin
drwxr-xr-x@ 2 flaviocopes  wheel       64 Nov 28 07:01 docs
lrwxr-xr-x@ 1 flaviocopes  wheel        9 Nov 28 07:01 link.txt -> notes.txt
-rw-r--r--@ 1 flaviocopes  wheel        3 Nov 28 07:01 notes.txt
-rwxr-xr-x@ 1 flaviocopes  wheel       10 Nov 28 07:01 run.sh

The first character tells you the type of entry:

The next 9 characters are the permissions, in 3 groups of 3: owner, group, everyone else. r is read, w is write, x is execute (for a directory, x means you can enter it). A - means that permission is missing. So rwxr-xr-x means the owner can do everything, while the group and the others can read and execute but not write. I explain how to change them in the chmod post.

Sometimes there’s one more character right after the permissions:

The . and .. entries are the current folder and its parent. They show up with -a.

The total line at the top is the disk space used by the listed files, counted in blocks. It’s not the number of files. On macOS a block is 512 bytes by default, and with GNU ls on Linux it’s 1024 bytes. That’s why the same folder gives different totals on different systems.

The date is the last modification time. For files modified more than 6 months ago, ls shows the year instead of the hour and minute.

Show sizes you can read

Sizes in bytes are hard to read. Add -h to get K, M and G:

ls -lh
-rw-r--r--@ 1 flaviocopes  wheel   1.9M Nov 28 07:01 big.bin

-h uses powers of 1024. GNU ls also has --si to use powers of 1000, like disk manufacturers do.

ls shows the size of a directory entry itself, not of what’s inside it. To see how much space a folder takes, use du instead.

Hidden files without . and ..

-a shows everything, including . and ... Most of the time you don’t care about those two, so use -A:

ls -lA

Sorting

By default ls sorts by name. You can change that:

Combine them. The most recent files at the bottom, right above your prompt, is a very handy one:

ls -ltr

The largest files first:

ls -lhS

Other options I use

-F adds a symbol after each name so you can tell what it is without the long format:

ls -F
big.bin    docs/    link.txt@    notes.txt    run.sh*

/ is a directory, * is an executable, @ is a symbolic link, | is a pipe and = is a socket.

-1 (the number one) prints one name per line. This is also what ls does on its own when the output goes to a pipe or a file and not to the terminal:

ls -1

-d lists the directory itself instead of its contents. Useful when you want to check the permissions of a folder:

ls -ld docs

-R lists all subfolders recursively. On a big folder this prints a lot, so find or tree are usually a better fit.

-i prints the inode number of each file.

GNU ls vs macOS ls

Linux uses GNU ls, while macOS ships the BSD version. The common options above work on both, but some don’t:

On a Mac you can install the GNU version with Homebrew (brew install coreutils) and run it as gls.

Don’t parse ls in scripts

The output of ls is made for humans. File names can contain spaces and even newlines, and the format differs between systems. In a script, loop over a glob instead:

for f in *.txt; do
  echo "$f"
done

The quotes around $f keep names with spaces in one piece.

Want me to talk about your product? You can sponsor this site.

Related posts about cli: