Ls Command in Linux with Examples

Ls command in Linux with Examples

ls command is used to list the contents of a directory.

The command has the following syntax.

Ls [OPTION]… [FILE]

This article gives you some examples of ls command. The command has been executed on Red Hat Enterprise Linux 9.

Examples:

List the entries of a current directory

If you want to list the content of your current working directory. The command is as follows:

ls

List the entries of a directory along with detail

Suppose you want to list the content along with details of its files, use -l option. The details include size, owner name, date of modification, and permissions.

ls -l

List the content of a specific directory

If you want to list the content of a specific directory, provide the path to the command as follows.

ls [Folderpath]

Suppose you want to view the content of /home/karim/Downloads, the command should be executed as follows.

ls /home/karim/Downloads

List the entries of a directory along with hidden files

By default, ls doesn’t show the hidden files. If you want to list the hidden files along with other files, execute the command with -a option.

ls -a

Print the entries in human-readable format

Use the -h and -s to print to show the content of a directory and its sizes in a human-readable format.

ls -h -s

List and Sort the entries of a directory

If you would like the content of a directory is listed in sorted order (by file size, largest first).

ls -S

Sort the entries by time

If you want to list the content sorted by time (newest first), use -t option as follows.

ls -t

Separate the entries with a comma

If you want to separate the entries of the content with a comma, use the -m parameter.

ls -m

Enclose entries in double quotes

If you want to enclose the entries in double quotes, use the -Q option as follows.

ls -Q

Print the index number

If you want to print the index number of files, use the -i option as follows.

ls -i

Thank you for reading this article. If you have any queries, you can send us via the comments.