Cyberithub

16 Best ls command examples in Linux

In this article, I will take you through the usage of ls command in Linux. ls command in Linux is a daily use command to list out the files and directories. From Linux Administrator to Linux Developer, everyone uses it to perform very basic listing operations. Hence it is important to understand the usage of ls command in Linux.

16 Best ls command examples in Linux 1

ls command in Linux

Before we look into ls command in Linux, let’s see what does ls stand for and what is ls. Then we will go through the different usages of ls command in Linux.

What does ls stand for?

ls stand for list directory contents.

What is ls?

ls command in Linux is used to list all the files and directories

NOTE:

Please note that I have used -lrt option with ls command very often to display the contents in a nice meaningful format with other options however you can use it the way you want. You don’t necessarily have to use it my way.

Also Read: Oracle IaaS Setup in 4 Easy Steps

1. Check Linux ls command version

To check the version of ls, use ls --version command.

[root@localhost ~]# ls --version
ls (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

2. Using default ls command

You can use ls command without any option to list out only files and directories in linux.

root@localhost:~# ls
dir1 dir2 dir3 file1 file2

3. List All Contents separated by comma sign

If you want to list all files and directories separated by comma sign, you can use ls -m command.

root@localhost:~# ls -m
dir1, dir2, dir3, file1, file2

-m: fill width with a comma separated list of entries

Also Read: How to Add User into Sudoers on Ubuntu

4. List All Contents in double quotes

If you want to list all contents in double quotes, you need to use ls -Q command.

root@localhost:~# ls -Q
"dir1" "dir2" "dir3" "file1" "file2"

-Q: enclose entry names in double quotes

NOTE:

Please note that I am running all the commands through root user. You can use any user with appropriate access to run all these commands.

5. List Contents by Modification Time

To list out contents by their modified time, use ls -lt command.

[root@localhost ~]# ls -lt
total 364936
drwxr-xr-x 2 root root 39 Dec 29 17:56 example
-rwxr-xr-x 1 root root 79 Dec 16 17:23 test.sh
-rw-r--r-- 1 root root 349 Nov 18 10:36 helm-rbac.yaml
-rw-r--r--. 1 root root 393 Nov 16 04:50 test.yaml
-rw-------. 1 root root 1261 Nov 16 01:45 anaconda-ks.cfg

-l: use a long listing format

-t : sort by modification time, newest first

6. Print in Long Listing format

To display the contents in long listing format, use ls -l command.

[root@localhost ~]# ls -l
total 364936
-rw-------. 1 root root 1261 Nov 16 01:45 anaconda-ks.cfg
-rw-r--r-- 1 root root 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
-rwxr-xr-x 1 root root 79 Dec 16 17:23 test.sh
-rw-r--r--. 1 root root 393 Nov 16 04:50 test.yaml

-l : use a long listing format

7. List all the files including the one starting with .

To display all the files including hidden files which starts with .(for e.g: .bashrc), you need to use ls -lrta command.

[root@localhost ~]# ls -lrta
total 364976
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
-rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
dr-xr-xr-x. 17 root root 224 Nov 16 01:44 ..
-rw-------. 1 root root 1261 Nov 16 01:45 anaconda-ks.cfg

-a : do not ignore entries starting with .

8. Print author of each file

To display the author of each of the files created, you need to use ls -l --author command.

[root@localhost ~]# ls -l --author
total 364936
-rw-------. 1 root root root 1261 Nov 16 01:45 anaconda-ks.cfg
-rw-r--r-- 1 root root root 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-r--r-- 1 root root root 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
drwxr-xr-x 2 root root root 39 Dec 29 17:56 example
-rw-r--r-- 1 root root root 40 Dec 16 14:23 example.pl

–author : with -l, print the author of each file

9. List Contents in Reverse Order

If you want to list all files and directories in reverse alphabetical order, then you need to use ls -lr command.

[root@localhost ~]# ls -lr
total 364936
-rw-r--r--. 1 root root 393 Nov 16 04:50 test.yaml
-rwxr-xr-x 1 root root 79 Dec 16 17:23 test.sh
drwxr-xr-x 2 root root 24 Dec 16 14:38 perl
-rw-r--r-- 1 root root 6428 Dec 12 19:08 my.active.firewall.rules
-rw-r--r-- 1 root root 349 Nov 18 10:36 helm-rbac.yaml
-rw-r--r-- 1 root root 40 Dec 16 14:23 example.pl
-rw-r--r-- 1 root root 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-------. 1 root root 1261 Nov 16 01:45 anaconda-ks.cfg

-r : reverse order while sorting

10. List in Human Readable Format

If you want to list all files and directories in human readable format, you need to use ls -lrth command.

[root@localhost ~]# ls -lrth
total 357M
-rw-------. 1 root root 1.3K Nov 16 01:45 anaconda-ks.cfg
-rw-r--r--. 1 root root 393 Nov 16 04:50 test.yaml
-rw-r--r-- 1 root root 349 Nov 18 10:36 helm-rbac.yaml
-rw-r--r-- 1 root root 29M Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 6.3K Dec 12 19:08 my.active.firewall.rules
-rw-r--r-- 1 root root 328M Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 40 Dec 16 14:23 example.pl
drwxr-xr-x 2 root root 24 Dec 16 14:38 perl
-rwxr-xr-x 1 root root 79 Dec 16 17:23 test.sh

-h: with -l, print sizes in human readable format (e.g., 1K 234M 2G)

11. List contents without Owner Details

To list out the contents without owner details, use ls -grt command.

[root@localhost ~]# ls -grt
total 364936
-rw-------. 1 root 1261 Nov 16 01:45 anaconda-ks.cfg
-rw-r--r--. 1 root 393 Nov 16 04:50 test.yaml
-rw-r--r-- 1 root 349 Nov 18 10:36 helm-rbac.yaml
-rw-r--r-- 1 root 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-r--r-- 1 root 6428 Dec 12 19:08 my.active.firewall.rules
-rw-r--r-- 1 root 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
-rw-r--r-- 1 root 40 Dec 16 14:23 example.pl
drwxr-xr-x 2 root 24 Dec 16 14:38 perl
-rwxr-xr-x 1 root 79 Dec 16 17:23 test.sh

-g: like -l, but do not list owner

12. List index number of each file

To list out index number of each files and directories, you need to use ls -li command.

[root@localhost ~]# ls -li
total 364936
8409154 -rw-------. 1 root root 1261 Nov 16 01:45 anaconda-ks.cfg
10072452 -rw-r--r-- 1 root root 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
10072453 -rw-r--r-- 1 root root 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
11519790 -rw-r--r-- 1 root root 40 Dec 16 14:23 example.pl
8409159 -rw-r--r-- 1 root root 349 Nov 18 10:36 helm-rbac.yaml

-i: print index number of each file

13. Sort ls Contents by File Size

To list out the contents by its size, you need to use ls -lrtS command.

[root@localhost ~]# ls -lrtS
total 364936
drwxr-xr-x 2 root root 24 Dec 16 14:38 perl
drwxr-xr-x 2 root root 39 Dec 29 17:56 example
-rw-------. 1 root root 1261 Nov 16 01:45 anaconda-ks.cfg
-rw-r--r-- 1 root root 6428 Dec 12 19:08 my.active.firewall.rules
-rw-r--r-- 1 root root 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm

-S: sort by file size

14. List Sub directories Recursively

To list out all the files and directories recursively, use ls -lRt command.

[root@localhost ~]# ls -lRt
.:
total 364936
drwxr-xr-x 2 root root 39 Dec 29 17:56 example
-rwxr-xr-x 1 root root 79 Dec 16 17:23 test.sh
drwxr-xr-x 2 root root 24 Dec 16 14:38 perl

./example:
total 8
-rw-r--r-- 1 root root 61 Dec 29 17:59 file2.txt
-rw-r--r-- 1 root root 61 Dec 29 17:59 file.txt

./perl:
total 4
-rw-r--r-- 1 root root 206 Dec 16 14:38 example.pl

-R: list subdirectories recursively

15. Print User ID and Group ID of all the Files

To display the user id and group id of all the files and directories, use ls -n command.

[root@localhost ~]# ls -n
total 364936
-rw-------. 1 0 0 1261 Nov 16 01:45 anaconda-ks.cfg
-rw-r--r-- 1 0 0 30147119 Dec 9 20:36 chef-15.6.10-1.el7.x86_64.rpm
-rw-r--r-- 1 0 0 343512169 Dec 14 05:38 chef-server-core-13.1.13-1.el7.x86_64.rpm
drwxr-xr-x 2 0 0 39 Dec 29 17:56 example
drwxr-xr-x 2 0 0 24 Dec 16 14:38 perl
-rwxr-xr-x 1 0 0 79 Dec 16 17:23 test.sh
-rw-r--r--. 1 0 0 393 Nov 16 04:50 test.yaml

-n: like -l, but list numeric user and group IDs

16. Check Aliases of ls command

To check all the aliases set for ls command in your system, use alias ls command.

[root@localhost ~]# alias ls
alias ls='ls --color=auto'

 

Popular Searches

  • ls sort by date
  • ls sort by size
  • ls recursive
  • linux list directories
  • ls meaning
  • what does ls stand for
  • linux ls sort by date

References: Shell Scripting in Linux

Leave a Comment