SlideShare a Scribd company logo
1 of 41
Download to read offline
35 ls Command Examples in
Linux (The Complete Guide)
Do you know how to list contents (files/directories) in Linux?
It’s straightforward.
In this article, you will learn what the LS command and its use is.
Promise me, you will read this article entirely, and I promise you that the
concept of your ls command will be cleared till the end of this article.
In my opinion, if someone starts learning Linux, the first command he should
learn is ls command.
What is the use of ls command?
In simple language, ls command helps to list the contents of a directory in
Linux with useful information.
ls command provides the following information:
● Permission details
● Content owner and group information
● Modified Date and Time
● Size (Normally ls lists sizes in Bytes, but we can also check the
size in Human Readable format)
● Inode information and so on
Note: What we call a folder in Microsoft Windows is called a directory in
Linux.
We usually use Microsoft Windows, and we are experts in it, that’s why I
compare my Linux articles with Windows similar features so that your concept
becomes clear.
What will you do if you want to list the contents of any directory in Microsoft
Windows?
Just double click on the folder whose contents you want to list, and its
contents will be listed in front of you because Windows is a GUI (Graphical
User Interface) based operating system.
Similarly, since Linux provides us with a Command Line Interface (CLI), we
have to use the ls command to see what is inside a directory.
ls was initially introduced in the Unix operating system, and as Linux is made
up of the Unix OS, we are using this command in Linux.
How to remember Linux commands?
Often people ask how to remember Linux commands easily. I have an
excellent trick to remember these commands.
Here it is:
Most of the commands of Linux are derived from ordinary words. Hence this
command can be easily remembered. Here some examples:
● ls command came from the Word “List.“
● mkdir command came from the phrase “Make Directory.“
● rmdir command came from the phrase “Remove Directory.“
● du command came from the phrase “Disk Usage.“
● pwd command came from the phrase “Print Working Directory”
and so on…
Suggested Read:
● How to create a Directory in Linux
● How to delete a directory in Linux
So, let’s try to understand ls commands and all its options with example.
Every command of Linux comes with many options, and by using these
options, we can use the complete features of that command.
In the table below, you will find all the options related to this command.
Options Explanation
-l Long listing of Files & Directories
--author Print authors of the Content
~ Display the contents of Home Directory
-a List Hidden Files & Directories
-A do not list implied (.) and (..)
-R Print Files & Directories Recursively
-d List directory themselves and not their
content
-h Display size in Human Readable
format
-1 Display one File/Directory per line
-s Print the allocated size of each File &
Directory
-F Display files & directories with different
indicators at the end
-i List Inode numbers
-x Display the contents in a line pattern
-m List contents in a comma-separated
format
-Q Display content names within Quotes
-n Display UID and GID of Files &
Directories
-g Only list Group names in a long listing
format
-G Only list Owner names in a long listing
format
-u List Files & Directories as per Access
Time
-r List files & directories in Reverse order
-S Sort contents as per its Size
-t Sort contents as per its Modification
Time
--hide Exclude/Hide file types from the List
--time-style="+%Y-%m-%d
$newline%m-%d %H:%M"
Display contents by different styled
modified date format
-X Sort files as per its Extension
--color=auto
--color=yes
--color=no
--color=never
Enable/Disable colors in Output
--help
man
Help/Manual page access
--version Check the version of ls command
Showing 1 to 28 of 28 entries
You can use these options with its core command with the help of hyphen (-).
For example:
~$ ls -l
Here ls is the core command, and -l is the option.
Note: You can also address the option as a flag.
Syntax:
You have to follow the below syntax to use ls command.
ls [OPTION]... [FILE]...
1. Simple listing of Files & Directories
Run the following command to list the files and directories of the current
directory in a simple format.
You can check your current directory using the pwd command.
By default, it lists the contents in alphabetical order.
Output:
~$ ls
Desktop Documents Downloads examples.desktop Music Pictures
Public Templates Videos
What you see in blue is the directory, and What you see in white is the file.
Almost all Linux distributions have this similar color pattern.
2. Long listing of Files and Directories
You can use the -l (It’s lowercase L) option to list contents in a long listing
format.
Output:
~$ ls -l
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
As you can see, the output is divided into Seven columns with a lot of useful
information related to files and directories.
So let’s try to understand one by one.
Column #1: First, we will understand the permission section :
● The first character indicates the file type. Linux has many file
types. Refer to the list below:
File Type Explanation
- Regular File
d Directory
n Network File
l Symbolic Link
c Character Special File
s Socket
b Block Special File
p Pipe (FIFO)
Showing 1 to 8 of 8 entries
● In Linux, permission has been divided into nine characters, which
are distributed in User, Group, and others.
The first three characters belong to the Owner (---), Next three characters
belong to Group (---), and Finally, the last three characters belong to Others
(---). Refer to the snapshot above.
Let me tell you a little bit about how to recognize permissions. Like any other
operating system, we have three types of permission in Linux :
● r – Read
● w – Write
● x – Execute
Read Also:
● chmod command: Understanding Linux File Permissions
● Special File Permissions in Linux (SUID, SGID and Sticky Bit)
Now that we have understood the file types and permissions, let’s move
toward other information that we got in a long listing of contents and try to
understand them.
● Column #2: Hard links belong to the file.
● Column #3: Owner of the file/directory
● Column #4: Group to which the file/directory belongs
● Column #5: Size (By Default prints in Bytes but we can display
them in a human readable format)
● Column #6: Last Modified Month, Date & Time
● Column #7: Name of Files & Directories
You can also use the following command to long listing of contents.
~$ ll
This is an alias of the ls -l command, and by default, you will find this in
every Linux distribution.
3. List authors of the contents
Now, as you know that using -l option we can list owner and group details.
There are many chances that the owner and the author can be the same.
I thought it necessary to include it in this guide because it is a feature of ls.
So to list the authors of the content you can use --author option. You have to
use this option with the combination -l.
Output:
~$ ls -l --author
total 52
drwxr-xr-x 4 ubuntu ubuntu ubuntu 4096 Jun 4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Jun 4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
-rw-r--r-- 1 ubuntu ubuntu ubuntu 0 Jun 4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Videos
4. List contents of a specific Directory
You can list the contents of a specific directory using ls command.
To do so, pass the path of the directory to the ls.
Here is an example:
I want to list the contents of Documents/data/ directory.
~$ ls -l Documents/data/
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test5.txt
5. List contents of multiple Directories
This command is the same as the above one.
The only difference is we have to mention the path of multiple directories
instead of one.
Let me show you an example.
Here I want to list the contents of the following directories :
1. data1
2. data2
3. data3
So the command would be:
~$ ls -l data1 data2 data3
data1:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file5.txt
data2:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test5.txt
data3:
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data2.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data3.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data4.txt
-rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data5.txt
6. List contents of the Home Directory
You don’t have to mention the entire path of the home directory to list the
contents.
All you have to do is pass the Tilde (~) Special character to ls because Tilde
(~) Indicates Home Directory in Linux.
Example:
~$ ls -l ~
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
7. List contents of Parent Directory
Let me explain to you what is the parent and current directory.
The directory within which the current directory exists is called the parent
directory.
The directory in which i am working now is called the current directory, also
known as Working Directory.
In this example my current directory is /home/ubuntu/data.
~/data$ pwd
/home/ubuntu/data
~/data$ ls
dir1 dir2 file1.txt file2.txt file3.txt file4.txt file5.txt
But our purpose is to list the contents of the parent directory.
In this case, its /home/ubuntu.
To do so, type the following command.
~/data$ ls ../
data Desktop Downloads file1.txt Music Public
Videos
data1 Documents examples.desktop file.txt Pictures Templates
You can also list the contents of 2nd & 3rd level Parent Directories.
ubuntu@ubuntu:~/data$ ls ../../
ubuntu
ubuntu@ubuntu:~/data$ ls ../../../
bin dev initrd.img lib64 mnt root snap sys
var
boot etc initrd.img.old lost+found opt run srv tmp
vmlinuz
cdrom home lib media proc sbin swapfile usr
8. List only files using ls command
By default, ls lists all available contents of a requested directory, but you can
list only files using the following command.
~$ ls -l | egrep -v '^d'
total 80
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
-rw-r--r-- 1 ubuntu ubuntu 23 May 1 07:18 file1
-rw-r--r-- 1 ubuntu ubuntu 21 May 1 07:01 file1.txtx
-rw-r--r-- 1 ubuntu ubuntu 0 May 1 06:56 file2
9. List only directories
To list only directories instead of all contents, use the following commands.
Method 1:
ubuntu@ubuntu:~$ ls -l | grep "^d"
drwxr-xr-x 2 ubuntu ubuntu 4096 May 1 05:46 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 20480 May 1 07:13 test
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
Suggested Read: grep Command: Search for a pattern in a file
Method 2:
~$ ls -ld */
10. List hidden Files and Directories
How to identify hidden files and directories in Linux?
It’s Simple.
In Linux, the file or directory that starts with a period (.) is hidden.
So to list the Hidden contents use -a option with ls.
~$ ls -a
. .bashrc docs file1.txtx .mozilla
.profile Templates
.. .cache Documents .gnupg Music
Public Videos
.bash_history .config Downloads .ICEauthority my
.ssh
.bash_logout Desktop examples.desktop .local Pictures
.sudo_as_admin_successful
As you can see, Implied is also listed in the above output.
● . – for Current Directory
● ..– for Parent Directory
If you don’t want to list Implied pass the -A option to ls.
~$ ls -A
.bash_history data examples.desktop .local Public
.bash_logout data1 file1.txt .mozilla .ssh
.bashrc Desktop file.txt Music
.sudo_as_admin_successful
.cache Documents .gnupg Pictures Templates
.config Downloads .ICEauthority .profile Videos
11. Recursively list the contents of
Directories and Sub-Directories
What is Recursive?
Recursive means the executed command in Linux will work on a complete
directory tree.
As per the following scenario, if I run the ls command to list the contents of
the directory dir1 recursively, it will display the contents of all three directories
(dir1, dir2 & dir3).
To list contents, recursively pass the -R option to ls.
Example:
Here I am listing the contents of the directory data, which has two
sub-directories i.e., dir1 & dir2.
~$ ls -R data
data:
dir1 dir2 file1.txt file2.txt file3.txt file4.txt file5.txt
data/dir1:
f1.txt f2.txt f3.txt f4.txt f5.txt
data/dir2:
note1.txt note2.txt note3.txt note4.txt note5.txt
12. Display one File/Directory per line
Use the following command to list one file/directory per line.
Note: ls -1(It’s Numeric One)
~$ ls -1
Desktop
Documents
Downloads
examples.desktop
Music
Pictures
Public
Templates
Videos
13. List directory themselves and not their
content
To list the directories themselves instead of their content, pass the -d option to
ls.
Refer to the following command.
~$ ls -ld data
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 05:25 data
14. Display size in Human Readable format
Usually, ls command displays the size of the file or directory in Bytes, which is
difficult to understand.
But if it displays the size of the file in Human Readable Format (GB, MB, KB),
then it will be easy to understand, which is possible with the help of ls
command.
● KB (K) – Kilo Bytes
● MB (M) – Mega Bytes
● GB (G) – Giga Bytes
To print the size in Human readable format, use the -h option. You have to use
this option with the combination -l.
~$ ls -lh Documents/data/
total 40K
-rw-r--r-- 1 ubuntu ubuntu 1.4K Apr 28 06:51 rsyslog.conf
-rw-r--r-- 1 ubuntu ubuntu 19K Apr 28 06:51 services
-rw-r--r-- 1 ubuntu ubuntu 2.7K Apr 28 06:51 sysctl.conf
-rw-r--r-- 1 ubuntu ubuntu 1.3K Apr 28 06:51 ucf.conf
-rw-r--r-- 1 ubuntu ubuntu 4.9K Apr 28 06:51 wgetrc
15. Print the allocated size of each File &
Directory
Usually, ls command prints the file size in a long listing format, but if you want
to display the size in the simple listing of contents, use the -s option.
~$ ls -hs
total 56K
4.0K Desktop 4.0K Documents 12K examples.desktop 4.0K Music 4.0K
Pictures 4.0K Templates
4.0K docs 4.0K Downloads 4.0K file1.txtx 4.0K my 4.0K
Public 4.0K Videos
16. List files & directories with different
indicators at the end
What is the use of Indicators?
Indicators are used to identify the file type. Here is the list of different types of
indicators:
● /– Directory
● |– Pipe
● @– Symlink.
● *– Executable files
● =– socket.
Use the -F option to append indicator to the entries.
~$ ls -F
data/ Desktop/ Downloads/ file1.txt@ Pictures/
Templates/
data1/ Documents/ examples.desktop Music/ Public/ Videos/
17. List Inode numbers using ls command
What is the Inode number?
An Inode is a unique number in Linux that is assigned to files.
Whenever you create a new file, it is assigned a file name and an Inode
number.
Inode number contains the following information’s:
● UID (User ID)
● GID (Group ID)
● File Size
● Permissions
● Modified Month, Date and Time
● Device ID
● Hard links
● Other meta information’s related to that file and many more.
Read Also: Explaining Soft Links And Hard Links in Linux
When you access a file, you use the name of that file from outside, but in the
back end, it remains mapped to an Inode number that processes all requested
tasks.
To print Inode numbers, you can use -i option.
~$ ls -li
total 44
659121 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
659125 drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
659122 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
659057 -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
659126 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
659127 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
659124 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
659123 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
659128 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
18. List contents by lines
By default, ls command lists the content by columns.
Pass the -x option to ls command to list entries by lines.
~$ ls -x
Desktop docs Documents Downloads examples.desktop file1.txtx
Music my Pictures Public
Templates Videos
19. List contents in a comma-separated
format
You can list the contents in a comma-separated format.
To do so, Pass the -m option to ls command.
~$ ls -m
Desktop, docs, Documents, Downloads, examples.desktop, file1.txtx,
Music, my, Pictures, Public,
Templates, Videos
20. Print content names within Quotes
Use the -Q option with ls to print the content names withing Quotes.
Refer to the following sample output.
~$ ls -lQ
total 44
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 "Desktop"
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 "Documents"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Downloads"
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 "examples.desktop"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Music"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Pictures"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Public"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Templates"
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Videos"
21. Display UID and GID of Files &
Directories
What is UID & GID?
By default, when we create a new user in Linux, it is assigned a UID and a
GID.
● UID stands for User Identifier
● GID stands for Group Identifier
Let’s check the ID information of a user named helpdesk:
Usually, ls -l command lists the name of Owner and Group, but we can use
the -n option to list UID & GID information.
~$ ls -ln
total 44
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 1000 1000 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 1000 1000 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Music
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Public
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Videos
Suggested Read:
● How to create Users in Linux
● How to create Groups in Linux
22. Only display Group names in a long
listing format
As we have seen, a long listing format lists both owner and group names.
But you can use the -g option to list only Group name instead of both.
~$ ls -lg
total 44
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Videos
23. Only display Owner names in a long
listing format
If you want to list only Owner names, pass the -G option to ls.
~$ ls -lG
total 44
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Videos
You can also use the following command to get the same output.
~$ ls -o
24. Print last access time of contents
You can display the last access time of files and directories using -u option.
~$ ls -lu
total 52
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:48 data1
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
lrwxrwxrwx 1 ubuntu ubuntu 27 Jun 4 21:59 file1.txt ->
/home/ubuntu/data/file1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Jun 4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Videos
With the combination of -ltu option, you can sort the contents by its access
time.
Latest accessed File/Directory will come in the first place.
~$ ls -ltu
total 52
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Videos
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Downloads
-rw-r--r-- 1 ubuntu ubuntu 0 Jun 4 22:19 file.txt
lrwxrwxrwx 1 ubuntu ubuntu 27 Jun 4 21:59 file1.txt ->
/home/ubuntu/data/file1.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:48 data1
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
25. List files & directories in Reverse order
With the help of -r option, you can sort the contents alphabetically in reverse
order.
~$ ls -lr
total 56
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
-rw-r--r-- 1 ubuntu ubuntu 21 Apr 29 21:23 file1.txtx
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
26. Sort contents in reverse order by
modification time
To list the contents in reverse order by modification time, pass the -ltr option
to ls command.
The latest modified content will come in the last place.
~$ ls -ltr
total 56
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
-rw-r--r-- 1 ubuntu ubuntu 21 Apr 29 21:23 file1.txtx
27. Sort contents by Size
To sort the files & directories by Size, you can pass the -S option to ls
command. Largest file comes first.
ubuntu@ubuntu:~$ ls -lhS
total 52K
-rw-r--r-- 1 ubuntu ubuntu 8.8K Apr 28 05:18 examples.desktop
drwxr-xr-x 4 ubuntu ubuntu 4.0K Jun 4 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4.0K Jun 4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Public
28. Sort Files & Directories by modification
time
Use the -t option to sort files & directories by modification time. The newest
file comes first.
~$ ls -lt
total 56
-rw-r--r-- 1 ubuntu ubuntu 21 Apr 29 21:23 file1.txtx
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
29. List the newest and oldest file as per its
modification time
Here I have listed some contents from which we will filter the latest and oldest
file by modification time.
~$ ls -l
total 52
drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 05:25 data
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop
lrwxrwxrwx 1 ubuntu ubuntu 27 Jun 4 21:59 file1.txt ->
/home/ubuntu/data/file1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 Jun 4 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos
Use the following command to filter out the latest modified file. For that we
need to combine ls with head command.
Note: Head command helps to display the first part of the file.
~$ ls -t | head -1
file.txt
Use the following command to filter out the oldest modified file.
~$ ls -t | tail -1
examples.desktop
30. Usage of Wildcards with the help of ls
command
What is Wildcard?
In simple language, a wildcard is a symbol or a special character.
Due to which we can match the pattern of a word or a string to get our desired
output. It helps a lot to save time.
Here are some popular Wildcard Characters :
● * – Asterisk
● [] – Brackets
● % – Percent
● ? – Question mark
● ! – Exclamation Mark
● # – Number sign (Hash)
● - – Hyphen
Let’s take some examples so that your concept becomes clear.
Here I have listed some contents, and with the help of this data, we will try to
understand wildcard.
~$ ls
file1.doc file3.doc file5.doc services test1.txt test3.txt
test5.txt wgetrc
file2.doc file4.doc rsyslog.conf sysctl.conf test2.txt test4.txt
ucf.conf
Ex. # 1 – List those files that start with “fi“
~$ ls fi*
file1.doc file2.doc file3.doc file4.doc file5.doc
Ex. # 2 – List those files whose extension is “.txt“
~$ ls *.txt
test1.txt test2.txt test3.txt test4.txt test5.txt
Ex. # 3 – Exclude those files from the list whose extension is “.txt“
~/data$ ls -l --hide=*.txt
total 8
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:25 dir1
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:25 dir2
31. List contents by different styled
modified date format
Usually, ls command prints the modified date in MM-DD format.
But with the help of --time-style option, we can display the date format of
our own choice. In fact you can also choose your time format.
Let’s take an example:
Print the Date in YYYY-MM-DD format and Time in HH:MM format.
~$ ls -lh --time-style="+%Y-%m-%d %H:%M"
total 52K
drwxr-xr-x 4 ubuntu ubuntu 4.0K 2020-06-04 23:46 data
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-06-04 05:39 data1
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:33 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Documents
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Downloads
-rw-r--r-- 1 ubuntu ubuntu 8.8K 2020-04-28 05:18 examples.desktop
lrwxrwxrwx 1 ubuntu ubuntu 27 2020-06-04 21:59 file1.txt ->
/home/ubuntu/data/file1.txt
-rw-r--r-- 1 ubuntu ubuntu 0 2020-06-04 22:19 file.txt
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Music
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Public
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Templates
drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Videos
32. Sort files by Extension
Run the following command to sort files by Extension.
~$ ls -X -1
rsyslog.conf
sysctl.conf
ucf.conf
file1.doc
file2.doc
file3.doc
file4.doc
file5.doc
test1.txt
test2.txt
test3.txt
test4.txt
test5.txt
33. Enable/Disable colors in Output
As you know, Linux uses different colors to display files and directories. Like:
● Blue color – Directory
● White color – File
● Sky Blue color – Symlinks
You can enable/disable the colorized output.
The following command will allow Linux to choose default colors.
~$ ls --color=auto
Use the below command to enable colors on output.
~$ ls --color=yes
You can use the following command to disable color on output.
~$ ls --color=never
The following output contains directories, and as you can see, the Colorized
output is disabled.
34. Help/Manual page access
Use the following commands to access the Manual Page/Help Page of ls
command.
~$ ls --help
~$ man ls
35. Check the version of ls command
Check the ls command version using the following command.
~$ ls --version
ls (GNU coreutils) 8.28
Infographic
Refer to this Infographic for complete ls command options.
You can visit at following websites to get more information on ls command.
● List directory contents
● ls Wikipedia Page
Conclusion
I hope you have learned something from this article and you may have found
that ls is a very important command in Linux.
I have tried my best to include all the features of ls command in this guide.
Now I’d like to hear your thoughts.
Was this guide useful to you?
Or maybe you have some queries.
Have I not included any command in this guide?
Leave a comment below.

More Related Content

Similar to 35 ls Command Examples in Linux (The Complete Guide).pdf

Similar to 35 ls Command Examples in Linux (The Complete Guide).pdf (20)

2.Utilities.ppt
2.Utilities.ppt2.Utilities.ppt
2.Utilities.ppt
 
BITS: Introduction to Linux - Software installation the graphical and the co...
BITS: Introduction to Linux -  Software installation the graphical and the co...BITS: Introduction to Linux -  Software installation the graphical and the co...
BITS: Introduction to Linux - Software installation the graphical and the co...
 
Directories description
Directories descriptionDirectories description
Directories description
 
Linux administration training
Linux administration trainingLinux administration training
Linux administration training
 
Commands and shell programming (3)
Commands and shell programming (3)Commands and shell programming (3)
Commands and shell programming (3)
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Linux intro 2 basic terminal
Linux intro 2   basic terminalLinux intro 2   basic terminal
Linux intro 2 basic terminal
 
8.1.intro unix
8.1.intro unix8.1.intro unix
8.1.intro unix
 
Unix
UnixUnix
Unix
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentials
 
part2.pdf
part2.pdfpart2.pdf
part2.pdf
 
QSpiders - Unix Operating Systems and Commands
QSpiders - Unix Operating Systems  and CommandsQSpiders - Unix Operating Systems  and Commands
QSpiders - Unix Operating Systems and Commands
 
Unix Trainning Doc.pptx
Unix Trainning Doc.pptxUnix Trainning Doc.pptx
Unix Trainning Doc.pptx
 
Linux file commands and shell scripts
Linux file commands and shell scriptsLinux file commands and shell scripts
Linux file commands and shell scripts
 
Basic linux commands
Basic linux commandsBasic linux commands
Basic linux commands
 
Lnx
LnxLnx
Lnx
 
An Introduction to Linux
An Introduction to LinuxAn Introduction to Linux
An Introduction to Linux
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux file system nevigation
Linux file system nevigationLinux file system nevigation
Linux file system nevigation
 
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic CommandsChapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
 

Recently uploaded

भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

35 ls Command Examples in Linux (The Complete Guide).pdf

  • 1. 35 ls Command Examples in Linux (The Complete Guide) Do you know how to list contents (files/directories) in Linux? It’s straightforward. In this article, you will learn what the LS command and its use is. Promise me, you will read this article entirely, and I promise you that the concept of your ls command will be cleared till the end of this article. In my opinion, if someone starts learning Linux, the first command he should learn is ls command.
  • 2. What is the use of ls command? In simple language, ls command helps to list the contents of a directory in Linux with useful information. ls command provides the following information: ● Permission details ● Content owner and group information ● Modified Date and Time ● Size (Normally ls lists sizes in Bytes, but we can also check the size in Human Readable format) ● Inode information and so on Note: What we call a folder in Microsoft Windows is called a directory in Linux. We usually use Microsoft Windows, and we are experts in it, that’s why I compare my Linux articles with Windows similar features so that your concept becomes clear. What will you do if you want to list the contents of any directory in Microsoft Windows? Just double click on the folder whose contents you want to list, and its contents will be listed in front of you because Windows is a GUI (Graphical User Interface) based operating system. Similarly, since Linux provides us with a Command Line Interface (CLI), we have to use the ls command to see what is inside a directory. ls was initially introduced in the Unix operating system, and as Linux is made up of the Unix OS, we are using this command in Linux.
  • 3. How to remember Linux commands? Often people ask how to remember Linux commands easily. I have an excellent trick to remember these commands. Here it is: Most of the commands of Linux are derived from ordinary words. Hence this command can be easily remembered. Here some examples: ● ls command came from the Word “List.“ ● mkdir command came from the phrase “Make Directory.“ ● rmdir command came from the phrase “Remove Directory.“ ● du command came from the phrase “Disk Usage.“ ● pwd command came from the phrase “Print Working Directory” and so on… Suggested Read: ● How to create a Directory in Linux ● How to delete a directory in Linux So, let’s try to understand ls commands and all its options with example. Every command of Linux comes with many options, and by using these options, we can use the complete features of that command.
  • 4. In the table below, you will find all the options related to this command. Options Explanation -l Long listing of Files & Directories --author Print authors of the Content ~ Display the contents of Home Directory -a List Hidden Files & Directories -A do not list implied (.) and (..) -R Print Files & Directories Recursively -d List directory themselves and not their content -h Display size in Human Readable format -1 Display one File/Directory per line
  • 5. -s Print the allocated size of each File & Directory -F Display files & directories with different indicators at the end -i List Inode numbers -x Display the contents in a line pattern -m List contents in a comma-separated format -Q Display content names within Quotes -n Display UID and GID of Files & Directories -g Only list Group names in a long listing format -G Only list Owner names in a long listing format -u List Files & Directories as per Access Time
  • 6. -r List files & directories in Reverse order -S Sort contents as per its Size -t Sort contents as per its Modification Time --hide Exclude/Hide file types from the List --time-style="+%Y-%m-%d $newline%m-%d %H:%M" Display contents by different styled modified date format -X Sort files as per its Extension --color=auto --color=yes --color=no --color=never Enable/Disable colors in Output
  • 7. --help man Help/Manual page access --version Check the version of ls command Showing 1 to 28 of 28 entries You can use these options with its core command with the help of hyphen (-). For example: ~$ ls -l Here ls is the core command, and -l is the option. Note: You can also address the option as a flag. Syntax: You have to follow the below syntax to use ls command. ls [OPTION]... [FILE]... 1. Simple listing of Files & Directories
  • 8. Run the following command to list the files and directories of the current directory in a simple format. You can check your current directory using the pwd command. By default, it lists the contents in alphabetical order. Output: ~$ ls Desktop Documents Downloads examples.desktop Music Pictures Public Templates Videos What you see in blue is the directory, and What you see in white is the file. Almost all Linux distributions have this similar color pattern. 2. Long listing of Files and Directories You can use the -l (It’s lowercase L) option to list contents in a long listing format. Output:
  • 9. ~$ ls -l total 44 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos As you can see, the output is divided into Seven columns with a lot of useful information related to files and directories. So let’s try to understand one by one. Column #1: First, we will understand the permission section :
  • 10. ● The first character indicates the file type. Linux has many file types. Refer to the list below: File Type Explanation - Regular File d Directory n Network File l Symbolic Link c Character Special File s Socket
  • 11. b Block Special File p Pipe (FIFO) Showing 1 to 8 of 8 entries ● In Linux, permission has been divided into nine characters, which are distributed in User, Group, and others. The first three characters belong to the Owner (---), Next three characters belong to Group (---), and Finally, the last three characters belong to Others (---). Refer to the snapshot above. Let me tell you a little bit about how to recognize permissions. Like any other operating system, we have three types of permission in Linux : ● r – Read ● w – Write ● x – Execute Read Also: ● chmod command: Understanding Linux File Permissions ● Special File Permissions in Linux (SUID, SGID and Sticky Bit) Now that we have understood the file types and permissions, let’s move toward other information that we got in a long listing of contents and try to understand them. ● Column #2: Hard links belong to the file. ● Column #3: Owner of the file/directory
  • 12. ● Column #4: Group to which the file/directory belongs ● Column #5: Size (By Default prints in Bytes but we can display them in a human readable format) ● Column #6: Last Modified Month, Date & Time ● Column #7: Name of Files & Directories You can also use the following command to long listing of contents. ~$ ll This is an alias of the ls -l command, and by default, you will find this in every Linux distribution. 3. List authors of the contents Now, as you know that using -l option we can list owner and group details. There are many chances that the owner and the author can be the same. I thought it necessary to include it in this guide because it is a feature of ls.
  • 13. So to list the authors of the content you can use --author option. You have to use this option with the combination -l. Output: ~$ ls -l --author total 52 drwxr-xr-x 4 ubuntu ubuntu ubuntu 4096 Jun 4 23:46 data drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Jun 4 05:39 data1 drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Documents drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 ubuntu ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop -rw-r--r-- 1 ubuntu ubuntu ubuntu 0 Jun 4 22:19 file.txt drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu ubuntu 4096 Apr 28 05:25 Videos
  • 14. 4. List contents of a specific Directory You can list the contents of a specific directory using ls command. To do so, pass the path of the directory to the ls. Here is an example: I want to list the contents of Documents/data/ directory. ~$ ls -l Documents/data/ total 0 -rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test1.txt -rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test2.txt -rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test3.txt -rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test4.txt -rw-r--r-- 1 ubuntu ubuntu 0 Apr 28 06:44 test5.txt 5. List contents of multiple Directories This command is the same as the above one. The only difference is we have to mention the path of multiple directories instead of one. Let me show you an example. Here I want to list the contents of the following directories : 1. data1 2. data2 3. data3
  • 15. So the command would be: ~$ ls -l data1 data2 data3 data1: total 0 -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file1.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file2.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file3.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file4.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 file5.txt data2: total 0 -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test1.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test2.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test3.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test4.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:40 test5.txt data3: total 0 -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data1.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data2.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data3.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data4.txt -rw-r--r-- 1 ubuntu ubuntu 0 May 2 06:41 data5.txt 6. List contents of the Home Directory You don’t have to mention the entire path of the home directory to list the contents. All you have to do is pass the Tilde (~) Special character to ls because Tilde (~) Indicates Home Directory in Linux. Example:
  • 16. ~$ ls -l ~ total 44 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos 7. List contents of Parent Directory Let me explain to you what is the parent and current directory. The directory within which the current directory exists is called the parent directory. The directory in which i am working now is called the current directory, also known as Working Directory.
  • 17. In this example my current directory is /home/ubuntu/data. ~/data$ pwd /home/ubuntu/data ~/data$ ls dir1 dir2 file1.txt file2.txt file3.txt file4.txt file5.txt But our purpose is to list the contents of the parent directory. In this case, its /home/ubuntu. To do so, type the following command. ~/data$ ls ../ data Desktop Downloads file1.txt Music Public Videos data1 Documents examples.desktop file.txt Pictures Templates You can also list the contents of 2nd & 3rd level Parent Directories. ubuntu@ubuntu:~/data$ ls ../../ ubuntu ubuntu@ubuntu:~/data$ ls ../../../ bin dev initrd.img lib64 mnt root snap sys var boot etc initrd.img.old lost+found opt run srv tmp vmlinuz cdrom home lib media proc sbin swapfile usr
  • 18. 8. List only files using ls command By default, ls lists all available contents of a requested directory, but you can list only files using the following command. ~$ ls -l | egrep -v '^d' total 80 -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop -rw-r--r-- 1 ubuntu ubuntu 23 May 1 07:18 file1 -rw-r--r-- 1 ubuntu ubuntu 21 May 1 07:01 file1.txtx -rw-r--r-- 1 ubuntu ubuntu 0 May 1 06:56 file2 9. List only directories To list only directories instead of all contents, use the following commands. Method 1: ubuntu@ubuntu:~$ ls -l | grep "^d" drwxr-xr-x 2 ubuntu ubuntu 4096 May 1 05:46 Desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents
  • 19. drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 20480 May 1 07:13 test drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos Suggested Read: grep Command: Search for a pattern in a file Method 2: ~$ ls -ld */ 10. List hidden Files and Directories How to identify hidden files and directories in Linux? It’s Simple. In Linux, the file or directory that starts with a period (.) is hidden. So to list the Hidden contents use -a option with ls. ~$ ls -a . .bashrc docs file1.txtx .mozilla .profile Templates .. .cache Documents .gnupg Music Public Videos .bash_history .config Downloads .ICEauthority my .ssh .bash_logout Desktop examples.desktop .local Pictures .sudo_as_admin_successful
  • 20. As you can see, Implied is also listed in the above output. ● . – for Current Directory ● ..– for Parent Directory If you don’t want to list Implied pass the -A option to ls. ~$ ls -A .bash_history data examples.desktop .local Public .bash_logout data1 file1.txt .mozilla .ssh .bashrc Desktop file.txt Music .sudo_as_admin_successful .cache Documents .gnupg Pictures Templates .config Downloads .ICEauthority .profile Videos 11. Recursively list the contents of Directories and Sub-Directories What is Recursive? Recursive means the executed command in Linux will work on a complete directory tree.
  • 21. As per the following scenario, if I run the ls command to list the contents of the directory dir1 recursively, it will display the contents of all three directories (dir1, dir2 & dir3). To list contents, recursively pass the -R option to ls. Example: Here I am listing the contents of the directory data, which has two sub-directories i.e., dir1 & dir2. ~$ ls -R data data: dir1 dir2 file1.txt file2.txt file3.txt file4.txt file5.txt
  • 22. data/dir1: f1.txt f2.txt f3.txt f4.txt f5.txt data/dir2: note1.txt note2.txt note3.txt note4.txt note5.txt 12. Display one File/Directory per line Use the following command to list one file/directory per line. Note: ls -1(It’s Numeric One) ~$ ls -1 Desktop Documents Downloads examples.desktop Music Pictures Public Templates
  • 23. Videos 13. List directory themselves and not their content To list the directories themselves instead of their content, pass the -d option to ls. Refer to the following command. ~$ ls -ld data drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 05:25 data 14. Display size in Human Readable format Usually, ls command displays the size of the file or directory in Bytes, which is difficult to understand. But if it displays the size of the file in Human Readable Format (GB, MB, KB), then it will be easy to understand, which is possible with the help of ls command. ● KB (K) – Kilo Bytes ● MB (M) – Mega Bytes ● GB (G) – Giga Bytes To print the size in Human readable format, use the -h option. You have to use this option with the combination -l. ~$ ls -lh Documents/data/ total 40K
  • 24. -rw-r--r-- 1 ubuntu ubuntu 1.4K Apr 28 06:51 rsyslog.conf -rw-r--r-- 1 ubuntu ubuntu 19K Apr 28 06:51 services -rw-r--r-- 1 ubuntu ubuntu 2.7K Apr 28 06:51 sysctl.conf -rw-r--r-- 1 ubuntu ubuntu 1.3K Apr 28 06:51 ucf.conf -rw-r--r-- 1 ubuntu ubuntu 4.9K Apr 28 06:51 wgetrc 15. Print the allocated size of each File & Directory Usually, ls command prints the file size in a long listing format, but if you want to display the size in the simple listing of contents, use the -s option. ~$ ls -hs total 56K 4.0K Desktop 4.0K Documents 12K examples.desktop 4.0K Music 4.0K Pictures 4.0K Templates 4.0K docs 4.0K Downloads 4.0K file1.txtx 4.0K my 4.0K Public 4.0K Videos 16. List files & directories with different indicators at the end What is the use of Indicators? Indicators are used to identify the file type. Here is the list of different types of indicators: ● /– Directory ● |– Pipe ● @– Symlink. ● *– Executable files
  • 25. ● =– socket. Use the -F option to append indicator to the entries. ~$ ls -F data/ Desktop/ Downloads/ file1.txt@ Pictures/ Templates/ data1/ Documents/ examples.desktop Music/ Public/ Videos/ 17. List Inode numbers using ls command What is the Inode number? An Inode is a unique number in Linux that is assigned to files. Whenever you create a new file, it is assigned a file name and an Inode number. Inode number contains the following information’s: ● UID (User ID) ● GID (Group ID) ● File Size ● Permissions ● Modified Month, Date and Time ● Device ID ● Hard links ● Other meta information’s related to that file and many more. Read Also: Explaining Soft Links And Hard Links in Linux
  • 26. When you access a file, you use the name of that file from outside, but in the back end, it remains mapped to an Inode number that processes all requested tasks. To print Inode numbers, you can use -i option. ~$ ls -li total 44 659121 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop 659125 drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents 659122 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads 659057 -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop 659126 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music 659127 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures 659124 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public 659123 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates 659128 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos 18. List contents by lines By default, ls command lists the content by columns.
  • 27. Pass the -x option to ls command to list entries by lines. ~$ ls -x Desktop docs Documents Downloads examples.desktop file1.txtx Music my Pictures Public Templates Videos 19. List contents in a comma-separated format You can list the contents in a comma-separated format. To do so, Pass the -m option to ls command. ~$ ls -m Desktop, docs, Documents, Downloads, examples.desktop, file1.txtx, Music, my, Pictures, Public, Templates, Videos 20. Print content names within Quotes Use the -Q option with ls to print the content names withing Quotes. Refer to the following sample output.
  • 28. ~$ ls -lQ total 44 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 "Desktop" drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 "Documents" drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Downloads" -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 "examples.desktop" drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Music" drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Pictures" drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Public" drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Templates" drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 "Videos" 21. Display UID and GID of Files & Directories What is UID & GID? By default, when we create a new user in Linux, it is assigned a UID and a GID. ● UID stands for User Identifier ● GID stands for Group Identifier Let’s check the ID information of a user named helpdesk:
  • 29. Usually, ls -l command lists the name of Owner and Group, but we can use the -n option to list UID & GID information. ~$ ls -ln total 44 drwxr-xr-x 2 1000 1000 4096 Apr 28 05:33 Desktop drwxr-xr-x 3 1000 1000 4096 Apr 28 06:44 Documents drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 1000 1000 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Music drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Public drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Templates drwxr-xr-x 2 1000 1000 4096 Apr 28 05:25 Videos Suggested Read: ● How to create Users in Linux ● How to create Groups in Linux 22. Only display Group names in a long listing format As we have seen, a long listing format lists both owner and group names. But you can use the -g option to list only Group name instead of both. ~$ ls -lg total 44 drwxr-xr-x 2 ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 3 ubuntu 4096 Apr 28 06:44 Documents drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 ubuntu 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Pictures
  • 30. drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Videos 23. Only display Owner names in a long listing format If you want to list only Owner names, pass the -G option to ls. ~$ ls -lG total 44 drwxr-xr-x 2 ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 3 ubuntu 4096 Apr 28 06:44 Documents drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 ubuntu 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu 4096 Apr 28 05:25 Videos You can also use the following command to get the same output. ~$ ls -o 24. Print last access time of contents You can display the last access time of files and directories using -u option. ~$ ls -lu total 52 drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 23:46 data drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:48 data1
  • 31. drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Downloads -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop lrwxrwxrwx 1 ubuntu ubuntu 27 Jun 4 21:59 file1.txt -> /home/ubuntu/data/file1.txt -rw-r--r-- 1 ubuntu ubuntu 0 Jun 4 22:19 file.txt drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Videos With the combination of -ltu option, you can sort the contents by its access time. Latest accessed File/Directory will come in the first place. ~$ ls -ltu total 52 drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 23:46 data drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Videos drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 22:48 Downloads -rw-r--r-- 1 ubuntu ubuntu 0 Jun 4 22:19 file.txt lrwxrwxrwx 1 ubuntu ubuntu 27 Jun 4 21:59 file1.txt -> /home/ubuntu/data/file1.txt drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:48 data1 drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 Desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop 25. List files & directories in Reverse order
  • 32. With the help of -r option, you can sort the contents alphabetically in reverse order. ~$ ls -lr total 56 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music -rw-r--r-- 1 ubuntu ubuntu 21 Apr 29 21:23 file1.txtx -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop 26. Sort contents in reverse order by modification time To list the contents in reverse order by modification time, pass the -ltr option to ls command. The latest modified content will come in the last place. ~$ ls -ltr total 56 -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop
  • 33. drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs -rw-r--r-- 1 ubuntu ubuntu 21 Apr 29 21:23 file1.txtx 27. Sort contents by Size To sort the files & directories by Size, you can pass the -S option to ls command. Largest file comes first. ubuntu@ubuntu:~$ ls -lhS total 52K -rw-r--r-- 1 ubuntu ubuntu 8.8K Apr 28 05:18 examples.desktop drwxr-xr-x 4 ubuntu ubuntu 4.0K Jun 4 23:46 data drwxr-xr-x 2 ubuntu ubuntu 4.0K Jun 4 05:39 data1 drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:33 Desktop drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Documents drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Downloads drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 28 05:25 Public 28. Sort Files & Directories by modification time Use the -t option to sort files & directories by modification time. The newest file comes first. ~$ ls -lt total 56 -rw-r--r-- 1 ubuntu ubuntu 21 Apr 29 21:23 file1.txtx drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 docs drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 29 21:19 my
  • 34. drwxr-xr-x 3 ubuntu ubuntu 4096 Apr 28 06:44 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop 29. List the newest and oldest file as per its modification time Here I have listed some contents from which we will filter the latest and oldest file by modification time. ~$ ls -l total 52 drwxr-xr-x 4 ubuntu ubuntu 4096 Jun 4 05:25 data drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:39 data1 drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:33 Desktop drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Documents drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Downloads -rw-r--r-- 1 ubuntu ubuntu 8980 Apr 28 05:18 examples.desktop lrwxrwxrwx 1 ubuntu ubuntu 27 Jun 4 21:59 file1.txt -> /home/ubuntu/data/file1.txt -rw-r--r-- 1 ubuntu ubuntu 0 Jun 4 22:19 file.txt drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 28 05:25 Videos Use the following command to filter out the latest modified file. For that we need to combine ls with head command.
  • 35. Note: Head command helps to display the first part of the file. ~$ ls -t | head -1 file.txt Use the following command to filter out the oldest modified file. ~$ ls -t | tail -1 examples.desktop 30. Usage of Wildcards with the help of ls command What is Wildcard? In simple language, a wildcard is a symbol or a special character. Due to which we can match the pattern of a word or a string to get our desired output. It helps a lot to save time. Here are some popular Wildcard Characters : ● * – Asterisk ● [] – Brackets ● % – Percent ● ? – Question mark ● ! – Exclamation Mark ● # – Number sign (Hash) ● - – Hyphen
  • 36. Let’s take some examples so that your concept becomes clear. Here I have listed some contents, and with the help of this data, we will try to understand wildcard. ~$ ls file1.doc file3.doc file5.doc services test1.txt test3.txt test5.txt wgetrc file2.doc file4.doc rsyslog.conf sysctl.conf test2.txt test4.txt ucf.conf Ex. # 1 – List those files that start with “fi“ ~$ ls fi* file1.doc file2.doc file3.doc file4.doc file5.doc Ex. # 2 – List those files whose extension is “.txt“ ~$ ls *.txt test1.txt test2.txt test3.txt test4.txt test5.txt Ex. # 3 – Exclude those files from the list whose extension is “.txt“ ~/data$ ls -l --hide=*.txt total 8 drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:25 dir1 drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 4 05:25 dir2 31. List contents by different styled modified date format
  • 37. Usually, ls command prints the modified date in MM-DD format. But with the help of --time-style option, we can display the date format of our own choice. In fact you can also choose your time format. Let’s take an example: Print the Date in YYYY-MM-DD format and Time in HH:MM format. ~$ ls -lh --time-style="+%Y-%m-%d %H:%M" total 52K drwxr-xr-x 4 ubuntu ubuntu 4.0K 2020-06-04 23:46 data drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-06-04 05:39 data1 drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:33 Desktop drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Documents drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Downloads -rw-r--r-- 1 ubuntu ubuntu 8.8K 2020-04-28 05:18 examples.desktop lrwxrwxrwx 1 ubuntu ubuntu 27 2020-06-04 21:59 file1.txt -> /home/ubuntu/data/file1.txt -rw-r--r-- 1 ubuntu ubuntu 0 2020-06-04 22:19 file.txt drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Music drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Pictures drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Public drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Templates drwxr-xr-x 2 ubuntu ubuntu 4.0K 2020-04-28 05:25 Videos 32. Sort files by Extension Run the following command to sort files by Extension. ~$ ls -X -1 rsyslog.conf sysctl.conf ucf.conf file1.doc file2.doc
  • 38. file3.doc file4.doc file5.doc test1.txt test2.txt test3.txt test4.txt test5.txt 33. Enable/Disable colors in Output As you know, Linux uses different colors to display files and directories. Like: ● Blue color – Directory ● White color – File ● Sky Blue color – Symlinks You can enable/disable the colorized output. The following command will allow Linux to choose default colors. ~$ ls --color=auto Use the below command to enable colors on output. ~$ ls --color=yes
  • 39. You can use the following command to disable color on output. ~$ ls --color=never The following output contains directories, and as you can see, the Colorized output is disabled. 34. Help/Manual page access Use the following commands to access the Manual Page/Help Page of ls command. ~$ ls --help ~$ man ls 35. Check the version of ls command Check the ls command version using the following command. ~$ ls --version ls (GNU coreutils) 8.28 Infographic
  • 40. Refer to this Infographic for complete ls command options.
  • 41. You can visit at following websites to get more information on ls command. ● List directory contents ● ls Wikipedia Page Conclusion I hope you have learned something from this article and you may have found that ls is a very important command in Linux. I have tried my best to include all the features of ls command in this guide. Now I’d like to hear your thoughts. Was this guide useful to you? Or maybe you have some queries. Have I not included any command in this guide? Leave a comment below.