21 Simple ls Command Examples For Linux

The ‘ls’ command is a standard GNU command used in Unix/Linux based operating systems to list directory contents and display information about the sub directories and files within.

The practical examples in this guide will show you how to use the ls command in all sorts of different situations.

As a core GNU command, ls should be installed and available by default. In CentOS it’s part of the coreutils package.

How To Use ls – Command Examples

  • 1. List Directory Contents

    When the ls command is run without any arguments, it prints out the contents of the current working directory by default.

    [root@centos7 ~]# ls
    abc.zip  anaconda-ks.cfg  Downloads  file.txt  swapfile  test  wget-log.txt  z.mp3
    

    We can optionally choose a specific directory that we want to list the contents of as an argument to the command.

    [root@centos7 ~]# ls /etc/httpd/
    conf  conf.d  conf.modules.d  logs  modules  run
    
  • 2. Long Format Listing

    As shown above ls is only showing us the names of the files and directories. We can use the -l option to display long format listing, which provides a lot more information about the contents.

    [root@centos7 ~]# ls -l
    total 1051924
    -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
    drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    

    Now let’s cover what each column in the output above means.

    1. The first column represents the permissions set on the file or directory. The first character here represents the type of file, for example files start with ‘-‘ while directories start with ‘d’. Therefore we can tell that ‘Downloads’ and ‘test’ are directories as they start with ‘d’.
    2. The second column refers to the number of links to the file or directory.
    3. The third column shows the user that owns the file or directory.
    4. The fourth column shows the group that owns the file or directory.
    5. The fifth column shows the file size in bytes of the file or directory.
    6. The sixth column shows the last modified date of the file or directory.
    7. The seventh column shows the actual name of the file or directory.
  • 3. Human Readable

    As we have seen above, the size of a file or directory is displayed in bytes. We can use the -h option to change the size to human readable format, which will show us K, M, or G values for example.

    [root@centos7 ~]# ls -lh
    total 1.1G
    -rw-r--r--. 1 root root   41 Sep  1 11:24 abc.zip
    -rw-------. 1 root root  984 Aug 29 14:21 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 4.0K Aug 31 13:58 Downloads
    -rw-r--r--. 1 root root  406 Dec 11  2015 file.txt
    -rw-r--r--. 1 root root 1.0G Aug 31 15:35 swapfile
    drwxr-xr-x. 4 root root   66 Aug 30 10:32 test
    -rw-r--r--. 1 root root  532 Aug 31 12:18 wget-log.txt
    -rw-r--r--. 1 root root 3.3M Sep  1 11:29 z.mp3
    

    Now we can much more easily see the sizes of our contents.

    Note that we have run -l and -h together here, as without -l, the size information would not display.

  • 4. Show Hidden Files

    By default the ls command will not display hidden files or directories, that is files or directories that start with the ‘.’ full stop symbol. We can view all hidden contents with ls by using the -a option to display all.

    [root@centos7 ~]# ls -la
    total 1051972
    dr-xr-x---.  6 root root       4096 Sep  1 11:41 .
    dr-xr-xr-x. 17 root root       4096 Aug 29 14:21 ..
    -rw-r--r--.  1 root root         41 Sep  1 11:24 abc.zip
    -rw-------.  1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    -rw-------.  1 root root      12045 Aug 31 16:41 .bash_history
    -rw-r--r--.  1 root root         18 Dec 29  2013 .bash_logout
    -rw-r--r--.  1 root root        176 Dec 29  2013 .bash_profile
    -rw-r--r--.  1 root root        176 Dec 29  2013 .bashrc
    drwxr-xr-x.  2 root root       4096 Aug 31 13:58 Downloads
    -rw-r--r--.  1 root root        406 Dec 11  2015 file.txt
    -rw-------.  1 root root        144 Sep  1 10:56 .lesshst
    drwxr-xr-x.  3 root root         22 Aug 31 14:36 .matplotlib
    drwxr-----.  3 root root         18 Aug 29 15:14 .pki
    -rw-------.  1 root root       1024 Aug 29 16:06 .rnd
    -rw-r--r--.  1 root root 1073741824 Aug 31 15:35 swapfile
    drwxr-xr-x.  4 root root         66 Aug 30 10:32 test
    -rw-------.  1 root root       4715 Sep  1 11:24 .viminfo
    -rw-r--r--.  1 root root        532 Aug 31 12:18 wget-log.txt
    -rw-r--r--.  1 root root    3407872 Sep  1 11:29 z.mp3
    
  • 5. Reverse Order

    By default the output of the ls command is sorted in alphabetical order. We can reverse this order with the -r option.

    [root@centos7 ~]# ls -lr
    total 1051924
    -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
    -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    
  • 6. Sort By Modification Time

    We can use the -t option to sort the output of ls by newest to oldest content.

    [root@centos7 ~]# ls -lt
    total 1051924
    -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
    drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    

    I like using this in combination with the previously mentioned -r option, which will instead list output oldest to newest, allowing us to see the most recently changed files at the bottom of the output.

    [root@centos7 ~]# ls -lrt
    total 1051924
    -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
    -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    

    I find this extremely helpful when listing the content of the /var/log directory, as it allows you to see which files were most recently modified right at the end of your output.

  • 7. Sort By File Size

    With the -S option we can sort the files and directories by size, largest to smallest.

    [root@centos7 ~]# ls -lS
    total 1051924
    -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
    -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    

    Again we could combine this with the -r option to reverse the results, or -h to make the file sizes more readable. Here we do both, and sort files from smallest to largest with human readable sizes.

    [root@centos7 ~]# ls -lrSh
    total 1.1G
    -rw-r--r--. 1 root root   41 Sep  1 11:24 abc.zip
    drwxr-xr-x. 4 root root   66 Aug 30 10:32 test
    -rw-r--r--. 1 root root  406 Dec 11  2015 file.txt
    -rw-r--r--. 1 root root  532 Aug 31 12:18 wget-log.txt
    -rw-------. 1 root root  984 Aug 29 14:21 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 4.0K Aug 31 13:58 Downloads
    -rw-r--r--. 1 root root 3.3M Sep  1 11:29 z.mp3
    -rw-r--r--. 1 root root 1.0G Aug 31 15:35 swapfile
    
  • 8. List Only Directories

    By default if we specify a directory at the end of the ls command, we will see the content within that directory. Instead we may want to simply view information about the directory itself, which is what the -d option is for.

    In the below example, we run ls -la on the test directory, and see that the contents within are returned. Once we change it to also use -d, we instead see information about the test directory itself, though it’s worth noting this also displays with -la as the current directory is shown as ‘.’

    [root@centos7 ~]# ls -la test/
    total 66192
    drwxr-xr-x. 4 root root       66 Aug 30 10:32 .
    dr-xr-x---. 6 root root     4096 Sep  1 11:41 ..
    -rw-r--r--. 1 root root  2823796 Aug 30 09:39 1.txt
    -rw-r--r--. 1 root root  2823796 Aug 30 09:39 2.mp3
    -rw-r--r--. 1 root root 62123512 Aug 30 10:32 3.iso
    drwxr-xr-x. 3 root root       30 Aug 30 09:39 test1
    drwxr-xr-x. 3 root root       38 Aug 30 09:40 test2
    
    [root@centos7 ~]# ls -lad test/
    drwxr-xr-x. 4 root root 66 Aug 30 10:32 test/
    
  • 9. View SELinux Context

    The ls command can display SELinux contexts, which is useful for Linux distributions such as RHEL or CentOS that have SELinux enabled and set to enforcing mode by default.

    [root@centos7 ~]# ls -Z
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 abc.zip
    -rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
    drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Downloads
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file.txt
    -rw-r--r--. root root unconfined_u:object_r:swapfile_t:s0 swapfile
    drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 test
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 wget-log.txt
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 z.mp3
    

    Note that the size and date columns have now been replaced with SELinux context information, this remains true even if we specify ls -lZ.

  • 10. List Contents Recursively

    We can list the contents recursively with the -R option, this will also list the contents within any sub directories that exist inside the directory we are running ls against.

    [root@centos7 ~]# ls -lR test/
    test/:
    total 66188
    -rw-r--r--. 1 root root  2823796 Aug 30 09:39 1.txt
    -rw-r--r--. 1 root root  2823796 Aug 30 09:39 2.mp3
    -rw-r--r--. 1 root root 62123512 Aug 30 10:32 3.iso
    drwxr-xr-x. 3 root root       30 Aug 30 09:39 test1
    drwxr-xr-x. 3 root root       38 Aug 30 09:40 test2
    
    test/test1:
    total 2760
    -rw-r--r--. 1 root root 2823796 Aug 30 09:39 1
    
    test/test2:
    total 5520
    -rw-r--r--. 1 root root 2823796 Aug 30 09:40 7
    -rw-r--r--. 1 root root 2823796 Aug 30 09:40 8
    

    Here we can see that the contents of the test directory are listed, as well as the sub directories inside it called test1 and test2 which both contain some numbered files.

  • 11. Inode Information

    The -i option prints the index number of each file, allowing us to see its inode.

    [root@centos7 ~]# ls -li
    total 1051924
    67191492 -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    67869919 -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
      145701 drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    67183553 -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    67191475 -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
      145703 drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    67151832 -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    67183574 -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    

    The inode has now become the first column and moved everything else over to the right.

  • 12. Indicate Directory

    There are a few ways we can determine if something in our ls output is a directory, such as the colour, or if we use the -l option it will start with a ‘d’. We can also use the -p option which will add a ‘/’ to the end of directories, making it a little more obvious.

    [root@centos7 ~]# ls
    abc.zip  anaconda-ks.cfg  Downloads  file.txt  swapfile  test  wget-log.txt  z.mp3
    
    [root@centos7 ~]# ls -p
    abc.zip  anaconda-ks.cfg  Downloads/  file.txt  swapfile  test/  wget-log.txt  z.mp3
    
  • 13. Classify Content With Symbols

    Like the -p option above, -F can also be used to append various symbols to the end of different types of files. The -F option will also add a ‘/’ to the end of a file, but will also add a ‘@’ to the end of a link, and a ‘*’ to the end of an executable file, allowing us to easily visually classify the file and directory output of ls.

    [root@centos7 ~]# ls -F
    abc.zip  anaconda-ks.cfg  Downloads/  file.txt  script.sh*  swapfile  test/  tmp-link@  wget-log.txt  z.mp3
    
  • 14. Classify Content With Colours

    We can also classify by adding colour using the --color option, as you may have noticed this is enabled by default in many Linux distributions such as CentOS.

    In CentOS this is aliased to actually run ls with ‘--color=auto’ every time, which will change the colours of the output based on things like file extension. It can help you visually distinguish between files and directories.

    [root@centos7 ~]# alias | grep ls
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    

    ls command color

    We can disable the colouring with --color=never

  • 15. Hide User Or Group

    With the -l option we can see the user and group that owns a file or directory. Using the -g option we can hide the user, and while using the -G option we can hide the group. Both can be run together to hide both the user and group information.

    [root@centos7 ~]# ls -gG
    total 1051928
    -rw-r--r--. 1         41 Sep  1 11:24 abc.zip
    -rw-------. 1        984 Aug 29 14:21 anaconda-ks.cfg
    drwxr-xr-x. 2       4096 Aug 31 13:58 Downloads
    -rw-r--r--. 1        406 Dec 11  2015 file.txt
    -rwxr-xr-x. 1         45 Sep  1 12:02 script.sh
    -rw-r--r--. 1 1073741824 Aug 31 15:35 swapfile
    drwxr-xr-x. 4         66 Aug 30 10:32 test
    -rw-r--r--. 1        532 Aug 31 12:18 wget-log.txt
    -rw-r--r--. 1    3407872 Sep  1 11:29 z.mp3
    
  • 16. List User And Group IDs

    We can see the UID/GID owner of a file or directory with the -n option. By default the UID/GID values are enumerated if the user or group exists, however we can force ls to display the UID/GID number with this.

    [root@centos7 ~]# ls -l /home/
    total 0
    drwx------. 2 user1 user1 79 Aug 30 11:29 user1
    drwx------. 2 user2 user2 79 Aug 30 11:30 user2
    
    [root@centos7 ~]# ls -n /home/
    total 0
    drwx------. 2 1000 1000 79 Aug 30 11:29 user1
    drwx------. 2 1001 1001 79 Aug 30 11:30 user2
    

    Here we can see that the user1 user and group has the UID/GID of 1000, while user2 has 1001.

  • 17. Sort By File Extension

    We can also sort the ls output based on the file extension with the -X option, which will arrange the output in alphabetical order based on the extension.

    [root@centos7 ~]# ls -lX
    total 1051928
    drwxr-xr-x. 2 root root       4096 Aug 31 13:58 Downloads
    -rw-r--r--. 1 root root 1073741824 Aug 31 15:35 swapfile
    drwxr-xr-x. 4 root root         66 Aug 30 10:32 test
    -rw-------. 1 root root        984 Aug 29 14:21 anaconda-ks.cfg
    -rw-r--r--. 1 root root    3407872 Sep  1 11:29 z.mp3
    -rwxr-xr-x. 1 root root         45 Sep  1 12:02 script.sh
    -rw-r--r--. 1 root root        406 Dec 11  2015 file.txt
    -rw-r--r--. 1 root root        532 Aug 31 12:18 wget-log.txt
    -rw-r--r--. 1 root root         41 Sep  1 11:24 abc.zip
    
  • 18. Show Almost All

    We can show almost all with the -A option, this works in a similar way to -a discussed previously, but ignores the current directory ‘.’ and the directory above ‘..’ from the results.

    [root@centos7 ~]# ls -A
    abc.zip          .bash_history  .bash_profile  Downloads  .lesshst     .pki  script.sh  test      .viminfo      z.mp3
    anaconda-ks.cfg  .bash_logout   .bashrc        file.txt   .matplotlib  .rnd  swapfile   tmp-link  wget-log.txt
    
    [root@centos7 ~]# ls -a
    .   abc.zip          .bash_history  .bash_profile  Downloads  .lesshst     .pki  script.sh  test      .viminfo      z.mp3
    ..  anaconda-ks.cfg  .bash_logout   .bashrc        file.txt   .matplotlib  .rnd  swapfile   tmp-link  wget-log.txt
    

    We can see that -a includes ‘.’ and ‘..’ listed in the output while the results from -A do not.

  • 19. Display One Entry Per Line

    By default the ls command by itself will list all contents on the same line, with the -1 option we can instead list one file or directory per line.

    [root@centos7 ~]# ls -1
    abc.zip
    anaconda-ks.cfg
    Downloads
    file.txt
    script.sh
    swapfile
    test
    tmp-link
    wget-log.txt
    z.mp3
    
  • 20. Show Version

    We can display the verison of our ls command with the --verison option.

    [root@centos7 ~]# ls --version
    ls (GNU coreutils) 8.22
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later .
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Richard M. Stallman and David MacKenzie.
    
  • 21. Show Help

    If you need any further assistance with the ls command, run it with the --help option which will provide further information and documentation. Note that I have not included all of the output here for brevity.

    [root@centos7 ~]# ls --help
    Usage: ls [OPTION]... [FILE]...
    

Summary

We have seen that the ls command has a lot of very useful options that allow us to show exactly the information about a file or directory that we need.

  1. Hi

    Good and very useful information about list command. Every option is explained well and easy to understand.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>