20+ ls Command Examples in Linux

This tutorial explains Linux “ls” command, options and its usage with examples.

“ls” command is used to list directory contents. This post describes “ls” command used in Linux along with usage examples and/or output.

Usage:
ls [OPTION]… [FILE]..

In computing, ls is a command to list files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification. It is one of the the most frequently used command in unix.

Here’s the listing of example usage of “ls” command:

1. To list directory contents:

sanfoundry-> ls
1.txt  2.txt  hello  sample

2. To display One File Per Line (ls -1):

advertisement
advertisement
sanfoundry-> ls -1
1.txt
2.txt
hello
sample

3. To display total information about Files/Directories(ls -l):

sanfoundry-> ls -l 
total 8
-rw-rw-r-- 2 him himanshu    12 Jul  1 23:11 1.txt
-rw-rw-r-- 2 him himanshu    0  Jul  1 23:11 2.txt
drwxrwxr-x 2 him himanshu 4096  Jul  6 19:25 hello
drwxrwxr-x 2 him himanshu 4096  Jul  6 19:25 sample

Field Explanation
1) The first character displays the the type of file.
* “-” Normal file
* “d” Directory
* “l” link file
Next 9 lines specifies the permissions. Each group of 3 lines corresponds to permissions. So first three lines stands for user permissions, next 3 for group and next 3 are for others permissions. Here r stands for read , w for write and x for execute.
2) Second field specifies the number of links for that file.
3) Third field specifies owner of the file. Here owner is “him”.
4) Fourth field specifies the group of the file. Here group “himanshu” belongs the file.
5) Fifth field specifies the size of file. Here file size for the file 1.txt is 12 bytes.
6) Sixth field specifies the last modified date and time of the file. Here for file 1.txt is july 13 23:11
7) Seventh Field specifies the name of the file/directory itself.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. To show all the hidden files(ls -a OR ls -A):

sanfoundry-> ls -a
.
..
Ada-payment                 .goutputstream-4HV4PW  
Backups                     .goutputstream-4TTDSW  
.bash_history                Fedora-Info.txt
.bash_logout                 .lftp
.bash_profile                libiconv-1.11.tar.tar

Here “.” means current directory and “..” means parent directory are shown.So if you don’t want them to display use “-A” option.

sanfoundry-> ls -A
Ada-payment                 .goutputstream-4HV4PW  
Backups                     .goutputstream-4TTDSW  
.bash_history                Fedora-Info.txt
.bash_logout                 .lftp
.bash_profile                libiconv-1.11.tar.tar

6. To display files Recursively (ls -R):

advertisement
sanfoundry-> ls -R SAN/
SAN/:
1.txt  2.txt  hello  sample
 
SAN/hello:
 
SAN/sample:

Here all the contents of the directory SAN are shown.

7. To display file size in human readable form(ls -lh file/directory name):
Here the size is displayed in the readable format. K stands for KB , M for MB , G for GB.

sanfoundry-> ls -lh fixing 
-rw-rw-r-- 1 himanshu himanshu 291 Jul  6 01:36 fixing
 
sanfoundry-> ls -lh Social\ Network/
total 3.2G
-rw-r--r-- 1 himanshu himanshu 115K Dec  7  2011 The.Social.Network.2010.720p.BRRip.XviD.AC3-FLAWL3SS[05-23-27].JPG
-rw-r--r-- 1 himanshu himanshu 3.2G Feb  9  2011 The.Social.Network.2010.720p.BRRip.XviD.AC3-FLAWL3SS.avi
-rw-r--r-- 1 himanshu himanshu 183K Dec 15  2010 The.Social.Network.2010.720p.BRRip.XviD.AC3-FLAWL3SS.srt
-rw-r--r-- 1 himanshu himanshu  15K Apr 16  2012 Thumbs.db

8. To order files based on last modified time(ls -lt):

advertisement
sanfoundry-> ls -lt 
total 12
-rw-rw-r-- 1 himanshu himanshu    0     Jul  7 00:47   xyz
-rw-rw-r-- 1 himanshu himanshu    12    Jul  7 00:47   him
drwxrwxr-x 2 himanshu himanshu    4096  Jul  7 00:46   sample.txt
drwxrwxr-x 2 himanshu himanshu    4096  Jul  6 19:25   hello
drwxrwxr-x 2 himanshu himanshu    4096  Jul  6 19:25   sample
-rw-rw-r-- 2 himanshu himanshu    10    Jul  1 23:11   1.txt
-rw-rw-r-- 2 himanshu himanshu    20    Jul  1 23:11   2.txt

9. To Order Files Based on Last Modified Time in Dec-ending Order(ls -lrt):

sanfoundry-> ls -lrt
total 12
-rw-rw-r-- 2 himanshu himanshu    0 Jul  1 23:11  2.txt
-rw-rw-r-- 2 himanshu himanshu    0 Jul  1 23:11  1.txt
drwxrwxr-x 2 himanshu himanshu 4096 Jul  6 19:25  sample
drwxrwxr-x 2 himanshu himanshu 4096 Jul  6 19:25  hello
drwxrwxr-x 2 himanshu himanshu 4096 Jul  7 00:46  sample.txt
-rw-rw-r-- 1 himanshu himanshu    0 Jul  7 00:47  him
-rw-rw-r-- 1 himanshu himanshu    0 Jul  7 00:47  xyz

10. To Stream output format; files are listed across the page, separated by commas(ls -m):

sanfoundry-> ls -m 
1.txt, 2.txt, hello, him, sample, sample.txt, xyz

11. To List all subdirectories(ls *):

sanfoundry-> ls *
1.txt  2.txt  him  xyz
 
hello:
 
sample:
 
sample.txt:

12. To Display File Inode Number(ls -i):

sanfoundry-> ls -i -1
592260 1.txt
592260 2.txt
612447 hello
543095 him
612445 sample
612009 sample.txt
543097 xyz

Here “-1” is added as option to print the inode numbers one in a line.

13. To sort files with Size(ls -S):

sanfoundry-> ls -lS
total 12
drwxrwxr-x 2 himanshu himanshu 4096 Jul  6 19:25 hello
drwxrwxr-x 2 himanshu himanshu 4096 Jul  6 19:25 sample
drwxrwxr-x 2 himanshu himanshu 4096 Jul  7 00:46 sample.txt
-rw-rw-r-- 2 himanshu himanshu    20 Jul  1 23:11 1.txt
-rw-rw-r-- 2 himanshu himanshu    10 Jul  1 23:11 2.txt
-rw-rw-r-- 1 himanshu himanshu    5 Jul  7 00:47 him
-rw-rw-r-- 1 himanshu himanshu    0 Jul  7 00:47 xyz

14. ls redirection to output file(ls > out.txt):

sanfoundry-> ls > out.txt 
sanfoundry-> cat out.txt 
1.txt
2.txt
hello
him
out.txt
sample
sample.txt
xyz

15. To list directories only(ls -d */):

sanfoundry-> ls -d */
hello/  sample/  sample.txt/

Note
List files and directories with full path:

$ ls -d $PWD/*

16. Display File UID and GID (ls -n):

sanfoundry-> ls -l 1.txt 
-rw-rw-r-- 2 himanshu himanshu 24 Jul  7 01:03 1.txt
sanfoundry-> ls -n 1.txt 
-rw-rw-r-- 2 1000 1000 24 Jul  7 01:03 1.txt

Here 1000 is the uid and next 1000 is gid of the file 1.txt.

17. To Do Visual Classification of Files(ls -F):

sanfoundry-> ls -F
1.txt  2.txt  hello/  him  out.txt  sample/  sample.txt/  xyz @link

Each element has a character at the end and it is classified as:
* / for directory
* @ link file
* nothing – normal file

18. Useful ls Command Aliases:
To Classify the file type by both color and special character

alias ls="ls -F --color=auto"

19. To Open Last Edited File (ls -t):

sanfoundry-> vi first.txt
sanfoundry-> vi second.txt
 
sanfoundry->vi `ls -t | head -1`

Here vi should open second.txt file Since , here “ls -t” sorts the files according to the modification time and “head -1” picks the first file in it.

20. Multiple option using ls:

sanfoundry-> ls -alF
total 60
drwxrwxr-x  5 himanshu himanshu  4096 Jul  7 01:03 ./
drwxrwxrwx 62 himanshu himanshu 28672 Jul  7 01:03 ../
-rw-rw-r--  2 himanshu himanshu    24 Jul  7 01:03 1.txt
-rw-rw-r--  2 himanshu himanshu    24 Jul  7 01:03 2.txt
drwxrwxr-x  2 himanshu himanshu  4096 Jul  6 19:25 hello/
-rw-rw-r--  1 himanshu himanshu     0 Jul  7 00:47 him
-rw-rw-r--  1 himanshu himanshu    52 Jul  7 01:00 out.txt
drwxrwxr-x  2 himanshu himanshu  4096 Jul  6 19:25 sample/
drwxrwxr-x  2 himanshu himanshu  4096 Jul  7 00:46 sample.txt/
-rw-rw-r--  1 himanshu himanshu     0 Jul  7 00:47 xyz

You can use any number of options with ls so as to get listing details in a more accurate format.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.