The ls command is one of the basic and most used commands that any Linux user/you should know. you can use to list information about files and directories within the Linux/Unix file system.
The ls command in Linux is pre-installed on all Linux/Unix based distributions (Operating System).
I believe, you must know, how to use ls command when you get into the command prompt of Linux Operating System.
In this tutorial, I will show you how to use the ls command in Linux with appropriate practical examples.
I will try to provide you a piece of detailed information with explanations of the most common ls options.
Again ls is a Linux shell command that lists directory contents of files and directories. See the following explanation and examples of ls command in Linux:
Use ls command without any option:
When you use ls command with no option, It will print a list of files and directories in a bare format where You won’t be able to view details like file types, size, modified date and time, permission and links, etc.
$ls
[vijay@localhost ~]$ ls data Desktop Documents Downloads file1 file2 file3 Music Pictures Public root-top-ps.txt Templates Videos [vijay@localhost ~]$
In the above result, you can see two color of text, blue and black. Blue color indicate towards directory other for file.
Most used options of ls command in Linux
As I have told you, Linux user use ls command daily basis and frequently even though, you may not aware and never use all the ls option available.
So I am going to explain some important options. you can use following command to see available options:
$ls –help
You can use man command to know more about ls command in Linux.
$man ls
Shows version of ls command
If you want to check version of ls command then use –version option. see the the following syntax:
[vijay@localhost ~]$ ls --version ls (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . 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. [vijay@localhost ~]$
Print List View by ls command
Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and its permission.
ls -l
[vijay@localhost ~]$ ls -l total 16 drwxrwxr-x. 3 vijay vijay 49 Apr 23 08:59 data drwxr-xr-x. 2 vijay vijay 25 Apr 16 12:50 Desktop drwxrwxr-x. 2 vijay vijay 6 Apr 6 10:57 Documents drwxr-xr-x. 9 vijay vijay 93 Apr 6 12:17 Downloads -rw-rw-r--. 1 vijay vijay 0 Apr 23 08:36 file1 -rw-rw-r--. 1 vijay vijay 92 Apr 23 08:48 file2 -rw-r--r--. 1 vijay vijay 119 Apr 23 08:55 file3 drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Music drwxr-xr-x. 2 vijay vijay 100 Apr 22 08:45 Pictures drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Public -rw-rw-r--. 1 vijay vijay 6650 Apr 16 11:40 root-top-ps.txt drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Templates drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Videos [vijay@localhost ~]$
drwxrwxr-x. 3 vijay vijay 49 Apr 23 08:59 data
1st Character – First character specifies the type of the file.
In the example above, starting character is ‘d’ and this indicates that this is a folder, and the hyphen (-) in the 1st character indicates that this is a normal file.
Following are the possible file type options in the 1st character of the ls -l output.
Field Explanation
– normal file
d : directory
s : socket file
l : link file
- Field 1 – File Permissions: After the 1 character, first field is started, it has 9 character specifies the files permission. The every 3 characters specifies read, write, execute permissions for user(root), group and others respectively in order. If all three permissions are given to user(root), group and others, the format looks like -rwxrwxrwx
- Field 2 – Number of links: Second field specifies the number of links for that file. In this example, 3 indicates only 3 link to this file.
- Field 3 – Owner: Third field specifies owner of the file. In this example, this file is owned by username ‘vijay’.
- Field 4 – Group: Fourth field specifies the group of the file. In this example, this file belongs to ”vijay’ group.
- Field 5 – Size: Fifth field specifies the size of file in bytes. In this example, ‘49’ indicates the file size in bytes.
- Field 6 – Last modified date and time: Sixth field specifies the date and time of the last modification of the folder. In this example, ‘Apr 23 08:59’ specifies the last modification time of the file.
- Field 7 – File/Folder name: The last field is the name of the file. In this example, the file name is 1.c.
View Hidden Files and folders
ls command is very powerful command, you can use it to view hidden files and folders.
In Linux hidden files are started with ‘.’ you can list all files including hidden file starting with ‘.‘. Use the follwoing syntax to view the hidden files
[vijay@localhost ~]$ ls -a . data .ICEauthority Templates .. Desktop .local .vboxclient-display.pid .bash_history Documents .mozilla .vboxclient-display-svga-x11.pid .bash_logout Downloads Music .vboxclient-draganddrop.pid .bash_profile .esd_auth Pictures .vboxclient-seamless.pid .bashrc file1 .pki Videos .cache file2 Public .viminfo .config file3 root-top-ps.txt [vijay@localhost ~]$
View Files with Human Readable Format
You can use the combination of -lh option, to pring sizes in human readable format.
ls -lh
[vijay@localhost ~]$ ls -lh total 16K drwxrwxr-x. 3 vijay vijay 49 Apr 23 08:59 data drwxr-xr-x. 2 vijay vijay 25 Apr 16 12:50 Desktop drwxrwxr-x. 2 vijay vijay 6 Apr 6 10:57 Documents drwxr-xr-x. 9 vijay vijay 93 Apr 6 12:17 Downloads -rw-rw-r--. 1 vijay vijay 0 Apr 23 08:36 file1 -rw-rw-r--. 1 vijay vijay 92 Apr 23 08:48 file2 -rw-r--r--. 1 vijay vijay 119 Apr 23 08:55 file3 drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Music drwxr-xr-x. 2 vijay vijay 147 Apr 23 12:45 Pictures drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Public -rw-rw-r--. 1 vijay vijay 6.5K Apr 16 11:40 root-top-ps.txt drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Templates drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Videos [vijay@localhost ~]$
List Directories with ‘/’ Character at the end
You can use ls command followed by -F option to add the ‘/’ Character at the end each directory.
ls -F
[vijay@localhost ~]$ ls -F data/ Documents/ file1 file3 Pictures/ root-top-ps.txt Videos/ Desktop/ Downloads/ file2 Music/ Public/ Templates/ [vijay@localhost ~]$
List Files in Reverse Order
The following command with ls -r option display files and directories in reverse order.
[vijay@localhost ~]$ ls -r Videos root-top-ps.txt Pictures file3 file1 Documents data Templates Public Music file2 Downloads Desktop [vijay@localhost ~]$
Recursively list Sub-Directories
ls -R option will list very long listing directory trees. See an example of output of the command.
[vijay@localhost ~]$ ls -R .: data Documents file1 file3 Pictures root-top-ps.txt Videos Desktop Downloads file2 Music Public Templates ./data: file1 file2 newfolder ./data/newfolder: ./Desktop: 'centos html' ./Documents: ./Downloads: dir1 dir1~ dir10 dir11 dir2 dir3 dir4 ./Downloads/dir1: ./Downloads/dir1~: ./Downloads/dir10: ./Downloads/dir11: ./Downloads/dir2: ./Downloads/dir3: ./Downloads/dir4: ./Music: ./Pictures: 'Screenshot from 2020-04-22 08-45-10.png' 'Screenshot from 2020-04-22 08-45-11.png' 'Screenshot from 2020-04-23 12-45-40.png' ./Public: ./Templates: ./Videos: [vijay@localhost ~]$
Reverse Output Order
You can use the combination of -ltr to show the latest modification file or directory date as last.
[vijay@localhost ~]$ ls -ltr total 16 drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Templates drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Public drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Videos drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Music drwxrwxr-x. 2 vijay vijay 6 Apr 6 10:57 Documents drwxr-xr-x. 9 vijay vijay 93 Apr 6 12:17 Downloads -rw-rw-r--. 1 vijay vijay 6650 Apr 16 11:40 root-top-ps.txt drwxr-xr-x. 2 vijay vijay 25 Apr 16 12:50 Desktop -rw-rw-r--. 1 vijay vijay 0 Apr 23 08:36 file1 -rw-rw-r--. 1 vijay vijay 92 Apr 23 08:48 file2 -rw-r--r--. 1 vijay vijay 119 Apr 23 08:55 file3 drwxrwxr-x. 3 vijay vijay 49 Apr 23 08:59 data drwxr-xr-x. 2 vijay vijay 147 Apr 23 12:45 Pictures [vijay@localhost ~]$
Sorting Files and Folders by Size
With combination of -lS displays file size in order, will display big in size first.
[vijay@localhost ~]$ ls -lS total 16 -rw-rw-r--. 1 vijay vijay 6650 Apr 16 11:40 root-top-ps.txt drwxr-xr-x. 2 vijay vijay 147 Apr 23 12:45 Pictures -rw-r--r--. 1 vijay vijay 119 Apr 23 08:55 file3 drwxr-xr-x. 9 vijay vijay 93 Apr 6 12:17 Downloads -rw-rw-r--. 1 vijay vijay 92 Apr 23 08:48 file2 drwxrwxr-x. 3 vijay vijay 49 Apr 23 08:59 data drwxr-xr-x. 2 vijay vijay 25 Apr 16 12:50 Desktop drwxrwxr-x. 2 vijay vijay 6 Apr 6 10:57 Documents drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Music drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Public drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Templates drwxr-xr-x. 2 vijay vijay 6 Mar 21 11:17 Videos -rw-rw-r--. 1 vijay vijay 0 Apr 23 08:36 file1 [vijay@localhost ~]$
Display Inode number of File or Directory
We can see some number printed before file / directory name. With -i options list file / directory with inode number.
[vijay@localhost ~]$ ls -i 1283312 data 51725415 file1 51718101 Pictures 1369377 Videos 17953282 Desktop 51725417 file2 1369375 Public 17983391 Documents 51725431 file3 51725621 root-top-ps.txt 34018305 Downloads 34018326 Music 51718100 Templates [vijay@localhost ~]$
Display UID and GID of Files
If you watn to display files and directories with its UID and GID, then you can use option -n with ls command.
[vijay@localhost ~]$ ls -n total 16 drwxrwxr-x. 3 1000 1000 49 Apr 23 08:59 data drwxr-xr-x. 2 1000 1000 25 Apr 16 12:50 Desktop drwxrwxr-x. 2 1000 1000 6 Apr 6 10:57 Documents drwxr-xr-x. 9 1000 1000 93 Apr 6 12:17 Downloads -rw-rw-r--. 1 1000 1000 0 Apr 23 08:36 file1 -rw-rw-r--. 1 1000 1000 92 Apr 23 08:48 file2 -rw-r--r--. 1 1000 1000 119 Apr 23 08:55 file3 drwxr-xr-x. 2 1000 1000 6 Mar 21 11:17 Music drwxr-xr-x. 2 1000 1000 147 Apr 23 12:45 Pictures drwxr-xr-x. 2 1000 1000 6 Mar 21 11:17 Public -rw-rw-r--. 1 1000 1000 6650 Apr 16 11:40 root-top-ps.txt drwxr-xr-x. 2 1000 1000 6 Mar 21 11:17 Templates drwxr-xr-x. 2 1000 1000 6 Mar 21 11:17 Videos [vijay@localhost ~]$
View Last Edited File date
Do you want to know the last edited date of any file? Yes it is possible and you can see by using ls-t option.
You can also sorts the file by modification time, showing the last edited file first by using ls-t option.
[vijay@localhost ~]$ ls -t Pictures file3 file1 root-top-ps.txt Documents Videos Templates data file2 Desktop Downloads Music Public [vijay@localhost ~]$
head -1 picks up this first file.
To open the last edited file in the current directory use the combination of ls and head commands as shown below.
[vijay@localhost ~]$ ls -t | head -1 Pictures [vijay@localhost ~]$
Display Directory Information:
When you use “ls -l” you will get the details of directories content. But if you want the details of the directory then you can use -d option as., For example, if you use ls -l /etc will display all the files under the etc directory. But, if you want to display the information about the /etc/ directory, use -ld option as shown below.
$ ls -l /etc
[vijay@localhost data]$ ls -ld /etc/ drwxr-xr-x. 136 root root 8192 Apr 23 12:36 /etc/ [vijay@localhost data]$
If you will not provide the location of disired location, then this option will print the details of current working directory
[vijay@localhost ~]$ ls -ld drwx------. 16 vijay vijay 4096 Apr 23 12:36 . [vijay@localhost ~]$
Conclusion:
You have learned about ls command in Linux, which is used to display content of any folder including files and folders in Linux and Unix.
For more information about the ls command, check the man page.
If you have any questions or feedback, feel free to leave a comment