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”.
Table of Contents
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
ShellScriptThe above command will display all file names in the current directory as shown in the illustration.
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
ShellScriptThe output will look like this:
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
ShellScriptWhen 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.
-h (human-readable)
Displays file sizes in a human-readable format, using units like KB, MB, and GB.
ls -lh
ShellScriptYou can easily see that test and test2 are displayed in KB units, while the Anaconda file is displayed in MB units.
-t (sort by time)
Sorts files by their last modification time, with the newest files first.
ls -t
ShellScriptBelow, you can check the time using the -l option together. You can see that the e.txt file was recently modified.
-r (reverse)
Reverses the order of the sort. When used with -t, the oldest files appear first.
ls -r
ShellScriptI tried using it with the -t option. You can see that a.txt is the oldest file and is at the top.
-R (recursive)
Displays files in the current directory and all its subdirectories recursively.
ls -R
ShellScriptIt shows the contents of the current directory . and all the file listings in the subdirectories test and test2.
-d (directories)
Lists only directories.
ls -d */
ShellScriptYou can check only the directories test and test2 as shown below.
-S (sort by file size)
Sorts files by size, with the largest files first.
ls -lS
ShellScriptAs shown in the figure below, it is sorted from the largest file to the smallest file.
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*
ShellScriptIt shows everything starting with test as follows. Since only directories match right now, it shows the files in those directories.
Displaying Hidden Files in Long Format
It outputs all files and directories in a long format, including hidden files.
ls -la
ShellScriptAs shown below, the size of the file is displayed in bytes, and you can see detailed information, including hidden files.
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
ShellScriptWhen you run the command above, it shows the file list as intended, as shown below.
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
ShellScriptYou can see that the file sizes are displayed as intended in a human-readable format, sorted by file size.