If you want to identify the content of a particular directory in Linux, you can use the ls command. This command is essential for navigating and managing your files in Linux, and here is the step-by-step guide on how to use it:
The ls command basic syntax
By default, the ls command lists the contents of the current directory. Just type the following on your Linux terminal:
ls
This command provides a simple list of the files and directories in the current directory
Desktop Documents Downloads Music Pictures Videos
Listing Files with Details
Using the ls command with the -l option , it allows you to list files and directories with detailed information like permissions, ownership, size, and modification date:
ls -l
Running the above command will give you an output with the following listing format:
drwxr-xr-x 2 user group 4096 Aug 31 10:20 Desktop
-rw-r--r-- 1 user group 543 Aug 30 08:15 file.txt
We can interpret the output as follows:
- The first column contains file permissions: drwxr-xr-x and -rw-r–r–
- The second column contains the number of links (hard links).
- The third column containers the owners of the files called user in this case
- The fourth column contains the group to which the owner of the file belongs to.
- The fifth column contains files sizes in bytes.
- The sixth column contains the date and time of last modifications to the files.
- The seventh column contains the filenames and directories associated to the previous columns.
RECOMMENDED READING: How to use the cd command to navigate Linux directories
Including Hidden Files
Files that begin with a dot (.
) are hidden by default. To list all files, including hidden ones, use the -a
option.
ls -a
When you use ls with the -a option, it will list all files in the current directory including the hidden files that start with dots in their names.
Combining Options for Detailed Hidden Files
To make a detail list of files including hidden files, you can use ls with the -la options combination:
ls -la
Sorting the List
The ls command also allows you to sort the output based on various criteria. For example, to sort files by modification time, you can use the -t
option:
ls -lt
The -lt options combination will produce a list of files and directories starting with the latest modification date and time:
-rw-r--r-- 1 user group 543 Aug 31 10:20 latest-file.txt
-rw-r--r-- 1 user group 321 Aug 30 08:15 older-file.txt
For reverse order, add the -r
option:
ls -ltr
Listing Files in Human-Readable Format
By default, the ls command produces files sizes in bytes. But you can use the -h option along with -l to display sizes in KB, MB, etc.
ls -lh
The -lh options combination will display file sizes in kilobytes:
drwxr-xr-x 2 user group 4.0K Aug 31 10:20 Desktop
-rw-r--r-- 1 user group 543K Aug 30 08:15 file.txt
Recursive Listing
To list all files and directories, including the contents of all subdirectories, use the -R
option.
ls -R
This command will produce the following sample output:
.:
Desktop Documents Downloads
./Desktop:
./Documents:
file1.txt file2.txt
Listing Directory Information
If you want to see only the directories, not files, you can use the following combination:
ls -d */
Listing Files by Size
You can list files by sorting them from the largest files to the smallest size using the -S
option:
ls -lS
Here is the sample output of using the -lS options combination:
-rw-r--r-- 1 user group 1.2M Aug 31 10:20 largefile.txt
-rw-r--r-- 1 user group 543 Aug 30 08:15 smallfile.txt