Listing Files and Directories on Linux

Linux uses files and directories to save and organize data and information. A file is a named collection of digital content such as text, audio, video, image, etc. A directory is a container. We use it to save and manage files and other directories.

Listing files and directories is an essential task on Linux. We use the ls command to list files and directories. It supports many options and arguments. Options modify the default output. Arguments provide the required data. The most commonly used argument is the directory's path, which contains the contents we want to list. Without an argument, it displays the contents of the current directory.

ls command

To view the contents of a specific directory, specify its name as an argument. For example, the following command lists the content of the data directory.

#ls data

listing data folder

If the desired directory is available in another directory, specify its path with the name. For example, to view the content of the home directory, we use the following command.

#ls /home

listing home folder

Without any option, it only lists the names of the contents. It does not provide any details about them. We use options to get detailed information about the contents. Let us understand important options and arguments through examples.

A hidden file or directory starts with a dot. By default, this command does not list the hidden files and directories. We use the option -a to view the hidden files.

#ls -a

viewing hidden file

We use the -l option to view detailed information (such as size, permission, owner, location, created date, modified date, etc.) about the files and directories stored in the specified directory.

#ls -l

The output organizes information in columns. The rightmost column shows the name of the file or directory. Before the name, it shows the date and time when it was last modified. It displays file size in bytes to the left of the date and time. It shows the group and owner before the file size. The next left column indicates the number of links it has. The next left column shows the permission that defines who can read, write, or execute it. The leftmost side shows the type of file or directory using a letter.

The following table shows the meaning of the letters it can have.

Letter Meaning/ Description
l It is a symbolic link to another file. In other words, it is a shortcut to another file.
d It is a directory.
-(hyphen) It is a regular file.
b It is a block device.
c It is a character device such as a serial port or a terminal.

ls -l command output

We can also use multiple options and arguments. For example, we use the following command to view detailed information, including hidden files.

#ls -la

The following table lists the meaning of essential options we can use to list files and directories.

Option Description
-a Includes hidden files and directories in the output.
-l Displays detailed information about the content such as the file type, permissions, link count, owner, group, size, date and time of last modification, and name of the file or directory
-lh Displays file sizes in human-friendly format
-lt Lists all files sorted by date and time (the newest file first)
-ltr Lists all files sorted by date and time (the oldest file first)
-R Lists the contents of the specified directory and all its subdirectories

Conclusion

Viewing and listing the contents of a directory is an essential file management task. The ls command does this job. It accepts a directory path as an argument. It lists the content of the specified directory. It supports many options. This tutorial explained the most widely used options and their meanings.

ComputerNetworkingNotes Linux Tutorials Listing Files and Directories on Linux

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com