Linux command ls 8 options

When it comes to the most frequently used Linux commands, ls is certainly at the top of the list. Let’s explore the usage of the Linux command ls, which is short for “list”.

Introduction to the Linux Command ls

The ls command is one of the most basic and frequently used commands in the Linux system, used to display a list of files and directories. By using the ls command, you can quickly view the contents of the current working directory, and various options allow you to adjust the output format.

Basic Usage

The ls command displays a list of files and directories in the current directory. By default, it excludes hidden files, which are files that begin with a dot (.).

ls
ShellScript

The above command will display all file names in the current directory as shown in the illustration.

Figure 1. Linux Command ls Execution Result
Figure 1. Linux Command ls Execution Result

Key Options of the Linux Command ls

Here are some commonly used options:

-l (long listing format)

This option displays detailed information about each file, including permissions, owner, group, size, modification time, and name.

ls -l
ShellScript

The output will look like this:

Figure 2. Linux Command ls -l Option Applied
Figure 2. Linux Command ls -l Option Applied

It was output in the following format:

-rw-r--r-- 1 user group 2048 Jan 1 12:34 file.txt


Each field represents:

  • File type and permissions (-rw-r–r–)
  • Number of links (1)
  • Owner (user)
  • Group (group)
  • Size (2048 bytes)
  • Last modification time (Jan 1 12:34)
  • File name (file.txt)

-a (all)

Displays all files and directories, including hidden files (those beginning with a dot).

ls -a
ShellScript

When used with the -l option, you can see that . (current directory) and .. (parent directory) are listed first, and hidden files like .hidden_file are also shown.

Figure 3. Linux Command ls -a Option Applied
Figure 3. Linux Command ls -a Option Applied

-h (human-readable)

Displays file sizes in a human-readable format, using units like KB, MB, and GB.

ls -lh
ShellScript

You can easily see that test and test2 are displayed in KB units, while the Anaconda file is displayed in MB units.

Figure 4. Linux Command ls -lh Option Applied
Figure 4. Linux Command ls -lh Option Applied

-t (sort by time)

Sorts files by their last modification time, with the newest files first.

ls -t
ShellScript

Below, you can check the time using the -l option together. You can see that the e.txt file was recently modified.

Figure 5. Linux Command ls -lt Option Applied
Figure 5. Linux Command ls -lt Option Applied

-r (reverse)

Reverses the order of the sort. When used with -t, the oldest files appear first.

ls -r
ShellScript

I tried using it with the -t option. You can see that a.txt is the oldest file and is at the top.

Figure 6. Linux Command ls -ltr Option Applied
Figure 6. Linux Command ls -ltr Option Applied

-R (recursive)

Displays files in the current directory and all its subdirectories recursively.

ls -R
ShellScript

It shows the contents of the current directory . and all the file listings in the subdirectories test and test2.

Figure 7. Linux Command ls -R Option Applied
Figure 7. Linux Command ls -R Option Applied

-d (directories)

Lists only directories.

ls -d */
ShellScript

You can check only the directories test and test2 as shown below.

Figure 8. Linux Command ls -d */ Option Applied
Figure 8. Linux Command ls -d */ Option Applied

-S (sort by file size)

Sorts files by size, with the largest files first.

ls -lS
ShellScript

As shown in the figure below, it is sorted from the largest file to the smallest file.

Figure 9. Linux Command ls -lS Option Applied
Figure 9. Linux Command ls -lS Option Applied

Practical Examples

Let’s explore some practical examples that you can actually use.

Using Wildcards

To see all files starting with ‘test’, you can use the wildcard * like ‘test*’. In this case, if the directory name matches, it will show all the files in that directory.

ls test*
ShellScript

It shows everything starting with test as follows. Since only directories match right now, it shows the files in those directories.

Figure 10. Linux Command Wildcard * Applied
Figure 10. Linux Command Wildcard * Applied

Displaying Hidden Files in Long Format

It outputs all files and directories in a long format, including hidden files.

ls -la
ShellScript

As shown below, the size of the file is displayed in bytes, and you can see detailed information, including hidden files.

Figure 11. Linux Command ls -la Applied
Figure 11. Linux Command ls -la Applied

Sorting and Displaying File Sizes

It displays the file sizes in a human-readable format and sorts them in reverse order from the oldest file to the newest.

ls -lhtr
ShellScript

When you run the command above, it shows the file list as intended, as shown below.

Figure 12. Linux Command ls -lhtr Applied
Figure 12. Linux Command ls -lhtr Applied

Sorting Files by Size in Reverse Order

If you want to check files from the smallest size to the largest size, you can use the -lS option with the -r option. And the sizes will be displayed in a human-readable format.

ls -lSrh
ShellScript

You can see that the file sizes are displayed as intended in a human-readable format, sorted by file size.

Figure 13. Linux Command ls -lSrh Applied
Figure 13. Linux Command ls -lSrh Applied

Reference

Leave a Comment