≡ Menu

Linux: Interesting ls command examples

The Linux ls command is probably the most used command. The acronym ls stands for list and the command lists all files in a directory. In this tutorial, I will give examples of a few different arguments that can be used to explore the file system on a Linux / Unix machine.

1) ls -la

The most commonly used arguments to the ls command are -l and -a

ls -la

The above command will output all files in a directory in the long format.

[root@server ~]# ls -la
total 1892
dr-xr-x---  7 root root    4096 Sep 21 13:59 .
dr-xr-xr-x 20 root root    4096 Sep 23  2011 ..
-rw-------  1 root root   17360 Sep 21 15:17 .bash_history
-rw-r--r--  1 root root      18 May 20  2009 .bash_logout
-rw-r--r--  1 root root     176 May 20  2009 .bash_profile
-rw-r--r--  1 root root     176 Sep 23  2004 .bashrc
drwxr-xr-x  3 root root    4096 Sep 21 13:40 .cpanm
-rw-r--r--  1 root root     100 Sep 23  2004 .cshrc
-rw-r--r--  1 root root 1026284 Sep 16 14:12 dbnamedomains.txt

The first column in the output lists the permissions. The beginning character can be a d which stands for directory or a - which stands for regular file and it can also be a l character, which stands for a symbolic link.

The second and third columns represents the user and group of the file. The third column is the size of the file in bytes. The fourth column is the date on which the file was created. The fifth column is the file name.

2) ls -laR

The -R option to ls will output all files in a directory and all sub-directories. R stands for recursive.

3) ls -laRt

The -t argument will list the files ordered by the modification date and time starting with the latest.

[root@server ~]# ls -lat
total 1896
dr-xr-x---  8 root root    4096 Sep 21 17:19 .
drwxr-xr-x  2 root root    4096 Sep 21 17:19 dir
lrwxrwxrwx  1 root root      11 Sep 21 17:12 nn -> newyork.sql
-rw-------  1 root root   17360 Sep 21 15:17 .bash_history
-rw-r--r--  1 root root      12 Sep 21 13:59 modules.dep.bin
drwxr-xr-x  3 root root    4096 Sep 21 13:40 .cpanm

To list files that were modified earliest (ordered by modification date and time in ascending order), use the -r option:

[root@server ~]# ls -latr
total 1896
-rw-r--r--  1 root root     100 Sep 23  2004 .cshrc
-rw-r--r--  1 root root     176 Sep 23  2004 .bashrc
-rw-r--r--  1 root root     129 Dec  4  2004 .tcshrc
-rw-r--r--  1 root root     176 May 20  2009 .bash_profile
-rw-r--r--  1 root root      18 May 20  2009 .bash_logout
dr-xr-xr-x 20 root root    4096 Sep 23  2011 ..
-rw-r--r--  1 root root    1954 Feb  2  2013 dnsquery.php
-rw-r--r--  1 root root     677 Feb  2  2013 dumpdbnamedomains.php

4) ls -lah

The -h argument will list the files with size denoted in a human readable format. So, instead of outputting the file size as 1024, it will say 1K.

[root@server ~]# ls -lah
total 1.9M
dr-xr-x---  7 root root  4.0K Sep 21 17:12 .
dr-xr-xr-x 20 root root  4.0K Sep 23  2011 ..
-rw-------  1 root root   17K Sep 21 15:17 .bash_history
-rw-r--r--  1 root root    18 May 20  2009 .bash_logout
-rw-r--r--  1 root root   176 May 20  2009 .bash_profile
-rw-r--r--  1 root root   176 Sep 23  2004 .bashrc
drwxr-xr-x  3 root root  4.0K Sep 21 13:40 .cpanm
-rw-r--r--  1 root root   100 Sep 23  2004 .cshrc
-rw-r--r--  1 root root 1003K Sep 16 14:12 dbnamedomains.txt

5) ls -1

The -1 will list only file names. This can be very useful if you use the output of ls in a shell script.

[root@server ~]# ls -a1
.
..
.bash_history
.bash_logout
.bash_profile
.bashrc
.cpanm
.cshrc
dbnamedomains.txt

6) ls -laSh

The -S argument instructs ls to list files in decreasing order of size.

[root@server ~]# ls -laSh
total 1.9M
-rw-r--r--  1 root root 1003K Sep 16 14:12 dbnamedomains.txt
-rw-r--r--  1 root root  433K Sep 16 17:10 newyork.sql
-rw-r--r--  1 root root  329K Feb  8  2013 lnxhc-1.2-1.noarch.rpm
-rw-------  1 root root   17K Sep 21 15:17 .bash_history

To output files in increasing order of size, use the -r option:

[root@server ~]# ls -laShr
total 1.9M
lrwxrwxrwx  1 root root    11 Sep 21 17:12 nn -> newyork.sql
-rw-r--r--  1 root root    12 Sep 21 13:59 modules.dep.bin
-rw-r--r--  1 root root    18 May 20  2009 .bash_logout
-rw-------  1 root root    61 Sep  9 13:14 .lesshst
-rw-r--r--  1 root root    86 Sep  9 14:40 getheaders.php
-rwxr-xr-x  1 root root    87 Sep 18 15:40 empty.sh
-rw-r--r--  1 root root   100 Sep 23  2004 .cshrc

7) ls –color=auto

The –color=auto option will print the output in a colored format. See the following image:

ls-colored-output

8) ls -lA

The -A option is used to print all files except the two special files for the current directory and the parent directory – ‘.’ and ‘..’.

[root@server ~]# ls -lA
total 1888
-rw------- 1 root root   17360 Sep 21 15:17 .bash_history
-rw-r--r-- 1 root root      18 May 20  2009 .bash_logout
-rw-r--r-- 1 root root     176 May 20  2009 .bash_profile
-rw-r--r-- 1 root root     176 Sep 23  2004 .bashrc
drwxr-xr-x 3 root root    4096 Sep 21 13:40 .cpanm
-rw-r--r-- 1 root root     100 Sep 23  2004 .cshrc
-rw-r--r-- 1 root root 1026284 Sep 16 14:12 dbnamedomains.txt

So these were 8 interesting ls command examples. I hope you enjoy your ls command experience using these options that this tutorial showed!

{ 0 comments… add one }

Leave a Comment