5 Quirky ‘ls’ Command Tricks Every Linux User Should Know


If you are a Linux user, you are probably familiar with "ls" command, which is used to list contents of a directory. However, did you know that there are several quirky and useful tricks that you can use with "ls" command? In this article, we will explore five of these tricks that every Linux user should know.

Displaying File Size in Human-Readable Format

By default, "ls" command displays file size in bytes. However, this can be difficult to read, especially for larger files. Fortunately, you can use "-h" option to display file size in a more human-readable format. For example −

$ ls -lh
total 4.0K
-rw-r--r-- 1 user user 1.1M Mar 23 10:05 file1.txt
-rw-r--r-- 1 user user  54K Mar 23 10:05 file2.txt

In output above, file sizes are displayed in a more readable format, with units such as "K" (kilobytes) and "M" (megabytes).

Displaying File Type with Colorful Output

Another useful trick is to display file type with colorful output. By default, "ls" command does not distinguish between different types of files. However, you can use "--color" option to display different types of files with different colors. For example −

$ ls --color
file1.txt  file2.txt  folder1/

In output above, regular files are displayed in white, while directory is displayed in blue. This can make it easier to identify different types of files at a glance.

Sorting Output by File Size

If you want to sort output of "ls" command by file size, you can use "-S" option. This will sort files in descending order, with largest files listed first. For example −

$ ls -lhS
total 4.0K
-rw-r--r-- 1 user user 1.1M Mar 23 10:05 file1.txt
-rw-r--r-- 1 user user  54K Mar 23 10:05 file2.txt

In output above, files are sorted by size, with largest file (file1.txt) listed first.

Displaying Hidden Files

By default, "ls" command does not display hidden files (files that begin with a dot). However, you can use "-a" option to display hidden files as well. For example −

$ ls -a
file1.txt  file2.txt  ..  .hidden_file

In output above, hidden file ".hidden_file" is displayed along with other files. This can be useful if you need to work with hidden files or want to see all files in a directory, including hidden ones.

Using Wildcards for Advanced Searching

Finally, you can use wildcards to perform advanced searching with "ls" command. For example, if you want to list all files in a directory that begin with letter "a", you can use following command −

$ ls a*

This will list all files in directory that begin with letter "a". You can also use other wildcards, such as "*" (to match any character) and "?" (to match a single character). For example, to list all files that end with ".txt", you can use following command −

$ ls *.txt
file1.txt  file2.txt

In output above, only files that end with ".txt" are listed.

Displaying Only Directories

If you want to list only directories in a directory, you can use "-d" option. For example −

$ ls -d */

In output above, only directories are listed, with "/" character indicating that they are directories.

Displaying File Permissions

By default, "ls" command displays file permissions in a cryptic format, such as "rw-r--r--". However, you can use "-l" option to display file permissions in a more human-readable format. For example −

$ ls -l
-rw-r--r-- 1 user user 1.1M Mar 23 10:05 file1.txt
-rw-r--r-- 1 user user  54K Mar 23 10:05 file2.txt

In output above, file permissions are displayed as "rw-r--r--", where "r" represents read permission, "w" represents write permission, and "-" represents no permission.

Displaying Last Modified Time

If you want to see when a file was last modified, you can use "-t" option to display files sorted by modification time. For example −

$ ls -lt
-rw-r--r-- 1 user user 1.1M Mar 23 10:05 file1.txt
-rw-r--r-- 1 user user  54K Mar 23 10:05 file2.txt

In output above, files are listed in order of modification time, with most recently modified file listed first.

Using Long Listing Format

If you want to see more detailed information about files in a directory, you can use "-l" option to display long listing format. For example −

$ ls -l
-rw-r--r-- 1 user user 1.1M Mar 23 10:05 file1.txt
-rw-r--r-- 1 user user  54K Mar 23 10:05 file2.txt

In output above, long listing format displays information such as file permissions, owner, group, size, and modification time.

Using Reverse Order

If you want to list files in reverse order, you can use "-r" option. For example −

$ ls -r
file2.txt  file1.txt

In output above, files are listed in reverse order, with "file2.txt" listed first.

Conclusion

The "ls" command is one of most basic and essential commands in Linux. However, as we have seen, there are several quirky and useful tricks that you can use with this command. By using these tricks, you can make your Linux experience more efficient and enjoyable. Try them out and see how they work for you!

Updated on: 11-Apr-2023

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements