Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
5 Quirky ‘ls’ Command Tricks Every Linux User Should Know
If you are a Linux user, you are probably familiar with the 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 the ls command? In this article, we will explore five powerful techniques that every Linux user should know.
Displaying File Size in Human-Readable Format
By default, the ls command displays file size in bytes. However, this can be difficult to read, especially for larger files. Fortunately, you can use the -h option to display file size in a more human-readable format
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 the 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 types with colorful output. By default, the ls command does not distinguish between different types of files. However, you can use the --color option to display different types of files with different colors
ls --color=auto
file1.txt file2.txt folder1/
In the output above, regular files are displayed in white, while directories are displayed in blue. This makes it easier to identify different file types at a glance.
Sorting Output by File Size
If you want to sort the output of the ls command by file size, you can use the -S option. This will sort files in descending order, with the largest files listed first
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 the output above, files are sorted by size, with the largest file (file1.txt) listed first.
Displaying Hidden Files and Advanced Listing
By default, the ls command does not display hidden files (files that begin with a dot). However, you can use the -a option to display all files, including hidden ones
ls -la
total 12K drwxr-xr-x 2 user user 4.0K Mar 23 10:05 . drwxr-xr-x 3 user user 4.0K Mar 23 10:04 .. -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 -rw-r--r-- 1 user user 12 Mar 23 10:03 .hidden_file
The -l option provides detailed information including file permissions, owner, group, size, and modification time.
Using Wildcards for Advanced Pattern Matching
You can use wildcards to perform advanced searching with the ls command. This is particularly useful for filtering files based on specific patterns
List Files Starting with Specific Letter
ls a*
List Files with Specific Extension
ls *.txt
file1.txt file2.txt
List Only Directories
ls -d */
List Files by Modification Time
ls -lt
This displays files sorted by modification time, with the most recently modified files listed first.
Comparison of Useful ls Options
| Option | Description | Example Usage |
|---|---|---|
-h |
Human-readable file sizes | ls -lh |
-a |
Show all files (including hidden) | ls -la |
-S |
Sort by file size (largest first) | ls -lhS |
-t |
Sort by modification time | ls -lt |
-r |
Reverse sort order | ls -lr |
--color |
Colorize output by file type | ls --color=auto |
Conclusion
The ls command is one of the most fundamental tools in Linux, but these advanced techniques can significantly enhance your productivity. By mastering these tricks from human-readable file sizes to pattern matching with wildcards you can navigate and analyze directory contents more efficiently than ever before.
