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. By default, without any option or argument, it displays the contents of the current directory.
If we want to view the contents of a specific directory, we need to specify its path as an argument. For example, the following command lists the content of the data directory.
#ls data
If the specified directory is available in another directory, we need to specify its full path. For example, to view the content of the home directory, we use the following command.
#ls /home
We use the options and arguments to view detailed information about the files and directories stored in the specified directory.
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
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 of this command is organized 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. To the left of the date and time, it shows the file size in bytes. It shows the group and owner before the file size. The next left column shows the number of links it has. The next left column shows the permission which determines who can read, write, or execute it. In the leftmost, it shows a letter that shows the type of file or directory.
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. |
We can also use multiple options and arguments. For example, to view detailed information including hidden files, we will use the following command.
#ls -la
The following table lists the meaning of important 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 with the newest file first |
-ltr | Lists all files sorted by date and time with the oldest file first |
-R | Lists the contents of the specified directory and all its subdirectories |