Using ls command in Linux


Gaming Laptop Series

The ls command is a basic and one of the most frequently used commands in a Linux or Unix system. It is used to list files and directories of a filesystem. When used with certain options it displays a list of files and directories with very useful information.

Today we will discuss the usage of the ls command in Linux with some examples.

Syntax of ls command

The syntax of the ls command is given below –

ls [Options] [File]

To see a detailed list of options that can be used with the ls command execute the following command in your terminal –

man ls

Display the list of files and directories

When the ls command is used without any arguments it simply displays the list of files and directories under the current working directory.

ls

You can see the output in the image below –

How to display the file and directories in the long listing format

When ls command is used with option -l (lowercase L) it displays the files and directories in the long listing format. In long listing format output includes information such as file name, modified date and time, size, owner of the file, and its permission.

Use the following command to display the files and directories in long listing format –

ls -l

Now see the output of this command –

Print the output in reverse order

You can print the output in reverse order by using option -r with the ls command. Now use the following command in your terminal –

ls -lr
[alert color=yellow]Note: While listing file or directories at some places such as /root you might need to have root permissions so you should use the sudo with the command[/alert]

How to view the hidden files and directories on Linux

A hidden file or directory starts with . (dot) in its name. To display the list of all files and directories including the hidden one use option -a with ls command.

ls -a

Or use the following to display all files and folders in long listing format –

ls -al

Display all the files in a human-readable format

When you use option -h with ls command it will display output in a human-readable format in which it displays the size like 500K, 2M, 1.5G, etc.

Now execute the following command –

ls -lah

The output will look like this –

Listing subdirectories recursively

If you use -R with the ls command it will display subdirectories recursively. Now use the following command –

ls -R

Conclusion

For more usage of ls see its manual page. Now if you have a query then leave it in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.