Table of Contents
- Introduction
- How to use ls command with long listing format (-l)
- How to use ls command with ” list one file per line ” option ( -1)
- How to use ls command to list all the hidden files and directories
- How to use ls command with human readable option (-h)
- How to sort listed files with Ls command
- Ls command with -U option ( not to sort by default way)
- Ls command with -t option (sort based on modification time )
- Ls command with the -r option (reverse the results with other sorting options)
- Ls command with -S option (sort based on size )
- Ls command with -X option (sorts Alphabetically by extension )
- How to use Ls command with option -R for recursive listing
- How to use Ls command with Option -s for printing allocation size
- How to use Ls command with option -i to print inode index
Introduction
Ls command helps with the listing of files and directories present under the current working directory
For an example , I used the ls command to see the files present under the home directory of the Root user
[root@discoveringsystems-centos ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg
In this article we will look into the different options available with the ls command utility and their use cases
To view all the options available with the ls command , we can use the –help option
[root@discoveringsystems-centos ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
.
.
.
[ output trimmed for brevity ]
How to use ls command with long listing format (-l)
In the following example , we changed the display of the list of files to long listing format using the option (-l) along with the ls command
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
How to use ls command with “ list one file per line” option ( -1)
In the following example , I was able to list the files under the present working directory in a format similar to long listing format , but without the file permission details , author, time info and file size , etc
[root@discoveringsystems-centos ~]# ls -1
anaconda-ks.cfg
B-file.txt
C-file.txt
D-file.txt
file12.txt
file4.txt
initial-setup-ks.cfg
set-of-files
How to use ls command to list all the hidden files and directories along with other normal files / directories
When we use the -A option
, we are listing all others and except the implied . and ..
[root@discoveringsystems-centos ~]# ls -Al
total 52
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-------. 1 root root 23819 Nov 10 18:16 .bash_history
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc
drwx------. 4 root root 31 Jul 5 02:37 .cache
drwx------. 4 root root 30 Jul 5 02:37 .config
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
drwx------. 3 root root 25 Jul 5 02:32 .dbus
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwx------. 2 root root 25 Jul 13 00:39 .ssh
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
drwxr-xr-x. 2 root root 41 Jul 20 00:21 .wireshark
When we use the option -a
we are listing all the files and directories in the present working directory and not ignoring entries starting with .
[root@discoveringsystems-centos ~]# ls -al
total 52
dr-xr-x---. 7 root root 235 Jul 20 00:21 .
dr-xr-xr-x. 19 root root 258 Nov 7 11:08 ..
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-------. 1 root root 23819 Nov 10 18:16 .bash_history
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc
drwx------. 4 root root 31 Jul 5 02:37 .cache
drwx------. 4 root root 30 Jul 5 02:37 .config
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
drwx------. 3 root root 25 Jul 5 02:32 .dbus
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwx------. 2 root root 25 Jul 13 00:39 .ssh
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
drwxr-xr-x. 2 root root 41 Jul 20 00:21 .wireshark
How to use ls command with human readable option (-h)
Using the “ human readable format ” option(-h) along with the ls command we display the size field in Kilobyte , Megabyte, Gigabyte instead of just bytes. In the example below we have the comparison of both normal format and human readable format
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
[root@discoveringsystems-centos ~]# ls -lh
total 8.0K
-rw-------. 1 root root 1.7K Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K Jul 5 02:33 initial-setup-ks.cfg
How to sort listed files with Ls command
By default ls command sorts the files/ directories in the alphabetical order
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
[root@discoveringsystems-centos ~]#
[root@discoveringsystems-centos ~]# touch B-file.txt
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
Ls command with -U option ( not to sort by default way)
Using option -U along with the ls command we are asking is not sort, but list entries in directory order. Following example shows the comparison of the sorted and not sorted lists
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file12.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file4.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
[root@discoveringsystems-centos ~]# ls -lU
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
-rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file4.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file12.txt
Ls command with -t option (sort based on modification time )
Using the -t option , we can sort the files and directories based on the modification time , newest first
[root@discoveringsystems-centos ~]# ls -tl
total 8
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
Ls command with the -r option (reverse the results with other sorting options)
After sorting with the -t option , if we would like to reverse the results we can use the -r option along with that. In the following example , with combination of the both -t and -r we can see the oldest shows up first in the list and the newest shows in the bottom . we can also use -r option individually without the -t option
[root@discoveringsystems-centos ~]# ls -tlr
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
Ls command with -S option (sort based on size )
Using -S option along with the ls command we can sort the files and directories based on the file size. Largest shows up first in the list.
[root@discoveringsystems-centos ~]# ls -Sl
total 8
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
Ls command with -X option (sorts Alphabetically by extension )
Using option -X , we can sort by the extension name ( sort alphabetically by extension ) instead of the name of the file to sort the files and directories. Following example shows the comparison of the default alphabetical sort based on the filenames vs the sort based on the extension names
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
[root@discoveringsystems-centos ~]# ls -lX
total 8
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
How to use Ls command with option -R for recursive listing
Using option -R along with the ls command , we can recursively list the files/ directories under present working directory
[root@discoveringsystems-centos ~]# ls -lR
.:
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
./set-of-files:
total 0
-rw-r--r--. 1 root root 0 Dec 7 11:18 file1.txt
-rw-r--r--. 1 root root 0 Dec 7 11:18 file2.txt
How to use Ls command with Option -s for printing allocation size
Using option -s along with ls command we can print allocation size in blocks while listing the files and directories. Following is the example comparison of the normal format vs printed along with allocation size in blocks format
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file12.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file4.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
[root@discoveringsystems-centos ~]# ls -sl
total 8
4 -rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
0 -rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
0 -rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
0 -rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
0 -rw-r--r--. 1 root root 0 Dec 7 11:45 file12.txt
0 -rw-r--r--. 1 root root 0 Dec 7 11:45 file4.txt
4 -rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
0 drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
How to use Ls command with option -i to print inode index
Using option -i along with the ls command , we can print the index number of each file ( inode) . Following is an example comparison of normal format vs inode format
[root@discoveringsystems-centos ~]# ls -l
total 8
-rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file12.txt
-rw-r--r--. 1 root root 0 Dec 7 11:45 file4.txt
-rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files
[root@discoveringsystems-centos ~]# ls -li
total 8
33582159 -rw-------. 1 root root 1721 Jul 4 19:30 anaconda-ks.cfg
35019834 -rw-r--r--. 1 root root 0 Dec 5 11:47 B-file.txt
33902906 -rw-r--r--. 1 root root 0 Dec 7 11:25 C-file.txt
35019819 -rw-r--r--. 1 root root 0 Dec 7 11:25 D-file.txt
35019824 -rw-r--r--. 1 root root 0 Dec 7 11:45 file12.txt
33732816 -rw-r--r--. 1 root root 0 Dec 7 11:45 file4.txt
35019839 -rw-r--r--. 1 root root 1769 Jul 5 02:33 initial-setup-ks.cfg
33582164 drwxr-xr-x. 2 root root 40 Dec 7 11:18 set-of-files