ls command with 25 practical examples – RHEL7
ls command is used to list
information about the FILEs (the current directory by default) with 25 practical examples
Syntax : ls
[options] arguments
1. Listing current directory content
we can list out all files and
directories in current path using ‘ls’ without hidden directories
[root@TechTutorial ~]# ls
a anaconda-ks.cfg
arkitsample.txt ARKIT.txt b c dir1 dir2 initial-setup-ks.cfg
techtutorial
2. Listing including hidden files
default ls command will not
list hidden objects, in order to see the hidden objects have to use option ‘-a’ ls command with 25 practical examples
[root@TechTutorial ~]# ls -a
. anaconda-ks.cfg
b
.bash_profile .cache .dbus initial-setup-ks.cfg
techtutorial
.. arkitsample.txt
.bash_history .bashrc
.config dir1
.lesshst
.viminfo
a
ARKIT.txt .bash_logout
c
.cshrc dir2 .tcshrc
3. Long list ( it display detailed info)
when you use ‘ls’ command it
will list files & directories without there permission, owner, group and
other details, ‘-l’ option will display owner, group, others, Size and time.
below is the example
[root@TechTutorial ~]# ls -l
total 24
-rw-r--r--. 1 root
root 0 Jan 11 15:47 a
-rw-------. 1 root ravi 1765
Jan 8 22:47 anaconda-ks.cfg
-rw-r--r--. 1 root
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 0 Jan 11 15:47 b
-rw-r--r--. 1 root
root 0 Jan 11 15:47 c
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 dir1
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 root ravi 1813
Jan 8 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5362
Jan 10 17:44 techtutorial
4. List files & directories separated by comma
if there is a requirement that
we have to list all the files and directories separated by comma
here is the example ls command with 25 practical examples
[root@TechTutorial ~]# ls -m
a, anaconda-ks.cfg,
arkitsample.txt, ARKIT.txt, b, c, dir1, dir2, initial-setup-ks.cfg, techtutorial
5. List remote directory files & directories with & without color
in order to list remote
directory content with color and without color below is the example
with color
[root@TechTutorial ~]# ls
--color=always
a anaconda-ks.cfg
arkitsample.txt ARKIT.txt b c dir1 dir2
initial-setup-ks.cfg techtutorial
without color, here all the
files and directories will display in block color
[root@TechTutorial ~]# ls
--color=never
a anaconda-ks.cfg
arkitsample.txt ARKIT.txt b c dir1 dir2
initial-setup-ks.cfg techtutorial
6. List only directory using option ‘-d’
ls command along with option
‘-d’ will display only directory path without its content
[root@TechTutorial ~]# ls -d
/etc/
/etc/
7. List files & directories detailed time stamp
as we can see above -l option
which will give long output, time stamp is not detailed, by following below
example it will give time stamp details including time zone and seconds
[root@TechTutorial ~]# ls -l
--time-style=full-iso
total 24
-rw-r--r--. 1 root
root 0 2016-01-11 15:47:47.042849437 +0530 a
-rw-------. 1 root ravi 1765
2016-01-08 22:47:13.626999586 +0530 anaconda-ks.cfg
-rw-r--r--. 1 root
root 2 2016-01-10 17:45:26.794985205 +0530 arkitsample.txt
-rw-r--r--. 1 root
root 3 2016-01-10 17:45:34.069985006 +0530 ARKIT.txt
-rw-r--r--. 1 root
root 0 2016-01-11 15:47:47.043849437 +0530 b
-rw-r--r--. 1 root
root 0 2016-01-11 15:47:47.043849437 +0530 c
drwxr-xr-x. 3 root
root 29 2016-01-10 17:20:06.963026703 +0530 dir1
drwxr-xr-x. 2 root
root 6 2016-01-10 17:05:21.261050887 +0530 dir2
-rw-r--r--. 1 root ravi 1813
2016-01-08 23:05:38.464011991 +0530 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5362
2016-01-10 17:44:46.245986312 +0530 techtutorial
8. Display files and directories its time stamp as “YY-MM-DD HH:MM”
No need to think a lot to list
files and directories with above time stamp format below is the example
[root@TechTutorial ~]# ls -l
--time-style long-iso
total 24
-rw-r--r--. 1 root
root 0 2016-01-11 15:47 a
-rw-------. 1 root ravi 1765
2016-01-08 22:47 anaconda-ks.cfg
-rw-r--r--. 1 root
root 2 2016-01-10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 3 2016-01-10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 0 2016-01-11 15:47 b
-rw-r--r--. 1 root
root 0 2016-01-11 15:47 c
drwxr-xr-x. 3 root
root 29 2016-01-10 17:20 dir1
drwxr-xr-x. 2 root
root 6 2016-01-10 17:05 dir2
-rw-r--r--. 1 root ravi 1813
2016-01-08 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5362
2016-01-10 17:44 techtutorial
9. List objects its time stamp as “MM-DD HH:MM”
List all files &
directories with date format YY-MM-DD HH:MM
[root@TechTutorial ~]# ls -l
--time-style iso
total 24
-rw-r--r--. 1 root
root 0 01-11 15:47 a
-rw-------. 1 root ravi 1765
01-08 22:47 anaconda-ks.cfg
-rw-r--r--. 1 root
root 2 01-10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 3 01-10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 0 01-11 15:47 b
-rw-r--r--. 1 root root
0 01-11 15:47 c
drwxr-xr-x. 3 root
root 29 01-10 17:20 dir1
drwxr-xr-x. 2 root
root 6 01-10 17:05 dir2
-rw-r--r--. 1 root ravi 1813
01-08 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5362
01-10 17:44 techtutorial
10. List files & directories with specified time stamp format
Here in this option we can
mention date command options to customize time stamp as required
below is the example
[root@TechTutorial ~]# ls -l
--time-style="+%Y-%m-%d %H:%M:%S"
total 24
-rw-r--r--. 1 root
root 0 2016-01-11 15:47:47 a
-rw-------. 1 root ravi 1765
2016-01-08 22:47:13 anaconda-ks.cfg
-rw-r--r--. 1 root
root 2 2016-01-10 17:45:26 arkitsample.txt
-rw-r--r--. 1 root
root 3 2016-01-10 17:45:34 ARKIT.txt
-rw-r--r--. 1 root
root 0 2016-01-11 15:47:47 b
-rw-r--r--. 1 root
root 0 2016-01-11 15:47:47 c
drwxr-xr-x. 3 root
root 29 2016-01-10 17:20:06 dir1
drwxr-xr-x. 2 root
root 6 2016-01-10 17:05:21 dir2
-rw-r--r--. 1 root ravi 1813
2016-01-08 23:05:38 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5362
2016-01-10 17:44:46 techtutorial
11. List Only Group Name without User name (Owner)
ls command will list only group
name without user name (Owner name) in long list lets see the example
[root@TechTutorial ~]# ls -lg
total 24
-rw-r--r--. 1
root 0 Jan 11 15:47 a
-rw-------. 1 ravi 1765
Jan 8 22:47 anaconda-ks.cfg
-rw-r--r--. 1
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1
root 0 Jan 11 15:47 b
-rw-r--r--. 1
root 0 Jan 11 15:47 c
drwxr-xr-x. 3 root
29 Jan 10 17:20 dir1
drwxr-xr-x. 2
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 ravi 1813
Jan 8 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root 5362 Jan 10
17:44 techtutorial
12. List Only Owner Name without Group Name
ls command will list only user
name (owner name) excluding group details we have to use ‘-G’ option along with
option ‘-l’
[root@TechTutorial ~]# ls -lG
total 24
-rw-r--r--. 1
root 0 Jan 11 15:47 a
-rw-------. 1 root 1765
Jan 8 22:47 anaconda-ks.cfg
-rw-r--r--. 1
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1
root 0 Jan 11 15:47 b
-rw-r--r--. 1
root 0 Jan 11 15:47 c
drwxr-xr-x. 3 root
29 Jan 10 17:20 dir1
drwxr-xr-x. 2
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 root 1813 Jan
8 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root 5362 Jan 10
17:44 techtutorial
13. List files & directories in human readable format
ls command will give you a nice
human readable format of ls command output use option ‘-lh’
[root@TechTutorial ~]# ls -lh
total 24K
-rw-r--r--. 1 root
root 0 Jan 11 15:47 a
-rw-------. 1 root ravi 1.8K
Jan 8 22:47 anaconda-ks.cfg
-rw-r--r--. 1 root
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 0 Jan 11 15:47 b
-rw-r--r--. 1 root
root 0 Jan 11 15:47 c
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 dir1
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 root ravi 1.8K
Jan 8 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5.3K
Jan 10 17:44 techtutorial
14. List inode number of files and directories
we can also list the inode
numbers of files and directories using option ‘-i’
[root@TechTutorial ~]# ls -i
71153561
a
71153570 arkitsample.txt 71153562 b 37492119 dir1 71163580 initial-setup-ks.cfg
71142770 anaconda-ks.cfg
71153571 ARKIT.txt 71153568 c
71153564 dir2 71153567 techtutorial
15. List only directories using -d option
ls command will only list
directories without files in order to do that we have to use #ls -d */ command.
[root@TechTutorial ~]# ls -d */
dir1/ dir2/
16. List files & directories, directories should append with / (slash)
in order to append the
directories with / ( slash ) we have to use option ‘-p’. if you observe below
example output all the directories ended with / ls command with 25 practical examples
[root@TechTutorial ~]# ls -p
a anaconda-ks.cfg
arkitsample.txt ARKIT.txt b c dir1/ dir2/
initial-setup-ks.cfg techtutorial
17. Print files & directory names in quoted format
As we can list the files and
directories using ls command, file names will be printed as quoted format using
-Q option
[root@TechTutorial ~]# ls -lQ
total 24
-rw-r--r--. 1 root
root 0 Jan 11 15:47 "a"
-rw-------. 1 root ravi 1765
Jan 8 22:47 "anaconda-ks.cfg"
-rw-r--r--. 1 root
root 2 Jan 10 17:45 "arkitsample.txt"
-rw-r--r--. 1 root
root 3 Jan 10 17:45 "ARKIT.txt"
-rw-r--r--. 1 root
root 0 Jan 11 15:47 "b"
-rw-r--r--. 1 root
root 0 Jan 11 15:47 "c"
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 "dir1"
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 "dir2"
-rw-r--r--. 1 root ravi 1813
Jan 8 23:05 "initial-setup-ks.cfg"
-rw-r--r--. 1 root root 5362
Jan 10 17:44 "techtutorial"
[root@TechTutorial ~]# ls -Q
"a"
"anaconda-ks.cfg" "arkitsample.txt"
"ARKIT.txt" "b" "c"
"dir1" "dir2"
"initial-setup-ks.cfg" "techtutorial"
18. Sort the list by time stamp
we can sort the files and
directories by its time, this option will list old time stamp below newer up
[root@TechTutorial ~]# ls -lt
total 24
-rw-r--r--. 1 root
root 0 Jan 11 15:47 b
-rw-r--r--. 1 root
root 0 Jan 11 15:47 c
-rw-r--r--. 1 root
root 0 Jan 11 15:47 a
-rw-r--r--. 1 root
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1 root root 5362 Jan
10 17:44 techtutorial
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 dir1
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 root ravi 1813
Jan 8 23:05 initial-setup-ks.cfg
-rw-------. 1 root ravi 1765
Jan 8 22:47 anaconda-ks.cfg
19. Print in reverse
As example 18 is showing output
old files below and newer files up. using ‘-r’ option print in reverse way, old
first and new last
[root@TechTutorial ~]# ls -ltr
total 24
-rw-------. 1 root ravi 1765
Jan 8 22:47 anaconda-ks.cfg
-rw-r--r--. 1 root ravi 1813
Jan 8 23:05 initial-setup-ks.cfg
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 dir2
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 dir1
-rw-r--r--. 1 root root 5362
Jan 10 17:44 techtutorial
-rw-r--r--. 1 root
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 0 Jan 11 15:47 a
-rw-r--r--. 1 root
root 0 Jan 11 15:47 c
-rw-r--r--. 1 root
root 0 Jan 11 15:47 b
20. List recursively
using option ‘-R’ we can list
files and directories in recursively
[root@TechTutorial ~]# ls -R
.:
a anaconda-ks.cfg
arkitsample.txt ARKIT.txt b c dir1 dir2
initial-setup-ks.cfg techtutorial
./dir1:
Ravi Test1
./dir1/Ravi:
Kumar
./dir1/Ravi/Kumar:
Tech
./dir1/Ravi/Kumar/Tech:
TUtorial
./dir1/Ravi/Kumar/Tech/TUtorial:
./dir2:
21. print the allocated size of each file, in blocks
using option ‘-S’ we can sort
by file size
[root@TechTutorial ~]# ls -lS
total 24
-rw-r--r--. 1 root root 5362
Jan 10 17:44 techtutorial
-rw-r--r--. 1 root ravi 1813
Jan 8 23:05 initial-setup-ks.cfg
-rw-------. 1 root ravi 1765
Jan 8 22:47 anaconda-ks.cfg
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 dir1
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 root
root 3 Jan 10 17:45 ARKIT.txt
-rw-r--r--. 1 root
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 0 Jan 11 15:47 a
-rw-r--r--. 1 root
root 0 Jan 11 15:47 b
-rw-r--r--. 1 root
root 0 Jan 11 15:47 c
22. sort by alphabetical order
in order to print in
alphabetical order we have to use -X option ls
command with 25 practical examples
[root@TechTutorial ~]# ls -lX
total 24
-rw-r--r--. 1 root
root 0 Jan 11 15:47 a
-rw-r--r--. 1 root
root 0 Jan 11 15:47 b
-rw-r--r--. 1 root
root 0 Jan 11 15:47 c
drwxr-xr-x. 3 root
root 29 Jan 10 17:20 dir1
drwxr-xr-x. 2 root
root 6 Jan 10 17:05 dir2
-rw-r--r--. 1 root root 5362
Jan 10 17:44 techtutorial
-rw-------. 1 root ravi 1765
Jan 8 22:47 anaconda-ks.cfg
-rw-r--r--. 1 root ravi 1813
Jan 8 23:05 initial-setup-ks.cfg
-rw-r--r--. 1 root
root 2 Jan 10 17:45 arkitsample.txt
-rw-r--r--. 1 root
root 3 Jan 10 17:45 ARKIT.txt
23. List with tab space
ls command default will display
file space as 8 COLS, using -T we can print with more tab space in between
files
[root@TechTutorial ~]# ls
a anaconda-ks.cfg
arkitsample.txt ARKIT.txt b c dir1 dir2
initial-setup-ks.cfg techtutorial
[root@TechTutorial ~]# ls -T1
a
anaconda-ks.cfg
arkitsample.txt
ARKIT.txt b
c dir1
dir2 initial-setup-ks.cfg techtutorial
24. Print file names in one row
using option -1 (numeric number
1) ls command with 25 practical examples
[root@TechTutorial ~]# ls -1
a
anaconda-ks.cfg
arkitsample.txt
ARKIT.txt
b
c
dir1
dir2
initial-setup-ks.cfg
techtutorial
25. Lets know the ls command version details and author details
[root@TechTutorial ~]# 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.
Comments
Post a Comment