Exploring the 'ls' Command in Linux: Options and Examples
Summary
The "ls" command in Linux is one of the most commonly used commands for listing the contents of directories
Detailed information about files and directories
The "ls" command provides a wealth of options to customize the output and gather more detailed information about files and directories. In this article, we’ll explore several commonly used options for the ls command, complete with examples to help you better understand their usage.
Basic Syntax
The basic syntax for the "ls" command is the following (If no directory is specified, "ls" lists the contents of the current working directory.)
ls [options] [directory]
1. Listing Files and Directories
By default, the ls command lists files and directories in a simple format. The following example shows the results after executing the "ls" command in a WordPress folder installation
$ ls
index.php wp-comments-post.php wp-load.php
license.txt wp-config-sample.php wp-login.php
readme.html wp-config.php wp-mail.php
wp-content/ wp-settings.php
wp-activate.php wp-cron.php wp-signup.php
wp-admin/ wp-includes/ wp-trackback.php
wp-blog-header.php wp-links-opml.php xmlrpc.php
2. Displaying Hidden Files
Hidden files in Linux start with a "." and are not displayed by default. Use the "-a" option to include hidden files in the output:
$ ls -a
./ wp-config.php wp-login.php
../ wp-activate.php wp-content/ wp-mail.php
.htaccess wp-admin/ wp-cron.php wp-settings.php
index.php wp-blog-header.php wp-includes/ wp-signup.php
license.txt wp-comments-post.php wp-links-opml.php wp-trackback.php
readme.html wp-config-sample.php wp-load.php xmlrpc.php
3. Long Format Listing
The "-l" option displays detailed information about files and directories, including permissions, ownership, size, and modification date:
$ ls -l
total 350
-rw-r--r-- 1 DELL 197121 405 Jan 3 09:00 index.php
-rw-r--r-- 1 DELL 197121 19915 Jan 3 09:00 license.txt
-rw-r--r-- 1 DELL 197121 7409 Jan 3 09:00 readme.html
-rw-r--r-- 1 DELL 197121 7387 Jan 3 09:00 wp-activate.php
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:06 wp-admin/
-rw-r--r-- 1 DELL 197121 351 Jan 3 09:00 wp-blog-header.php
-rw-r--r-- 1 DELL 197121 2323 Jan 3 09:00 wp-comments-post.php
-rw-r--r-- 1 DELL 197121 3336 Jan 3 09:00 wp-config-sample.php
-rw-r--r-- 1 DELL 197121 3597 Jan 3 09:09 wp-config.php
....
4. Sorting by Modification Time
To sort files by their last modification time, use the "-t" option:
$ ls -lt
total 350
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:10 wp-content/
-rw-r--r-- 1 DELL 197121 3597 Jan 3 09:09 wp-config.php
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:07 wp-includes/
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:06 wp-admin/
-rw-r--r-- 1 DELL 197121 2323 Jan 3 09:00 wp-comments-post.php
-rw-r--r-- 1 DELL 197121 5102 Jan 3 09:00 wp-trackback.php
-rw-r--r-- 1 DELL 197121 7387 Jan 3 09:00 wp-activate.php
-rw-r--r-- 1 DELL 197121 3937 Jan 3 09:00 wp-load.php
-rw-r--r-- 1 DELL 197121 2502 Jan 3 09:00 wp-links-opml.php
-rw-r--r-- 1 DELL 197121 8543 Jan 3 09:00 wp-mail.php
-rw-r--r-- 1 DELL 197121 19915 Jan 3 09:00 license.txt
-rw-r--r-- 1 DELL 197121 29032 Jan 3 09:00 wp-settings.php
-rw-r--r-- 1 DELL 197121 51367 Jan 3 09:00 wp-login.php
-rw-r--r-- 1 DELL 197121 3336 Jan 3 09:00 wp-config-sample.php
....
5. Recursively Listing Subdirectories
The "-R" option allows you to list all files and directories recursively. (Find below a sample of the generated output of the command)
$ ls -R wp-content
wp-content:
index.php plugins/ themes/ uploads/
wp-content/plugins:
akismet/ hello.php index.php
wp-content/plugins/akismet:
LICENSE.txt class.akismet-admin.php class.akismet.php wrapper.php
_inc/ class.akismet-cli.php index.php
akismet.php class.akismet-rest-api.php readme.txt
changelog.txt class.akismet-widget.php views/
....
6. Human-Readable File Sizes
When combined with the -l option, the -h option displays file sizes in a human-readable format (e.g., KB, MB):
$ ls -lh
total 350K
-rw-r--r-- 1 DELL 197121 405 Jan 3 09:00 index.php
-rw-r--r-- 1 DELL 197121 20K Jan 3 09:00 license.txt
-rw-r--r-- 1 DELL 197121 7.3K Jan 3 09:00 readme.html
-rw-r--r-- 1 DELL 197121 7.3K Jan 3 09:00 wp-activate.php
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:06 wp-admin/
-rw-r--r-- 1 DELL 197121 351 Jan 3 09:00 wp-blog-header.php
-rw-r--r-- 1 DELL 197121 2.3K Jan 3 09:00 wp-comments-post.php
-rw-r--r-- 1 DELL 197121 3.3K Jan 3 09:00 wp-config-sample.php
-rw-r--r-- 1 DELL 197121 3.6K Jan 3 09:09 wp-config.php
...
7. Combining Options
You can combine multiple options for more detailed output. For instance, to display hidden files in a long format with human-readable file sizes, please refer to the following command.
$ ls -alh
total 363K
drwxr-xr-x 1 DELL 197121 0 Jan 7 17:32 ./
drwxr-xr-x 1 DELL 197121 0 Jan 3 08:59 ../
-rw-r--r-- 1 DELL 197121 529 Jan 3 09:10 .htaccess
-rw-r--r-- 1 DELL 197121 405 Jan 3 09:00 index.php
-rw-r--r-- 1 DELL 197121 20K Jan 3 09:00 license.txt
-rw-r--r-- 1 DELL 197121 7.3K Jan 3 09:00 readme.html
-rw-r--r-- 1 DELL 197121 7.3K Jan 3 09:00 wp-activate.php
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:06 wp-admin/
-rw-r--r-- 1 DELL 197121 351 Jan 3 09:00 wp-blog-header.php
-rw-r--r-- 1 DELL 197121 2.3K Jan 3 09:00 wp-comments-post.php
-rw-r--r-- 1 DELL 197121 3.3K Jan 3 09:00 wp-config-sample.php
...
8. Classifying File Types
The "-F" option appends a character to each file name indicating its type:
- / for directories
- * for executables
- @ for symbolic links
$ ls -F
index.php wp-blog-header.php wp-cron.php wp-mail.php
license.txt wp-comments-post.php wp-includes/ wp-settings.php
readme.html wp-config-sample.php wp-links-opml.php wp-signup.php
wp-activate.php wp-config.php wp-load.php wp-trackback.php
wp-admin/ wp-content/ wp-login.php xmlrpc.php
9. Reverse Sorting
To reverse the order of the output, use the "-r" option.
$ ls -lr
total 350
-rw-r--r-- 1 DELL 197121 3246 Jan 3 09:00 xmlrpc.php
-rw-r--r-- 1 DELL 197121 5102 Jan 3 09:00 wp-trackback.php
-rw-r--r-- 1 DELL 197121 34385 Jan 3 09:00 wp-signup.php
-rw-r--r-- 1 DELL 197121 29032 Jan 3 09:00 wp-settings.php
-rw-r--r-- 1 DELL 197121 8543 Jan 3 09:00 wp-mail.php
-rw-r--r-- 1 DELL 197121 51367 Jan 3 09:00 wp-login.php
-rw-r--r-- 1 DELL 197121 3937 Jan 3 09:00 wp-load.php
-rw-r--r-- 1 DELL 197121 2502 Jan 3 09:00 wp-links-opml.php
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:07 wp-includes/
-rw-r--r-- 1 DELL 197121 5617 Jan 3 09:00 wp-cron.php
drwxr-xr-x 1 DELL 197121 0 Jan 3 09:10 wp-content/
...
10. Listing Files by Size
The "-S" option sorts files by size (largest to smallest)
$ ls -lS
total 350
-rw-r--r-- 1 DELL 197121 51367 Jan 3 09:00 wp-login.php
-rw-r--r-- 1 DELL 197121 34385 Jan 3 09:00 wp-signup.php
-rw-r--r-- 1 DELL 197121 29032 Jan 3 09:00 wp-settings.php
-rw-r--r-- 1 DELL 197121 19915 Jan 3 09:00 license.txt
-rw-r--r-- 1 DELL 197121 8543 Jan 3 09:00 wp-mail.php
-rw-r--r-- 1 DELL 197121 7409 Jan 3 09:00 readme.html
-rw-r--r-- 1 DELL 197121 7387 Jan 3 09:00 wp-activate.php
-rw-r--r-- 1 DELL 197121 5617 Jan 3 09:00 wp-cron.php
-rw-r--r-- 1 DELL 197121 5102 Jan 3 09:00 wp-trackback.php
-rw-r--r-- 1 DELL 197121 3937 Jan 3 09:00 wp-load.php
-rw-r--r-- 1 DELL 197121 3597 Jan 3 09:09 wp-config.php
An essential tool
The "ls" command is an essential tool for navigating and managing files in Linux. By mastering its various options, you can tailor its output to suit your specific needs and make file management more efficient. Experiment with these options to become more proficient in using this versatile command.