For anyone using the command line, the ls command is one of the most often typed. Here we will outline it's basic usage, options and show some examples including piping to other commands.

Basic Usage

ls by itself, just lists the current working directories contents.

$ ls
Desktop Downloads Pictures sratch.txt Videos
Documents Music Public Templates vim-install.txt

As you can see, the output is kind of hard to read. That is why most people use the long ( -l ) option. This prints out the same information, but in a long listing. This makes it easier to read and also includes some more information about each file. The additional information include permissions, links, owner, group, size, time, and name.

$ ls -l
total 40
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Desktop
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Documents
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Downloads
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Music
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Pictures
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Public
-rw-rw-r--. 1 savona savona 96 Jan 20 08:26 sratch.txt
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Templates
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Videos
-rw-rw-r--. 1 savona savona 3755 Jan 20 08:24 vim-install.txt

Listing Hidden Files

Using the -a option, we can also see the hidden files (those starting with a period).

$ ls -la
total 104
drwx------. 17 savona savona 4096 Jan 20 08:26 .
drwxr-xr-x. 3 root root 4096 Jan 13 09:13 ..
-rw-------. 1 savona savona 277 Jan 16 18:48 .bash_history
-rw-r--r--. 1 savona savona 18 Oct 30 13:07 .bash_logout
-rw-r--r--. 1 savona savona 193 Oct 30 13:07 .bash_profile
-rw-r--r--. 1 savona savona 231 Oct 30 13:07 .bashrc
drwx------. 16 savona savona 4096 Jan 17 23:21 .cache
drwxr-xr-x. 15 savona savona 4096 Jan 17 23:21 .config
drwx------. 3 savona savona 4096 Jan 13 09:14 .dbus
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Desktop
.... OUTPUT TRUNCATED ....

Sorting

Sort By Time

We can also sort the output. Using the time ( -t ) we can sort by time. As you can see from the output below, the files are sorted by date and time with the newest file at the top of the output.

$ ls -lt
total 40
-rw-rw-r--. 1 savona savona 96 Jan 20 08:26 sratch.txt
-rw-rw-r--. 1 savona savona 3755 Jan 20 08:24 vim-install.txt
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Documents
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Music
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Pictures
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Videos
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Desktop
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Downloads
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Public
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Templates

Sort in Reverse Order

I use the sorting option almost everything I use ls. But I usually use it in reverse ( -r ) order. This conveniently puts the newest file at the bottom and makes it easy to find while working in large directories.

$ ls -ltr
total 40
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Templates
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Public
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Downloads
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Desktop
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Videos
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Pictures
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Music
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Documents
-rw-rw-r--. 1 savona savona 3755 Jan 20 08:24 vim-install.txt
-rw-rw-r--. 1 savona savona 96 Jan 20 08:26 sratch.txt

Sort by File Extension

We can also sort by file extension, which comes in handy if your working with a certain type of file in a large directory.

$ ls -Xlr
total 52
-rw-rw-r--. 1 savona savona 3755 Jan 20 08:24 vim-install.txt
-rw-rw-r--. 1 savona savona 145 Jan 20 08:46 scratch.txt
-rw-rw-r--. 1 savona savona 29 Jan 20 08:51 run.sh
-rw-rw-r--. 1 savona savona 51 Jan 12 08:54 rsync.sh
-rw-rw-r--. 1 savona savona 31 Jan 18 08:49 myscript.sh
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Videos
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Templates
.... OUTPUT TRUNCATED ....

Sort Files by Size

It is possible to sort by size using ( -S ).

$ ls -lS
total 52
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Desktop
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Documents
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Downloads
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Music
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Pictures
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Public
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Templates
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Videos
-rw-rw-r--. 1 savona savona 3755 Jan 20 08:24 vim-install.txt
-rw-rw-r--. 1 savona savona 145 Jan 20 08:46 scratch.txt
-rw-rw-r--. 1 savona savona 51 Jan 12 08:54 rsync.sh
-rw-rw-r--. 1 savona savona 31 Jan 18 08:49 myscript.sh
-rw-rw-r--. 1 savona savona 29 Jan 20 08:51 run.sh

Human Readable Output with ls Command

Using the -h option will show the file sizes in human readable format (instead of bytes it will show MB, GB, etc)

$ ls -lrth
total 1.6G
-rw-rw-r--. 1 savona savona 18M Mar 19 2018 TinyCore-current.iso
-rw-rw-r--. 1 savona savona 1.6G Oct 17 18:49 lubuntu-18.10-desktop-amd64.iso
-rw-rw-r--. 1 savona savona 51 Jan 12 08:54 rsync.sh
drwxr-xr-x. 2 savona savona 4.0K Jan 13 09:14 Templates
drwxr-xr-x. 2 savona savona 4.0K Jan 13 09:14 Public

Recursive Listing

You can also recursively list out all directories inside your current working directory. Here I am listing out my home directory recursively, showing all the directories and their contents.

$ ls -lR
.:
total 52
drwxr-xr-x. 2 savona savona 4096 Jan 20 10:48 Desktop
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Documents
drwxr-xr-x. 2 savona savona 4096 Jan 20 10:48 Downloads
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Music
-rw-rw-r--. 1 savona savona 31 Jan 18 08:49 myscript.sh
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Pictures
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Public
-rw-rw-r--. 1 savona savona 51 Jan 12 08:54 rsync.sh
-rw-rw-r--. 1 savona savona 29 Jan 20 08:51 run.sh
-rw-rw-r--. 1 savona savona 145 Jan 20 08:46 scratch.txt
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Templates
drwxr-xr-x. 2 savona savona 4096 Jan 13 09:14 Videos
-rw-rw-r--. 1 savona savona 3755 Jan 20 08:24 vim-install.txt
./Desktop:
total 16
-rw-rw-r--. 1 savona savona 8978 Jan 20 10:48 Devices.txt
-rw-rw-r--. 1 savona savona 9 Jan 20 10:44 Somefile.txt
./Documents:
total 0
./Downloads:
total 1673220
-rw-rw-r--. 1 savona savona 1694498816 Oct 17 18:49 lubuntu-18.10-desktop-amd64.iso
-rw-rw-r--. 1 savona savona 18874368 Mar 19 2018 TinyCore-current.iso
./Music:
total 0
./Pictures:
total 0
./Public:
total 0
./Templates:
total 0
./Videos:
total 0

Listing SELinux Context

This is an important one that is often overlooked. You can use the -Z option to list all the files along with their respective SELinux context.

$ ls -Z
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Desktop
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Documents
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Downloads
drwxr-xr-x. savona savona unconfined_u:object_r:audio_home_t:s0 Music
-rw-rw-r--. savona savona unconfined_u:object_r:user_home_t:s0 myscript.sh
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Pictures
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Public
-rw-rw-r--. savona savona unconfined_u:object_r:user_home_t:s0 rsync.sh
-rw-rw-r--. savona savona unconfined_u:object_r:user_home_t:s0 run.sh
-rw-rw-r--. savona savona unconfined_u:object_r:user_home_t:s0 scratch.txt
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Templates
drwxr-xr-x. savona savona unconfined_u:object_r:user_home_t:s0 Videos
-rw-rw-r--. savona savona unconfined_u:object_r:user_home_t:s0 vim-install.txt

Piping Output to other Commands

You can pipe the output of ls into other commands for further processing, here are a couple examples.

Here we will use grep to find what we are looking for.

$ ls -lrt | grep scratch
-rw-rw-r--. 1 savona savona 145 Jan 20 08:46 scratch.txt

Here we will sort files by size, but exclude directories using grep.

$ ls -lSh | grep -v '^d'
total 52K
-rw-rw-r--. 1 savona savona 3.7K Jan 20 08:24 vim-install.txt
-rw-rw-r--. 1 savona savona 145 Jan 20 08:46 scratch.txt
-rw-rw-r--. 1 savona savona 51 Jan 12 08:54 rsync.sh
-rw-rw-r--. 1 savona savona 31 Jan 18 08:49 myscript.sh
-rw-rw-r--. 1 savona savona 29 Jan 20 08:51 run.sh

Here we will pipe the above output to awk to get a list of JUST filenames. This is useful if you are iterating though the list.

$ ls -lSh | grep -v '^d' | awk '{print $9}'
vim-install.txt
scratch.txt
rsync.sh
myscript.sh
run.sh

That is it folks, the basics of the ls command. Sound off in the comments if I missed anything.

Related Articles: