The ls command list files and directories in Linux within the file system, and shows detailed information about them. This article will show you how to use the ls command through examples and most common ls options. Below is the syntax of the command

ls [OPTION]... [FILE]...

When used with no options and arguments, ls displays a list of the names of all files in the current working directory

root@tryit-driven:~# ls                                                                                                                                
t1.txt  t2.txt  

Show Detailed Information

The default output of the ls command shows only the names of the files and directories. Use -l ( lowercase L) option to print files in a long listing format. This format shows following information:

  • Type
  • Permissions
  • Number of hard links to the file.
  • Owner
  • Group
  • Size
  • Date and Time
  • Name
root@tryit-driven:~# ls -la                                                                                                                            
total 0                                                                                                                                                
drwx------ 1 root root  38 Mar  3 13:59 .                                                                                                              
drwxr-xr-x 1 root root 172 Mar  3 13:58 ..                                                                                                             
drwxr-x--- 1 root root   6 Mar  3 13:59 .config                                                                                                        
-rw-r--r-- 1 root root   0 Mar  3 13:59 t1.txt                                                                                                         
-rw-r--r-- 1 root root   0 Mar  3 13:59 t2.txt 

The first character in the above example shows the type. Types can be one of the following

  • – : Regular file
  • b : Block special file
  • c : Character special file
  • d : Directory
  • l : Symbolic link
  • n : Network file
  • p : FIFO
  • s : Socket

The next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. The number 1 after the permission characters is the number of hard links to this file.

The next two fields root root are showing the file owner and the group, followed by the size of the file in bytes. Use the -h option if you want to print sizes in a human-readable format instead of bytes.

root@tryit-session:~#
root@tryit-driven:~# ls -lh                                                                                                                            
total 0                                                                                                                                                
-rw-r--r-- 1 root root 0 Mar  3 13:59 t1.txt                                                                                                           
-rw-r--r-- 1 root root 0 Mar  3 13:59 t2.txt

Next value shows last file modification date and time. The last column is the name of the file.

Sorting Output

By default, the ls command is listing the files in alphabetical order. --sort option allows you to sort the output.

  • –sort=extension (or -X ) : Sort alphabetically by extension.
  • –sort=size (or -S) : Sort by file size.
  • –sort=time ( or -t) : Sort by modification time.
  • –sort=version (or -v) : Natural sort of version numbers.

Use the -r option to get the results in the reverse sort order.

# Sort the file by modification time
root@tryit-driven:~# ls -lt /var                                                                                                                       
total 4                                                                                                                                                
drwxr-xr-x 1 root root   126 Mar  3 13:58 lib                                                                                                          
drwxr-xr-x 1 root root    72 Mar  3 06:16 cache                                                                                                          
drwxr-xr-x 1 root root     0 Mar  2 08:29 opt                                                                                                                                                                                                            
drwxrwxrwt 1 root root     0 Feb 17 00:44 lock                                                                                                         
drwxr-xr-x 1 root root     0 Apr 15  2020 backups                                                                                                      
drwxrwsr-x 1 root staff    0 Apr 15  2020 local                                                                                                        
drwxrwxrwt 1 root root     0 Apr 15  2020 tmp 

# Sort the file by modification time in reverse order
root@tryit-driven:~# ls -ltr /var                                                                                                                      
total 4                                                                                                                                                
drwxrwxrwt 1 root root     0 Apr 15  2020 tmp                                                                                                          
drwxrwsr-x 1 root staff    0 Apr 15  2020 local                                                                                                        
drwxr-xr-x 1 root root     0 Apr 15  2020 backups                                                                                                      
drwxrwxrwt 1 root root     0 Feb 17 00:44 lock                                                                                                                                                                                                      
drwxr-xr-x 1 root root    72 Mar  3 06:16 cache                                                                                                        
drwxr-xr-x 1 root root   126 Mar  3 13:58 lib  

List Subdirectories Recursively

By default ls command only lists the files and directories in the top level directory. To display the contents of the subdirectories recursively use -R.

ls -R

To list all files including hidden files (starting with .), use -a option as shown below.-F option will add the ‘/’ character at the end of each directory.

root@tryit-glad:~# ls -a /var                                                                                                                          
.  ..  backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp
                                                                               
root@tryit-glad:~# ls -aF /var                                                                                                                         
./  ../  backups/  cache/  lib/  local/  lock/  log/  mail/  opt/  run@  spool/  tmp/       

Below command will show the file size in human readable format.

ls -lh