ls command stands for list. It is used to list the directory contents. We can list the files and directories inside a directory. With ls command options we can sort the directory contents alphabetically or in ascending and descending order and display the directory contents based on the last modified date and time in ascending order.
ls command sysntax:
ls [options] [FILE]
Display output in long list format:
By default ls command display the output in horizontal format. If we want to display more details like file owner, file group, file size, created time etc use -l option. -l option can be used with other options as well.
# ls -l
total 7
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
Display the hidden files:
ls command by default does not display the hidden files and directories. With ls -a option we can display all the hidden files inside a directory.
# ls -la
total 12
dr-xr-x---. 20 root root 4096 Oct 10 12:08 .
dr-xr-xr-x. 17 root root 224 Sep 21 03:26 ..
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
-rw-------. 1 root root 9086 Oct 10 06:30 .bash_history
-rw-r--r--. 1 root root 18 May 11 2019 .bash_logout
-rw-r--r--. 1 root root 176 May 11 2019 .bash_profile
-rw-r--r--. 1 root root 176 May 11 2019 .bashrc
drwx------. 10 root root 236 Sep 21 05:07 .cache
drwx------. 12 root root 230 Oct 4 12:35 .config
-rw-r--r--. 1 root root 100 May 11 2019 .cshrc
drwx------. 3 root root 25 Sep 21 04:59 .dbus
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
Do not display . and .. in a directory:
By default with ls -a command it will display all the hidden files along with . (dot) and .. (two dots). Those are referring to current directory (.) and previous directory (..) . If you don’t want to display it in the output you can use -A option with ls command.
# ls -A
anaconda-ks.cfg .bashrc .dbus .esd_auth .lesshst Pictures scripts test_1.txt test.py
.bash_history .cache Desktop .gitconfig .local .pki Student_data1 test_2 Videos
.bash_logout .config Documents .ICEauthority ls_command_data.sh Public .tcshrc test_data .viminfo
.bash_profile .cshrc Downloads initial-setup-ks.cfg Music .python_history Templates test_file.txt youu
Display author of the file:
With –author option you can display the author of the files in the command output. We need to use –author option along with -l as below.
# ls -l --author
total 8
-rw-------. 1 root root root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root root 6 Sep 21 05:04 Pictures
Display block size of each file:
By default ls -l command display the size of the files or directories based on their sizes. With –block-size=K we can display files sizes in blocks of KBs. If you use –block-size=M it will display all files sizes in blocks of MBs. Below are the valid units we can use to display the block size.
K,M,G,T,P,E,Z,Y
K = KB (KiloByte) Equals to 1024 Bytes
M = MB (MegaByte) Equals to 1024 KB
G = GB (GigaByte) Equals to 1024 MB
T = TB (TeraByte) Equals to 1024 GB
P = PB (PetaByte) Equals to 1024 TB
E = EB (ExaByte) Equals to 1024 PB
Z = ZB (ZettaByte) Equals to 1024 EB
Y = YB (YottaByte) Equals to 1024 ZB
# ls -l --block-size=M
total 1M
-rw-------. 1 root root 1M Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1M Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1M Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Music
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Pictures
Do not display files ending with ~:
If you don’t want to display the files or directories entries which are ending with ~ use -B option along with l (long list) in ls command as below.
# ls -lB
Display the output in color format:
We can display the ls command output in color format with –color=always option. For color we have parameters like always, auto, never. If we use always then ls command output will be displayed in color format bifurcating files and directories in a different colors. If we use ‘never’ in color then no color formatting applied to the output.
# ls -l --color=always
# ls -l --color=never
# ls -l --color=auto
# ls -l --color=always
total 8
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
List the directory itself:
With -d option along with -l option in ls command we can list the directory details and not the directory contents. If you want to check the directory properties you can use this command.
# ls -ld
dr-xr-x---. 20 root root 4096 Oct 10 12:08 .
Differentiate directories in the output:
Files and directories can be differentiated with indicators such as / (slash) so that it is easy to identify which are files and which are directories. Below commands will work same.
# ls -F
# ls --file-type
# ls -l --indicator-style=slash
# ls -l --indicator-style=file-type
# ls -l --indicator-style=classify
# ls -F
anaconda-ks.cfg Documents/ initial-setup-ks.cfg Music/ Public/ Student_data1/ test_1.txt test_data/ test.py youu/
Desktop/ Downloads/ ls_command_data.sh* Pictures/ scripts/ Templates/ test_2/ test_file.txt Videos/
Append / (slash) to directories:
With -p option we can append / (slash) to directories in the output.
# ls -p
Display the timestamp of files and directories:
By using –full-time option with ls command we can display the full timestamp of files and directories in the output
# ls --full-time
total 8
-rw-------. 1 root root 1032 2022-09-21 04:43:08.227171884 -0400 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 2022-09-21 05:04:39.783250524 -0400 Desktop
drwxr-xr-x. 2 root root 6 2022-09-21 05:04:39.784250524 -0400 Documents
drwxr-xr-x. 2 root root 6 2022-09-21 05:04:39.783250524 -0400 Downloads
-rw-r--r--. 1 root root 1431 2022-09-21 05:02:31.786246428 -0400 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 2022-10-10 05:51:36.253348906 -0400 ls_command_data.sh
drwxr-xr-x. 2 root root 6 2022-09-21 05:04:39.784250524 -0400 Music
drwxr-xr-x. 2 root root 6 2022-09-21 05:04:39.784250524 -0400 Pictures
Display group name of files and directories:
With -g option we can display only the group name and not the owner name of the files and directories.
# ls -g
total 8
-rw-------. 1 root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root 6 Sep 21 05:04 Pictures
Don’t display the group names:
With -G option along with -l option we can only display owner name and not the group name of files and directories.
# ls -Gl
# ls -o
List directories first:
List all the directories first in the output and display the files next to it.
# ls -l --group-directories-first
total 15
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Public
drwxr-xr-x. 2 root root 6 Oct 10 05:22 scripts
drwxr-xr-x. 2 root root 6 Oct 10 02:11 Student_data1
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Templates
drwxrwxrwx. 2 root root 6 Sep 30 12:32 test_2
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Videos
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
Display output in human readable format:
With -h along with -l and -s options we can print the output in human readable format. For suppose if a file contains a size of 1024 KB it can be displayed as 1MB as 1024 KB is equals to 1MB. Likewise it will apply for all sizes.
# ls -lh
# ls -sh
# ls -lh
total 20K
-rw-------. 1 root root 1.1K Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1.4K Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1.9K Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
Do not display specific files or by pattern:
With –hide option or with –ignore option we can hide the files are directories to be display in the output by mentioning the file names. You can use wild card characters to specify files patterns to hide it from the output.
To hide all the files in the output ending with .txt use below command.
# ls --hide=*.txt
# ls -l --hide=*.txt
# ls --ignore=*.txt
# ls -l --ignore=*.txt
In below ls command output there are not .txt files displayed.
# ls -l --hide=*.txt
total 9
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Public
To hide all the files in the output ending with .conf use below command.
# ls --hide=*.cfg
# ls -l --hide=*.cfg
# ls --ignore=*.cfg
# ls -l --ignore=*.cfg
# ls -l --hide=*.cfg
total 7
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Public
Display inode details of all files:
With -i option we can display the inode (index node) details of all the files in that directory. In inode files Linux store metadata of each file.
# ls -i
# ls -li
# ls -li
total 8
101415551 -rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
68200740 drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
68200741 drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
101415236 drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
101415266 -rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
102174052 -rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
101415287 drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
863182 drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
Display details of the file for the symbolic links:
While displaying details for the symbolic links with -L option we can show the details of the file instead of the link.
# ls -Ll
Display files and directories in comma separated format:
With -m option files and directories can be displayed in a comma separated format in horizontal style.
# ls -m
anaconda-ks.cfg, Desktop, Documents, Downloads, initial-setup-ks.cfg, ls_command_data.sh, Music, Pictures, Public, scripts, Student_data1, Templates, test_1.txt,
test_2, test_data, test_file.txt, test.py, Videos, youu
Display user, group details in numeric format:
With -n option user and group details can be displayed in numeric format instead of actual user, group names.
# ls -nl
# ls --numeric-uid-gid
# ls -nl
total 8
-rw-------. 1 0 0 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 0 0 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 0 0 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 0 0 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 0 0 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 0 0 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 0 0 6 Sep 21 05:04 Music
drwxr-xr-x. 2 0 0 6 Sep 21 05:04 Pictures
Enclose names of files and directories in quotes:
We can enclose all the entries using -Q option in ls command.
# ls -Q
# ls -Ql
# ls --quote-name
# ls -l --quote-name
# ls -Q
"anaconda-ks.cfg" "Documents" "initial-setup-ks.cfg" "Music" "Public" "Student_data1" "test_1.txt" "test_data" "test.py" "youu"
"Desktop" "Downloads" "ls_command_data.sh" "Pictures" "scripts" "Templates" "test_2" "test_file.txt" "Videos"
# ls -l --quote-name
total 8
-rw-------. 1 root root 1032 Sep 21 04:43 "anaconda-ks.cfg"
drwxr-xr-x. 2 root root 6 Sep 21 05:04 "Desktop"
drwxr-xr-x. 2 root root 6 Sep 21 05:04 "Documents"
drwxr-xr-x. 2 root root 6 Sep 21 05:04 "Downloads"
-rw-r--r--. 1 root root 1431 Sep 21 05:02 "initial-setup-ks.cfg"
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 "ls_command_data.sh"
drwxr-xr-x. 2 root root 6 Sep 21 05:04 "Music"
drwxr-xr-x. 2 root root 6 Sep 21 05:04 "Pictures"
Display the output in reverse order:
While sorting the ls command output with -r we can sort it in reverse order.
# ls -lr
total 8
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Videos
-rw-r--r--. 1 root root 0 Oct 8 02:07 test.py
-rw-r--r--. 1 root root 187 Oct 5 12:38 test_file.txt
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
drwxrwxrwx. 2 root root 6 Sep 30 12:32 test_2
-rw-r--r--. 1 root root 134 Oct 5 06:55 test_1.txt
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Templates
Display subdirectories information:
By default ls command displays only the current directory contents. To display sub directories details also use -R option in ls command.
# ls -lR
.:
total 20
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Public
drwxr-xr-x. 2 root root 6 Oct 10 05:22 scripts
drwxr-xr-x. 2 root root 6 Oct 10 02:11 Student_data1
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Templates
-rw-r--r--. 1 root root 134 Oct 5 06:55 test_1.txt
drwxrwxrwx. 2 root root 6 Sep 30 12:32 test_2
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
-rw-r--r--. 1 root root 187 Oct 5 12:38 test_file.txt
-rw-r--r--. 1 root root 0 Oct 8 02:07 test.py
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Videos
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
./Desktop:
total 0
./Documents:
total 0
./Downloads:
total 0
./Music:
total 0
Sort files by size:
With -S option in ls command we can sort the output based on the size of the files. Largest size file will display first
# ls -lS
total 9
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 187 Oct 5 12:38 test_file.txt
-rw-r--r--. 1 root root 134 Oct 5 06:55 test_1.txt
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
Sort files by time, size, extension, version:
We can sort files and directories based on their properties like creation time, modified time, file size, file extension, version.
# ls -l --sort=none
# ls -l --sort=time
# ls -l --sort=size
# ls -l --sort=extension
# ls -l --sort=version
# ls -l --sort=time
total 9
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Oct 10 05:22 scripts
drwxr-xr-x. 2 root root 6 Oct 10 02:11 Student_data1
-rw-r--r--. 1 root root 0 Oct 8 02:07 test.py
-rw-r--r--. 1 root root 187 Oct 5 12:38 test_file.txt
-rw-r--r--. 1 root root 134 Oct 5 06:55 test_1.txt
drwxrwxrwx. 2 root root 6 Sep 30 12:32 test_2
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
Sort files by atime, access, use, ctime, status:
We can sort files and directories based on their properties like creation time, modified time, file size, file extension, version.
# ls -l --time=atime
# ls -l --time=access
# ls -l --time=use
# ls -l --time=ctime
# ls -l --time=status
# ls -l --time=atime
total 8
-rw-------. 1 root root 1032 Sep 21 05:01 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Desktop
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Documents
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 Oct 10 06:00 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Music
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Pictures
Display files timestamp in different formats:
The file creation time or modified time we can display it different formats. Below are the available formats to display the files timestamp in ls command.
# ls -l --time-style=full-iso
# ls -l --time-style=long-iso
# ls -l --time-style=iso
# ls -l --time-style=locale
# ls -l --time-style=+%H:%M
# ls -l --time-style=+%H:%M
total 8
-rw-------. 1 root root 1032 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 05:04 Desktop
drwxr-xr-x. 2 root root 6 05:04 Documents
drwxr-xr-x. 2 root root 6 05:04 Downloads
-rw-r--r--. 1 root root 1431 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 05:04 Music
drwxr-xr-x. 2 root root 6 05:04 Pictures
Display the output by sorting based on modification time:
By using -t along with -l option we can display the output by sorting based on the modification time of files and directories. Newest entries will display first.
# ls -lt
total 9
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Oct 10 05:22 scripts
drwxr-xr-x. 2 root root 6 Oct 10 02:11 Student_data1
-rw-r--r--. 1 root root 0 Oct 8 02:07 test.py
-rw-r--r--. 1 root root 187 Oct 5 12:38 test_file.txt
-rw-r--r--. 1 root root 134 Oct 5 06:55 test_1.txt
drwxrwxrwx. 2 root root 6 Sep 30 12:32 test_2
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
Display output by sorting based on name and show access time:
With -u option along with -l option sort the output based on the file names and display the access time of the files and directories.
# ls -lu
total 8
-rw-------. 1 root root 1032 Sep 21 05:01 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Desktop
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Documents
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 Oct 10 06:00 ls_command_data.sh
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Music
drwxr-xr-x. 2 root root 6 Oct 10 01:28 Pictures
Display output by sorting based on access time:
We can sort the output bases on the access time of the files and directories with -t and -u option along with -l option in ls command.
# ls -ltu
total 7
-rwxr-xr-x. 1 root root 1859 Oct 10 06:00 ls_command_data.sh
drwxr-xr-x. 3 root root 16 Oct 10 05:38 youu
drwxr-xr-x. 2 root root 6 Oct 10 05:38 Templates
drwxrwxrwx. 2 root root 6 Oct 10 05:38 test_2
drwxr-xr-x. 4 root root 36 Oct 10 05:38 test_data
drwxr-xr-x. 2 root root 6 Oct 10 05:38 Public
drwxr-xr-x. 2 root root 6 Oct 10 05:38 scripts
Display output by sorting directories:
With -U option we can sort the output based on directory names. Only directories will display in sorted format and files will display after the directory listing.
# ls -lU
total 7
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Templates
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Public
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
Display output by sorting based on files extension:
With -X option along with -l option output can be sorted based on the files extension.
# ls -lX
total 20
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Downloads
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Public
drwxr-xr-x. 2 root root 6 Oct 10 05:22 scripts
drwxr-xr-x. 2 root root 6 Oct 10 02:11 Student_data1
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Templates
drwxrwxrwx. 2 root root 6 Sep 30 12:32 test_2
drwxr-xr-x. 4 root root 36 Sep 26 12:36 test_data
drwxr-xr-x. 2 root root 6 Sep 21 05:04 Videos
drwxr-xr-x. 3 root root 16 Sep 26 12:42 youu
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rw-r--r--. 1 root root 0 Oct 8 02:07 test.py
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
-rw-r--r--. 1 root root 134 Oct 5 06:55 test_1.txt
-rw-r--r--. 1 root root 187 Oct 5 12:38 test_file.txt
Display the security context of files and directories:
With -Z option along with -l option security context of all the files and directories can be displayed.
# ls -lZ
# ls -l --context
# ls -lZ
total 8
-rw-------. 1 root root system_u:object_r:admin_home_t:s0 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root system_u:object_r:admin_home_t:s0 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root unconfined_u:object_r:admin_home_t:s0 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Sep 21 05:04 Pictures
Display the version of ls command:
With –version option we can check the version of ls command installed.
# ls --version
ls (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://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.
Please check this shell script to demonstrate all the ls (list) command examples.
For more information on ls (list) command please visit ls command manual site here.