17 Best Practical Examples of ls Command in Linux

17 Best Practical Examples of ls Command in Linux
17 Best Practical Examples of ls Command in Linux

17 Best Practical Examples of ls Command

The ls command in Linux is used to list files and directories information inside the filesystem. It is the most basic command in the Linux systems. The ls command can also display additional information about files. As it is the most basic command in Linux systems, It is available in all Linux distributions by default.

Basic Syntax

Ffolllowing is the basic sysntax of ls command:

ls [OPTIONS] [FILES]

Where,
OPTIONS: It specifies the options that additionally used with command.
FILES: File or directory name.

Examples of ls Command in Linux

Following are the examples of ls command in Linux

1. Simple ls Command

When you use ls command without any options, it will display all the non-hidden contents in current directory:

ls

The output should be:

"17

2. Display files inside another directory

To display all the files inside another directory you should use ls command followed by the path of the directory.

In the following example files inside /etc directory will be displayed:

ls /etc

The output should be:

"17

3. Display Hidden Files

You can display all the files and directories inside the current directory including hidden files by using option -a option with ls command:

ls -a

The output should be:

"17

In above output you can seen hidden file .updated is showed.

4. Display Files in Long Format

To display files in the current directory in long format -l option is used with ls command. In the following example the all the files including hidden files will be listed in long format

ls -la

The output should be:

"17

5. Display All Files Recursively

You can use -R option with ls command to display all files in the directory recursively. It also includes subdirectory file listings.

ls -R

The output should be:

./unbound:
icannbundle.pem root.key./UPower:
UPower.conf./vbox:./vmware-tools:
guestproxy-ssl.conf resume-vm-default suspend-vm-default
poweroff-vm-default scripts vgauth
poweron-vm-default statechange.subr vgauth.conf./vmware-tools/scripts:
vmware

6. Display All Files Sorted by Size

To display all files sorted by file size -S should be used with ls command additionally. It displays all the files sorted by size in the directory:

In the following example the command will display all files inside directory sorted by size in long format:

ls -lS

The output should be:

"17

7. Display All Files Sorted by Size in Reverse Order

To display all files sorted by file size in reverse order -r and -S should be used with ls command additionally. It displays all the files sorted by size in the directory in reverse order:

In the following example the command will display all files inside directory sorted by size in long format:

ls -lSr

The output should be:

"17

8. Display All Files Sorted by Modification Time

To display all files sorted by modification time -t should be used with ls command additionally. It displays all the files sorted by modification time in the directory in decreasing order:

In the following example the command will display all files inside directory sorted by modification time in long format with hidden files:

ls -alt

The output should be:

"17

9. Display All Files Sorted by Modification Time in Reverse Order

To display all files sorted by modification time in reverse order -r and -t should be used with ls command additionally. It displays all the files sorted by modification time in the directory in increasing order:

In the following example the command will display all files inside directory sorted by modification time in long format with hidden files:

ls -altr

The output should be:

"17

10. Display Specific Directory Information

You can display specific directory information by using the ls command with -d option. In the following example, it will display information of the /etc directory in the long format:

ls -ld /etc

The output should be:

"17

11. Display files in Human Readable Format

To display files in human readable format -h option should be used with ls command. In the following example, the command will display all files in human readable long format:

ls -lh

The output should be:

"17

12. Display files without color

You can display files without color by using --color option with ls command like given below:

ls --color=no

The output should be:

"17

13. Display Files with Inode Numbers

To display files with inode numbers -i option should be used with ls command. In the following example, the command will display all files with inode number:

ls -i

The output should be:

3932186 abrt                      3932247 modprobe.d
3932770 adjtime                   3932248 modules-load.d
3932771 aliases                   3932831 motd
3932187 alsa                      3933189 motd.d
3932188 alternatives              3933860 mtab
3932772 anacrontab                3932832 mtools.conf
3934267 ant.d                     3932249 multipath
3932773 appstream.conf            3932833 my.cnf
3932774 asound.conf               3932250 my.cnf.d
3932775 at.deny                   3932834 nanorc
3932776 atmsigd.conf              3932251 ndctl

14. Display Files with UID and GID

To display files with UID and GID -i option should be used with ls command. In the following example, the command will display all files with UID and GID:

ls -n

The output should be:

total 2140
drwxr-xr-x.  3  0   0   4096 Feb  5 21:37 abrt
-rw-r--r--.  1  0   0     46 Apr 17 11:14 adjtime
-rw-r--r--.  1  0   0   1518 Jul 13  2018 aliases
drwxr-xr-x.  3  0   0   4096 Jan  7 20:33 alsa
drwxr-xr-x.  2  0   0   4096 May 26 01:55 alternatives
-rw-r--r--.  1  0   0    541 Mar 18 16:26 anacrontab
drwxr-xr-x.  2  0   0   4096 Apr 20 09:56 ant.d
-rw-r--r--.  1  0   0    769 Aug  4  2018 appstream.conf

15. Display Files in Reverse Order

To display files in reverse order -r option should be used with ls command. In the following example, the command will display all files in reverse order:

ls -r

The output should be:

"17

16. Display More Information about ls Command

To display more information about ls command --help option should be used with ls command:

ls --help

The output should be:

"17

17. Display Files Without Color in Human Readable Format

You can display files without color in human readable format by using --color and -h option with ls command like given below:

ls -h --color=no

The output should be:

"17

Conclusion

You have successfully learned 17 Best Practical Examples of ls Command in Linux. If you have any queries please don’t forget to comment out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here