17 The most practical example of the ls command on Linux

17 best working examples of the ls command on Linux

17 most practical examples of the ls command

The Linux ls command is used to list information about files and directories in the file system. The most basic command on Linux systems. The ls command can also display additional information about a file. Since it is the most basic command on Linux systems, it is available by default on all Linux distributions.

Basic syntax

The following is the basic syntax of the ls command.

ls [OPTIONS] [FILES]

Here, Options: Specify any additional options to be used with the command. File: File or directory name.

Linux ls command example

Here is an example of the ls command on Linux

1. Simple ls command

If you use the ls command without options, non-hidden Content current directory:

ls

The output looks like this:

17 The most practical example of the ls command on Linux

2. Display files in another directory

To see all the files in another directory, you must use the directory path after the ls command.

In the following sample file /etc The directory will be displayed:

ls /etc

The output looks like this:

17 The most practical example of the ls command on Linux

3. Show hidden files

You can view all files and directories in the current directory. hidden files With options -a ls command options:

ls -a

The output looks like this:

17 The most practical example of the ls command on Linux

In the above output, the hidden file .updated is displayed.

4. View files in long format

To view files in the current directory in long format -l Options are used with the ls command. The following example lists all files, including hidden files long format

ls -la

The output looks like this:

17 The most practical example of the ls command on Linux

5. Display all files recursively

Can be used -R An option of the ls command to recursively display all files in a directory. It also contains a subdirectory file list.

ls -R

The output looks like this:

./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. Show all files sorted by size

To view all files sorted by file size -S Must be used additionally with the ls command. Displays all files sorted by size in the directory.

In the following example, the command sorts and displays all files in the directory size To long format:

ls -lS

The output looks like this:

17 The most practical example of the ls command on Linux

7. Show all files sorted by size in reverse order

To view all files sorted by file size in reverse order -r And -S Must be used additionally with the ls command. Displays all files sorted by size in the directory in reverse order.

In the following example, the command sorts and displays all files in the directory size To long format:

ls -lSr

The output looks like this:

17 The most practical example of the ls command on Linux

8. Show all files sorted by modification time

To view all files sorted by modification time -t Must be used additionally with the ls command. Displays all files in the directory, sorted by modification time, in descending order.

In the following example, the command sorts and displays all files in the directory modification time To long format When hidden files:

ls -alt

The output looks like this:

17 The most practical example of the ls command on Linux

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 Must be used additionally with the ls command. Displays all files in the directory, sorted by modification time, in ascending order.

In the following example, the command sorts and displays all files in the directory modification time To long format When hidden files:

ls -altr

The output looks like this:

17 The most practical example of the ls command on Linux

10. Display specific directory information

To display specific directory information, use the ls command as follows: -d option. The following example displays information for /etc Long format directory:

ls -ld /etc

The output looks like this:

17 The most practical example of the ls command on Linux

11. Display the file in a human-readable format

To view a file in human-readable format -h Options must be used with the ls command. In the following example, the command displays all files in long, human-readable format.

ls -lh

The output looks like this:

17 The most practical example of the ls command on Linux

12. Display files without colors

Can be used to display files without color --color Options for the ls command, such as:

ls --color=no

The output looks like this:

17 The most practical example of the ls command on Linux

13. Display files using inode numbers

To display files with inode numbers -i Options must be used with the ls command. In the following example, the command displays all files by inode number.

ls -i

The output looks like this:

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. View files using UID and GID

To view UID and GID files -i Options must be used with the ls command. In the following example, the command displays all files with UID and GID.

ls -n

The output looks like this:

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 Options must be used with the ls command. In the following example, the command displays all files in reverse order.

ls -r

The output looks like this:

17 The most practical example of the ls command on Linux

16. Displaying detailed information about the ls command

To display detailed information about the ls command --help Options must be used with the ls command.

ls --help

The output looks like this:

17 The most practical example of the ls command on Linux

17. Display colorless files in human-readable format

Can be used to display files in a human-readable format without color --color And -h Options for the ls command, such as:

ls -h --color=no

The output looks like this:

17 The most practical example of the ls command on Linux

Conclusion

You have successfully learned 17 working examples of the ls command on Linux. If you have any questions, don’t forget to comment them out.

Sidebar