In Linux/Unix platform ‘ls’ command is one of the most repeatedly used command. I am very sure, this is one of the very first command you have been trained when you get into the shell/command prompt.
This guide will teach you how to use the various options of ‘ls’ command. All the below examples of ‘ls’ command are tested on RHEL/CENTOS 7.6.
The Global Syntax of ls command with options:
ls [OPTION] [File]
There are a lot of options available using ‘ls’ command, but we will be looking the most used and combined possible options only. The following tabular gives you the possible options in ‘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 to all files and directories?
# ls -l
Note: Using ‘ls -l’ option will display long listing format of content one per line of the current directory. The line started with some characteristic of “file or directory permission, Owner and Group Name, File size, created/modified date and time, file/folder name”.
3. How to list all the hidden files?
# 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 begins with the dot ‘.’ format are marked hidden.
4. How to list Directories with ‘/’ classification at the end?
# ls -F
Note: Using ‘ls -F’ will added ‘/’ 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 files and directories without owner details?
# ls -g
Note: ‘ls -g’ option is similar to ‘ls -l’, but with ‘-g’ option will skip the owner details of 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: ‘ls -i’ option will list the index (called inode) number of each file and directory. In the above example you can 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 ‘ls -n’ option will list the UID (User ID) and GID (Group ID) of all the files and directory 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 files and directories in reverse order?
# ls -r
Note: ‘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 subdirectories?
# ls -lR
Note: Using ‘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 folder at first. In the above example you can see a folder named ‘linuxteck’ and a file named ‘test’ are listed at the first place. These two files are the newly created ones.
12. How to list the size of the files in human-readable format?
# ls -lh
Note: Using ‘ls -lh’ option will show the size of each file in a human-readable format. Reading files in bytes may get confused instead we can read files in KB,MB,GB etc, is very easier “e.g.,” To read 5782242 bytes will get confused whereas to read 5.7 M is much user-friendly.
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 ‘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 the list of a particular directory using ‘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 the list of all the files under a particular directory. In the above example you can see all the files are listed under the Desktop directory.
16. How to list files and directories sorting 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 print in the human readable then you can use ‘ls -lSh’. It will print the output file size in KB,MB,GB, etc.
17. How to list the version of ls command?
# ls –v OR # ls –version
Note: Use one of the above commands to print the version of ls command.
Please refer the manual page by using the below commands to explore in-depth
# man ls
# info ls
I hope this article will help you to learn few options of ‘ls’ commands in Linux! Drop me your feedback/comments and please share if you like 🙂