Practical examples of using ls command

As for Linux System Administration, the command ls should be the one   that we use most, but how much do you know about this command?

ls has more than 50 optional parameters on common linux distributions, and we only need one or two of them in most cases. With some understandings of the rest of these parameters, we could get convenience on daily operations. Here I would show some practical examples of ls for Linux system administrators.

Example 1: List only directories

root@admon:/var/www/www.admon.org# ls -d */
papers/  punycode/  surveys/  timezone/  tools/  tuiguang/  wp-admin/  wp-content/  wp-includes/

We can use this one as well, which I use most. :p

# ls -F | grep /$
papers/
punycode/
surveys/
timezone/
tools/
tuiguang/
wp-admin/
wp-content/
wp-includes/

Example 2: Sort files by size
Parameter -S is for this,

# ls -Sl
total 312
-rw-r--r--  1 www-data www-data 65025 Feb  2 16:02 sitemap.xml
-rw-r--r--  1 www-data www-data 31909 Dec 16 04:48 wp-login.php
-rw-r--r--  1 www-data www-data 25665 Dec 16 04:48 wp-signup.php
-rw-r--r--  1 www-data www-data 19929 Oct 26 13:30 license.txt
-rw-r--r--  1 www-data www-data 10880 Dec 16 04:48 wp-settings.php
-rw-r--r--  1 www-data www-data  9184 Feb  2 16:02 sitemap.xml.gz

Example 3: Sort files by modified time
The newly modified files come first.

root@admon:/var/www/www.admon.org# ls -tl
total 312
-rw-r--r--  1 www-data www-data 65025 Feb  2 16:02 sitemap.xml
-rw-r--r--  1 www-data www-data  9184 Feb  2 16:02 sitemap.xml.gz
drwxr-xr-x  7 www-data www-data  4096 Jan 29 00:02 wp-content
drwxrwxr-x  3 www-data www-data  4096 Jan 28 07:59 papers
-rw-r--r--  1 www-data www-data  7185 Jan 24 05:27 readme.html
-rw-r--r--  1 www-data www-data  3087 Dec 16 04:48 wp-config-sample.php
-rw-r--r--  1 www-data www-data 31909 Dec 16 04:48 wp-login.php

Example 4: Sort by file extension
This sorts files by file extension.

#ls -lX
drwxr-xr-x  9 www-data www-data  4096 Jan 28  2013 wp-admin
drwxr-xr-x  7 www-data www-data  4096 Jan 29 00:02 wp-content
drwxr-xr-x 12 www-data www-data  4096 Dec 16 04:48 wp-includes
-rw-r--r--  1 www-data www-data  9184 Feb  2 16:02 sitemap.xml.gz
-rw-r--r--  1 www-data www-data  7185 Jan 24 05:27 readme.html
-rw-r--r--  1 www-data www-data   418 Oct 26 13:30 index.php
-rw-r--r--  1 www-data www-data  4892 Oct 26 13:30 wp-activate.php

Example 5: Reverse order while sorting
You can use -r while doing sorting.

Example 6: List inode for each files
When you have the inode of a file, it gives you more ability to operate it. E.g. you can delete a file by its inode.

# ls -i sitemap*
269338 sitemap.xml  268897 sitemap.xml.gz

Example 7: Show modified time in full time mode
We have two commands for this too.

root@admon:/var/www/www.admon.org# ls --full-time sitemap.xml*
-rw-r--r-- 1 www-data www-data 65025 2014-02-02 16:02:28.505716194 +0000 sitemap.xml
-rw-r--r-- 1 www-data www-data  9184 2014-02-02 16:02:28.505716194 +0000 sitemap.xml.gz
root@admon:/var/www/www.admon.org# ls -l --time-style=full-iso sitemap.xml*
-rw-r--r-- 1 www-data www-data 65025 2014-02-02 16:02:28.505716194 +0000 sitemap.xml
-rw-r--r-- 1 www-data www-data  9184 2014-02-02 16:02:28.505716194 +0000 sitemap.xml.gz

Example 8: Run ls without alias
Sometimes your ls command may be aliased, you can run ls command without any additional parameters like below. Any of the commands works.

# \ls
# /bin/ls

If you have any other fancy use, please leave a comment below.

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *