15 Practical examples of ls command

Linux ls command is a basic command which is essential to know for all Linux users. It is used to list information of files and directories within a file system.

To use ls command, you don’t need to install any special package in your Linux machine. It is a part of GNU core utility package which is come with all distro of Linux.

The ls is very common utility, where you can’t imagine your work without using this utility. If you start working on Linux machine, be leave me within 60 min you will use this ls command uncountable numbers of time.

In this tutorial article, we will show you how to use ls command by using several examples of ls command.

ls command syntax:
ls -[option]

Suggested Reading: Linux sed command Structure

Examples:

Example 1: List all files in Linux

The “ls” command is use to list all files and directory. You can use the “ls” command without any option to list all files and directories in the current location.

# ls

Output:

root@linuxconcept:/home/satish# ls
backup  backup-copy.sh  backup.sh  jokes.sh  linuxconcept-05-Dec-2019.zip  oldserver.zip  test

Example 2: Use ls command with option -l

You can use the “-l” option with “ls” command to display file or directory, size, modified date and time, owner or files and permission on the file, as shown below:

# ls -l

Output:

root@linuxconcept:/home/satish# ls -l
total 1814116
drwxr-xr-x 3 root   root        4096 Dec 16 00:10 backup
-rwxr-xr-x 1 root   root         168 Nov 20 17:50 backup-copy.sh
-rwxrwxrwx 1 root   root        3440 Apr 21  2019 backup.sh
-rwxrwxrwx 1 root   root        2362 Apr  5  2019 jokes.sh
-rw-r--r-- 1 satish satish 900346420 Dec  5 12:23 linuxconcept-05-Dec-2019.zip
-rw-r--r-- 1 satish satish 957272306 Dec  4 10:23 oldserver.zip
drwxr-xr-x 4 satish satish      4096 Aug 25 01:34 test

Example 3: Use ls to view hidden files

You can use the “ls” command to check hidden files or directories by using “-a” option, as shown below:

# ls -a

Output:

root@linuxconcept:/home/satish# ls -a
.   backup          backup.sh      .bash_logout  .cache    linuxconcept-05-Dec-2019.zip  .profile                   test
..  backup-copy.sh  .bash_history  .bashrc       jokes.sh  oldserver.zip                 .sudo_as_admin_successful  .viminfo

Example 4: Get ls command output in Human Readable format

You can use “-lh” option with the “ls” command to get the size of files and directories in a human-readable format, as shown below:

# ls -lh

Output:

root@linuxconcept:/home/satish# ls -lh
total 1.8G
drwxr-xr-x 3 root   root   4.0K Dec 16 00:10 backup
-rwxr-xr-x 1 root   root    168 Nov 20 17:50 backup-copy.sh
-rwxrwxrwx 1 root   root   3.4K Apr 21  2019 backup.sh
-rwxrwxrwx 1 root   root   2.4K Apr  5  2019 jokes.sh
-rw-r--r-- 1 satish satish 859M Dec  5 12:23 linuxconcept-05-Dec-2019.zip
-rw-r--r-- 1 satish satish 913M Dec  4 10:23 oldserver.zip
drwxr-xr-x 4 satish satish 4.0K Aug 25 01:34 test

Example 5: List files and Directories with ‘/’ Character at the end

When you use the “-F” option with the “ls” command, it will add the “/” character at the end of each directory.

# ls -F

Output:

root@linuxconcept:/home/satish# ls -F
backup/  backup-copy.sh*  backup.sh*  jokes.sh*  linuxconcept-05-Dec-2019.zip  oldserver.zip  test/

Example 6: List Linux files in Reverse Order

The “ls” command with the “-r” option use to display files and directories in reverse order.

# ls -r

Output:

root@linuxconcept:/home/satish# ls -r
test  oldserver.zip  linuxconcept-05-Dec-2019.zip  jokes.sh  backup.sh  backup-copy.sh  backup

Example 7: List out Sub-Directories recursively

You can display sub-directories in a recursive list by using the “ls” command with the “-R” option, as shown below:

# ls -R

Output:

root@linuxconcept:/home/satish# ls -R
.:
backup  backup-copy.sh  backup.sh  jokes.sh  linuxconcept-05-Dec-2019.zip  oldserver.zip  test
./backup:
2019-12-16  quotes-16-12-2019-00-10.sql
./backup/2019-12-16:
secure.zip  database.zip  concept.zip  india.zip  linux.zip  mech.zip  media.zip  poetry.zip  skill.zip
./test:
a  z
./test/a:
abc.txt
./test/z:
abc.txt

Example 8: Reverse ls command output order

You can use the “-ltr” option with ls command to show the latest modification file or directory date as last, as shown below:

# ls -ltr

Output:

root@linuxconcept:/home/satish# ls -ltr
total 1814116
-rwxrwxrwx 1 root   root        2362 Apr  5  2019 jokes.sh
-rwxrwxrwx 1 root   root        3440 Apr 21  2019 backup.sh
drwxr-xr-x 4 satish satish      4096 Aug 25 01:34 test
-rwxr-xr-x 1 root   root         168 Nov 20 17:50 backup-copy.sh
-rw-r--r-- 1 satish satish 957272306 Dec  4 10:23 oldserver.zip
-rw-r--r-- 1 satish satish 900346420 Dec  5 12:23 linuxconcept-05-Dec-2019.zip
drwxr-xr-x 3 root   root        4096 Dec 16 00:10 backup

Example 9: Sort ls command output with File Size

You can sort the ls command output by the size of files and big size file display first by using the “-ls” option, as shown below:

# ls -ls

Output:

root@linuxconcept:/home/satish# ls -lS
total 1814116
-rw-r--r-- 1 satish satish 957272306 Dec  4 10:23 oldserver.zip
-rw-r--r-- 1 satish satish 900346420 Dec  5 12:23 linuxconcept-05-Dec-2019.zip
drwxr-xr-x 3 root   root        4096 Dec 16 00:10 backup
drwxr-xr-x 4 satish satish      4096 Aug 25 01:34 test
-rwxrwxrwx 1 root   root        3440 Apr 21  2019 backup.sh
-rwxrwxrwx 1 root   root        2362 Apr  5  2019 jokes.sh
-rwxr-xr-x 1 root   root         168 Nov 20 17:50 backup-copy.sh

Example 10: Display inode number of file using ls

You can also find the inode number of file and directory by using ls command with the “-i” option, as shown below:

# ls -i

Output:

root@linuxconcept:/home/satish# ls -i
4589076 backup  3934403 backup-copy.sh  1979010 backup.sh  3934233 jokes.sh  3932171 linuxconcept-05-Dec-2019.zip  3932170 oldserver.zip  4063651 test

Example 11: List Directory information

You can use the path of a directory with ls command to get the information of the directory to list out the files and directory available with a given directory, as shown in the below example to display files and directory.

# ls -l /tmp

Output:

root@linuxconcept:/home/satish# ls -l /tmp
total 20
-rw-rw-rw- 1 www-data www-data    0 Jun 28 13:01 comet-cache-1975936.lock
-rw-rw-rw- 1 www-data www-data    0 Jun 28 13:26 comet-cache-2104323.lock
-rw-r--r-- 1 www-data www-data 6803 Feb 20  2019 connection-test-image-wUd4Ih.tmp
drwx------ 3 root     root     4096 Dec 11 20:30 systemd-private-3aa507869c9f4439bdd37883000913de-apache2.service-qeBbcM
drwx------ 3 root     root     4096 Jul  2 20:21 systemd-private-3aa507869c9f4439bdd37883000913de-systemd-timesyncd.service-Ibw28g
drwx------ 2 root     root     4096 Dec 16  2018 vmware-root

Example 12: Display UID and GID of files using ls

You can display UID and GID of files and directories using the “-n” option with ls command.

# ls -n

Output:

root@linuxconcept:/home/satish# ls -n
total 1814116
drwxr-xr-x 3    0    0      4096 Dec 16 00:10 backup
-rwxr-xr-x 1    0    0       168 Nov 20 17:50 backup-copy.sh
-rwxrwxrwx 1    0    0      3440 Apr 21  2019 backup.sh
-rwxrwxrwx 1    0    0      2362 Apr  5  2019 jokes.sh
-rw-r--r-- 1 1000 1000 900346420 Dec  5 12:23 linuxconcept-05-Dec-2019.zip
-rw-r--r-- 1 1000 1000 957272306 Dec  4 10:23 oldserver.zip
drwxr-xr-x 4 1000 1000      4096 Aug 25 01:34 test

Example 13: ls command and its Aliases

You can have made an alias for ls command when you execute ls command; it will take the “-l” option by default and display long listing as mentioned earlier.

# alias ls="ls -l"

Output:

root@linuxconcept:/home/satish# alias ls="ls -l"
root@linuxconcept:/home/satish# ls
total 1814116
drwxr-xr-x 3 root   root        4096 Dec 16 00:10 backup
-rwxr-xr-x 1 root   root         168 Nov 20 17:50 backup-copy.sh
-rwxrwxrwx 1 root   root        3440 Apr 21  2019 backup.sh
-rwxrwxrwx 1 root   root        2362 Apr  5  2019 jokes.sh
-rw-r--r-- 1 satish satish 900346420 Dec  5 12:23 linuxconcept-05-Dec-2019.zip
-rw-r--r-- 1 satish satish 957272306 Dec  4 10:23 oldserver.zip
drwxr-xr-x 4 satish satish      4096 Aug 25 01:34 test

You can see the number of alias available in your system by using the “alias” command, as shown below:

# alias

Output:

root@linuxconcept:/home/satish# alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls -l'

To remove previously defined an alias, use the unalias command as shown below:

# unalias ls

Output:

root@linuxconcept:/home/satish# unalias ls
root@linuxconcept:/home/satish# ls
backup  backup-copy.sh  backup.sh  jokes.sh  linuxconcept-05-Dec-2019.zip  oldserver.zip  test

Example 14: Show version of ls command

You can check the installed “ls” command utility version using the “–version” option, as shown below:

# ls --version

Output:

root@linuxconcept:/home/satish# ls --version
ls (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Richard M. Stallman and David MacKenzie.

Example 15: Show ls help page

Check the help page for the “ls” command using the “–help” option.

# ls --help

Output:

root@linuxconcept:/home/satish# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and/or -s, print human readable sizes
                               (e.g., 1K 234M 2G)
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage
  -l                         use a long listing format
  -L, --dereference          when showing file information for a symbolic
                               link, show information for the file the link
                               references rather than for the link itself
  -m                         fill width with a comma separated list of entries
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print raw entry names (don't treat e.g. control
                               characters specially)
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -s, --size                 print the allocated size of each file, in blocks
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u);
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time (newest first)
      --time-style=STYLE     with -l, show times using style STYLE:
                               full-iso, long-iso, iso, locale, or +FORMAT;
                               FORMAT is interpreted like in 'date'; if FORMAT
                               is FORMAT1<newline>FORMAT2, then FORMAT1 applies
                               to non-recent files and FORMAT2 to recent files;
                               if STYLE is prefixed with 'posix-', STYLE
                               takes effect only outside the POSIX locale
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid '\n' with -q or -b
      --help     display this help and exit
      --version  output version information and exit
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.
Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
root@linuxconcept:/home/satish#

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Related Articles