On Linux/Unix platforms, the 'ls' command is one of the most frequently used commands. This should be one of the first commands you train when you enter the shell/command prompt.
This guide will teach you how to use the various options of 'ls' command. All the below examples of the 'ls' command are tested on RHEL/CENTOS 7.6.
Global Syntax ls command with options:
ls [OPTION] [File]
There are a lot of options available using the 'ls' command, but we will be looking at the most used and combined possible options only. The following tabular gives you the possible options and functions in the 'ls' command:
1. How to list files using 'ls' with no option?
anaconda cron-20190721 glusterfs maillog-20190804 pluto secure-20190730
sssd Xorg.9.log audit cron-20190730 httpd mariadb ppp
Note: Using the 'ls' command with no option will simply list all files and directories. You cannot see any other information.
2. How to long list one per line for all files and directories?
# ls -l
Note: Using the 'ls -l' option will display a long listing format of content one per line of the current directory. The line started with some characteristics of "file or directory permission, Owner and Group Name, File size, created/modified date and time, file/folder name".
# ls -a
Note: 'ls -a' will list all the hidden files started with (DOT '.') format along with the normal files. In Unix/Linux all hidden files begin with the dot '.' the formats are marked hidden.
4. How to list Directories with '/' classification at the end?
# ls -F
Note: Using 'ls -F' will add '/' classification at the end of each directory. In the above example, you can see all the directories are listed with '/' sign at the end.
5. How to list all the files and directories without the owner details?
# ls -g
Note: The 'ls -g' option is similar to the 'ls -l' option, but with the '-g' option it will skip the owner details of the files and directories. In the above example it lists all the files without owner details.
6. How to list the index (inode) number of each File and Directory?
# ls -i
Note: The 'ls -i' option will list the index (called inode) number of each file and directory. In the above example you can have some number index/inode printed before the files and directories.
7. How to list the files and directories separated by a comma?
Note: Using 'ls -m' will display all the files and directories separated by a comma.
8. How to list UID and GID of files and directories?
# ls -n
Note: Using the 'ls -n' option will list the UID (User ID) and GID (Group ID) of all the files and directory as one per line. In the above example you can see a normal user and group (UID and GID) has 1000 whereas the root UID and GID has 0.
9. How to list all the files and directories in reverse order?
# ls -r
Note: The 'ls -r' option will list all files and directories in reverse order. In the above example you can see, all the files and directories are sorted in reverse alphabetical order.
10. How to get a recursive listing of all sub-directories?
# ls -lR
Note: Using the 'ls -lR' option will long list in the tree format of all directory and sub-directories.
11. How to list the most recently modified files and folders?
# ls -t
Note: Option 'ls -t' will list out all the recently modified files and folders first. In the above example you can see a folder named 'linuxteck' and a file named 'test' are listed in the first place. These two files are the newly created ones.
12. How to list the size of the files in a human-readable format?
# ls -lh
Note: Using the 'ls -lh' option will show the size of each file in a human-readable format. Reading files in bytes may get confusing instead we can read files in KB,MB,GB etc, it is much easier "e.g.," To read 5782242 bytes will get confused whereas to read 5.7 M is much more user-friendly.
In addition, the ls command won't display the total amount of space taken up by the contents of the directory. In order to determine the size of any directory, you can use the du command
13. How to list a particular file details?
# ls -l cron.txt
Note: Using the above command, you can get the list of a particular file using the 'ls -l filename' command. In the above example you can see the details of "cron.txt" file details.
14. How to list a particular directory details?
# ls -ld testing/
Note: Using the above command you can get a list of a particular directory using the 'ls -ld directory name' command. In the above example you can see the details of "testing" directory details.
15. How to list the files under a directory?
# ls -l Desktop/
Note: Using the above command, you can get a list of all the files under a particular directory. In the above example you can see all the files listed under the Desktop directory.
16. How to list files and directories sorted by file-size?
# ls -lS
Note: Using the above command will list all the files and directories sorted by file size. In the above example you can see the largest size file (lastlog) shown at the beginning of the list. If you want this list to be printed in the human readable then you can use 'ls -lSh'. The output file size in KB, MB, GB, etc.
17. How to list the version of the ls command?
# ls --v OR # ls --version
Note: Use one of the above commands to print the version of the ls command.
Please refer to the manual page using the below commands to explore in-depth
# man ls
# info ls
I hope this article will help you to learn a few options of 'ls' commands in Linux! Drop me your feedback/comments and please share if you like 🙂
A few ls references are collected from this site