1. Overview

The ls command is a command line tool that lists files and directories within the file system. We can also use it to show us detailed information about the listed files. It’s part of the GNU core utilities package, which is installed by default on all Linux distros.

In this tutorial, we’ll explore common options of the ls command. We’ll delve into basic examples and more advanced example utilization that streamlines file management tasks.

2. Common ls Command Options

The basic syntax of the ls command is fairly straightforward:

ls [OPTIONS] [FILES]
  • [OPTIONS]: the various command-line options of the ls command
  • [FILES]: represents the name(s) of the file(s) to be processed

Let’s look at some of the most commonly used ls command options:

Options Description
-m Lists directory contents, separated by commas
-Q Displays directory contents, enclosed within quotation marks
-l Displays files in a long-list format
-lh Displays file sizes in human-readable format
-g Omits the group ownership column
-F Adds a forward slash to directories
-i Displays the inode number of files and directories
-a Displays all files, including hidden files
-la Displays all files and directories in a long list format
-r Sort files in reverse order
-X Sorts files alphabetically by file extension
-R Displays files and directories recursively

3. Common ls command Examples

Let’s dive deeper into some practical examples of using ls.

3.1. List Files and Directories in Linux

Using ls by itself is the most basic and common form of the command. For example, let’s list all the directories and files in our current working directory:

$ ls
 automation                                     meeting_minutes.txt
 AWS-ETL-S3-to-Snowflake                        MISP
 AWSKEY                                         Music
 bin                                            my_file.txt
...

3.2. Long Listing of Files in Linux

For a more detailed view of files and directories, we use the -l option. In particular, this option provides a long listing format, including information such as ownership, file permission, size, and modification time:

$ ls -l 
total 37996
drwxr-xr-x 5 kali kali     4096 Jan 16 13:39 guest-authoring
drwxr-xr-x 8 kali kali     4096 Jan 20  1970 idea-IC-233.13135.103
-rwxrw-rw- 1 kali kali 10282364 Sep 11  2023 SampleVideo.mp4
-rw-r--r-- 1 kali kali 28613436 Apr 10 03:12 warp-terminal_0.2024.04.02.08.02.stable.01_amd64.deb
...

From the output, each line represents a file or directory. The first column shows the file permission. Then, the next shows the owner, the group, the modification time, and finally, the file or directory name.

If we want to also include hidden files and directories, we need to use the -a option:

$ ls -a 
 .                                              ISMS
 ..                                             .java
 .anaconda                                      .jupyter
 automation                                     .lesshst
 .avidemux6
...

In the output, we can see additional entries that are each preceded by a dot, representing hidden files and directories.

The -l option is often combined with the -a option to give us a better view, listing all files and directories in our current working directory, including hidden ones:

$ ls -la
total 335536
drwx------ 85 kali kali      4096 Apr 16 15:38  .
drwxr-xr-x  7 root root      4096 Apr  6 14:48  ..
drwxr-xr-x  3 kali kali      4096 Jun 26  2023  .anaconda
drwxr-xr-x  2 kali kali      4096 Dec  1 04:19  automation
...

3.3. Display File Size in a Human-Readable Format

Next, we can use the -lh option to make file sizes more easily understandable. In particular, this option displays file sizes in a human-readable format, using units such as kilobytes (K), megabytes (M), and gigabytes (G).

For example, let’s list all the files and directories in the current directory with human-readable file sizes:

$ ls -lh
-rwxrw-rw- 1 kali kali 9.9M Sep 11  2023 SampleVideo.mp4
-rw------- 1 kali kali 2.4K May  2 19:20 sincere-muse-422223-0c570b360e21.json
-rw-r--r-- 1 kali kali  26M Apr 24 06:26 terraform_1.8.2_linux_amd64.zip

The system chooses a suitable unit that’s easier to understand than just the total number of bytes.

3.4. Distinguish Directories and Files in Linux

To distinguish between directories and regular files in Linux, we can add the -F option. This option adds a slash character to the end of each directory in the list.

For example, let’s check the listing of all files and directories in the current directories with distinguishing characters:

$ ls -F
 automation/                                    meeting_minutes.txt
 AWS-ETL-S3-to-Snowflake/                       MISP/
 AWSKEY/                                        Music/
 bin/                                           my_file.txt
 cred.py
...

In the output, directories are denoted by a trailing slash (/), while regular files appear without any appended special characters.

3.5. Sorting of Files

We can also use the ls command to sort files. By default, ls sorts in lexical order.

Let’s first demonstrate this default behavior:

$ ls
guest-authoring        SampleVideo.mp4

However, we can sort files in reverse lexical order with the -lr option:

$ ls -lr
total 335268
-rw-r--r--  1 kali kali      9279 Nov 30 06:31  your_output_file.csv
drwxr-xr-x  8 kali kali      4096 Jul  1  2023  XSStrike
-rw-r--r--  1 kali kali       320 Jan 17 13:08  wget-log
...

Moreover, we can also use the ls command with the -lt option to sort files based on their modification time. In particular, sorting files by modification time can be particularly useful when we need to identify recently modified files or track changes in a directory:

$ ls -lt
total 335268
-rw-r--r--  1 kali kali        42 Apr 16 09:35  dir3
-rw-r--r--  1 kali kali        42 Apr 16 09:32  dir1
-rw-r--r--  1 kali kali        42 Apr 16 08:31  dir2
-rwxr-xr-x  1 kali kali       323 Apr 15 10:02  copy_multiple.sh
...

The output displays files and directories listed in descending order based on their modification time, with the most recently modified files appearing first.

We can also use this command to sort by file size with the -S option.

For example, let’s sort files by size by listing all files and directories in the current directory:

$ ls -S
 ideaIC-2023.3.2.tar.gz                   mage-zoomcamp
 jdk-21_linux-x64_bin.deb                 Music

In this output, files and directories are listed in descending order based on their size, with the largest files appearing first.

3.6. List File Inode Number in Linux

ls also provides the ability to display the inode number of files and directories. Thus, we can use the ls command with the -i option to list the inode number along with the files and directories:

$ ls -i
3932431  automation
3935686  meeting_minutes.txt
...

In this output, each line represents a file or directory, with the inode number displayed at the beginning of each line.

3.7. List Specific File Types or Extensions

We can utilize the ls command along with the wildcard character (*) to match specific file types or extensions.

For example, let’s list only text files in the current directory with ls command and wildcards:

$ ls *.txt 
meeting_minutes.txt my_file.txt test.txt

Additionally, we can use wildcards to match files that begin with the same prefix.

For example, to list all files starting with m, we can use:

$ ls m*
meeting_minutes.txt  my_file.txt

This allows for more flexible and specific file listing based on patterns and prefixes.

4. Conclusion

In this article, we explored the versatility of the ls command in displaying details about the files and directories in our system.

We looked at some common commands and how ls has flexibility in both the information it displays and the order of the files and directories it shows us.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments