Basic “ls” Command examples in Linux

Introduction

In this article, we are going to learn how to use the Linux ls command in Linux. Basically, ls command is a basic command in Linux used to List files and directories. ls command comes with so many arguments and features like you can sort files and directories by Date, by Size, able to check hidden files and directories, permissions, inode information and so on.

So let’s have a look at useful and very important Linux ls command with examples.

Some useful Linux ls command arguments :

  • l – List files and directories in Long Listing Format.
  • a – To check hidden files and directories.
  • i – To check Inode Informations.
  • n – List UID and GID number of Owner and Groups to which the files and directories are belongs.
  • g – List group of files and directories to which they belongs.
  • G – List users and not groups of files and directories to which they belongs.
  • h – List the size of the files and directories in Human Readable format.

Linux ls command with Examples

[root@localhost ~]# ls       ### List Files and Directories
anaconda-ks.cfg  Documents    install.log.syslog  Public
data             Downloads    Music               Templates
Desktop          install.log  Pictures            Videos

You can use ls command with argument -l to get more advance information related files and directories like Permissions, Owner, Group, Size, Created and Modified month, date and time, file name…etc…

[root@localhost ~]# ls -l   # Long Listing of Files and Directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

To list details of a particular file refer the below command.

[root@localhost ~]# ls -l file.txt   # List a Particular File
-rw-r--r--. 1 root root 0 Apr 26 10:52 file.txt

To list the content of a particular directory refer the below command.

[root@localhost ~]# ls -l /mydata/   # List a Particular Directory
total 0
-rw-r--r--. 1 root root 0 Apr 26 10:53 file1.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file2.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file3.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file4.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file5.txt

You can check hidden files and directories by using Linux ls command with argument -a.

[root@localhost ~]# ls -a   # Checking hidden files and directories
.                .config    .gconf           .gvfs               Pictures
..               .cshrc     .gconfd          .ICEauthority       Public
anaconda-ks.cfg  data       .gnome2          install.log         .pulse
.bash_history    .dbus      .gnome2_private  install.log.syslog  .pulse-cookie
.bash_logout     Desktop    .gnote           .local              .ssh
.bash_profile    Documents  .gnupg           .mozilla            .tcshrc
.bashrc          Downloads  .gstreamer-0.10  Music               Templates
.cache           .esd_auth  .gtk-bookmarks   .nautilus           Videos

To check inode number of files and directories use ls command with option -i.

[root@localhost ~]# ls -i  # Checking Inode numbers of files and directories
401494 anaconda-ks.cfg  785089 Downloads           785094 Pictures
917077 data             392451 install.log         785091 Public
785088 Desktop          392452 install.log.syslog  785090 Templates
785092 Documents        785093 Music               785095 Videos

OR you can use the -i argument with -l as shown on below command.

[root@localhost ~]# ls -li
total 100
401494 -rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
917077 drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
785088 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
785092 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
785089 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
392451 -rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
392452 -rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
785093 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
785094 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
785091 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
785090 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
785095 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

ls command with argument -o will show owner of the files and directories and will not show the groups.

[root@localhost ~]# ls -o   # Listing only owner of files and directories
total 100
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

Linux ls command with option -m will list the files and directories separated by comma.

[root@localhost /]# ls -m   # List seperated by comma
bin, boot, dev, etc, home, lib, lib64, lost+found, media, mnt, mydata, opt,
proc, root, sbin, selinux, srv, sys, tmp, users, usr, var

ls command with option -Q will list files and directories in which names of files and directories are enclosed in quotes. Refer the sample output below.

[root@localhost /]# ls -lQ   # Labels of Files and directories enclosed by Quotes
total 106
dr-xr-xr-x.   2 root root  4096 Apr 16 09:59 "bin"
dr-xr-xr-x.   5 root root  1024 Apr  9 02:45 "boot"
drwxr-xr-x.  19 root root  4120 Apr 26 08:51 "dev"
drwxr-xr-x. 102 root root 12288 Apr 26 10:34 "etc"
drwxr-xr-x.   9 root root  4096 Apr 20 09:39 "home"
dr-xr-xr-x.  10 root root  4096 Apr  9 02:39 "lib"
dr-xr-xr-x.   9 root root 12288 Apr 16 09:59 "lib64"
drwx------.   2 root root 16384 Apr  9 02:35 "lost+found"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "media"
drwxr-xr-x.   3 root root  4096 Apr  9 02:46 "mnt"
drwxr-xr-x.   2 root root  4096 Apr 26 10:53 "mydata"
drwxr-xr-x.   3 root root  4096 Apr  8 21:17 "opt"
dr-xr-xr-x. 162 root root     0 Apr 26 08:49 "proc"
dr-xr-x---.  27 root root  4096 Apr 26 10:52 "root"
dr-xr-xr-x.   2 root root 12288 Apr 16 09:59 "sbin"
drwxr-xr-x.   7 root root     0 Apr 26 08:49 "selinux"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "srv"
drwxr-xr-x.  13 root root     0 Apr 26 08:49 "sys"
drwxrwxrwt.  15 root root  4096 Apr 26 10:56 "tmp"
drwxrwxrwx.   3 root root  4096 Apr 20 09:22 "users"
drwxr-xr-x.  13 root root  4096 Apr  9 02:35 "usr"
drwxr-xr-x.  22 root root  4096 Apr 16 09:24 "var"

you can list directories recursively using Linux ls command with the argument -R. Recursively means it will list all the directory with is all subdirectory in a tree format.

[root@localhost ~]# ls -R   # Listing Directories Recursively
.:
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

./data:
a.txt  b.txt  c.txt

./Desktop:
data1  data2  data3

./Desktop/data1:

./Desktop/data2:

./Desktop/data3:

./Documents:

./Downloads:

./Music:

./Pictures:

./Public:

./Templates:

./Videos:

ls command with ~ will switch you to home directory.

[root@localhost Downloads]# pwd 
/root/Downloads
[root@localhost Downloads]# ls ~   # Switch to Home Directory
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

Linux ls command with -t will list the files and directories by it’s modification date in ascending order, means the higher will be come first.

[root@localhost ~]# ls -lt   # Sort by Modified by date
total 100
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates

You can sort the files and directories by it size using ls command with argument -S.

[root@localhost ~]# ls -lS   # Sort by Size in ascending order
total 100
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt

ls command with option -h will list the size of the files and directories in Human Readable format for Example 20K (20 KB), 15M (15 MB).

[root@localhost ~]# ls -lh   # List the size in Human Readable format
total 100K
-rw-------. 1 root root 2.7K Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Apr 24 09:10 data
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Documents
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Downloads
-rw-r--r--. 1 root root    0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root  41K Apr  9 02:44 install.log
-rw-r--r--. 1 root root 9.0K Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Music
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Public
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Templates
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Videos

ls command with option -n will show the UID (User ID) and GID (Group ID) numbers of file and directory.

[root@localhost ~]# ls -ln   # List the UID and GID Numbers
total 108
-rw-------. 1 0   0  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 0   0  4096 Apr 24 09:10 data
drwxr-xr-x. 5 0   0  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 0   0     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 0   0 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 0   0  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 0 501  4096 Apr 28 09:48 test
drwxr-xr-x. 2 0 506  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Videos

If you want to list only Owner of the files and directories to which they are belongs and not groups then use Linux ls command with argument -G.

[root@localhost ~]# ls -lG   # Disable the Group Listing and only lists owners
total 108
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 5 root  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr 28 09:48 test
drwxr-xr-x. 2 root  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

ls command with -g will list the group of the files and directories to which they are belongs.

[root@localhost ~]# ls -g   # Lists groups to which they belongs
total 108
-rw-------. 1 root        2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root        4096 Apr 24 09:10 data
drwxr-xr-x. 5 root        4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root           0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root       41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root        9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 sales       4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Videos

ls command with -F will list the directories with a /at end. Refer the sample output below.

[root@localhost ~]# ls -lF   # Add's / at the end of the directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads/
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos/

For more reference on Linux ls related commands and it’s arguments you can run below commands in your linux system.

[root@localhost ~]# man help
[root@localhost ~]# ls --help
Related Post