Commonly Used ls Command Examples in Linux

The ls command is a standard in Unix and Unix-like systems such as Linux. It is used to display a directory’s contents. This article describes quickly some of the most widely used ls command examples in Linux.

1. The simplest way to use the ls command is to run it without any options or arguments, like this. It will provide a list of all the files and folders in the current directory:

$ls

2. By default, ls will not list hidden files. To list all files and directories, including hidden files (files with names that start with a dot "."), add the -a flag:

$ls  -a

3. To display a long format list of files and directories, including permissions, ownership, size, and modification date, add the -l flag:

$ls  -l

4. To show file sizes in human-readable formats, such as "200K" or "10M" or "1.2G". This option is best used with the -l option:

$ls -lh

5. To sort files by size, with the largest files listed first, use the -S option. This option is best used in the long listing format while showing file sizes using the -h flag:

$ls  -S
$ls  -lhS

6. To sort files by modification time, with the most recently modified files listed first, use the -t option:

$ls  -t
$ls  -lt

7. When you add the -r flag, ls will list files and directories in reverse order. You can also combine it with the -l flag like this:

$ls  -lr

8. You can also display only the directory name without showing its contents by using the -d flag:

$ls -d
$ls  -ld

9. To add a trailing character to each filename to indicate its type (such as a "/" for directories or a "*" for executable files), use the -F options like this:

$ls -F

10. Last but not least, for better readability, you can add the -G option to display files and directories with colorized output. This option might not have any effect if your terminal already has well-configured coloring:

$ls -G

Conclusion

These are just a few instances of the various options available with the ls command. You may access the manual page for the ls command and its options by typing man ls on the terminal.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *