Shell script for ls command examples in Linux

ls (list) command in Linux is used to list the directory contents. It has more options to work with listing the directory contents. I had written a detailed article about ls (list) command examples in below article. Please check it to learn more about every option in ls command.

ls command examples in Linux

Here i have developed a shell script to demonstrate all the ls (list) command options in a single script. You can copy and execute this script to check all the options of ls commands with output. It is an addon script to demonstrate the ls command examples. If you want to check each and every command and practice then check this link for more details.

Steps to execute this script:

  • Create a file named ls_command_examples.sh in any of the path in the server.
  • Copy the below script to the file you create above and save it.
  • Change the file permission to 755 with below command.
$ chmod 755 ls_command_examples.sh
  • Execute the script with below command.
$ sh ls_command_examples.sh

Shell script for ls (list) command examples:

Below script will explain every option in ls (list) command in one place.

#!/bin/bash

#+------------------------------------------------------------------------------+
#| Name : ls_command_examples.sh                                                |
#| Author: www.linuxtec.co                                                      |
#| Date : 10_OCT_2022                                                           |
#| Purpose: Demonstrate all the ls (list) command examples in single script     |
#+------------------------------------------------------------------------------+

ls_commands=('ls -l' 'ls -la ' 'ls -A' 'ls -l --author' 'ls -l --block-size=M' 'ls -lB' 'ls -l --color=always' 'ls -l --color=never' 'ls -l --color=auto' 'ls -ld' 'ls -F' 'ls --file-type' 'ls -l --indicator-style=slash' 'ls -l --indicator-style=file-type' 'ls -l --indicator-style=classify' 'ls -p' 'ls --full-time' 'ls -g' 'ls -Gl' 'ls -o' 'ls --group-directories-first' 'ls -lh' 'ls -sh' 'ls --hide=*.txt' 'ls -l --hide=*.txt' 'ls --ignore=*.txt' 'ls -l --ignore=*.txt' 'ls --hide=*.cfg' 'ls -l --hide=*.cfg' 'ls --ignore=*.cfg' 'ls -l --ignore=*.cfg' 'ls -i' 'ls -li' 'ls -Ll' 'ls -m' 'ls -nl' 'ls --numeric-uid-gid' 'ls -Q' 'ls -Ql' 'ls --quote-name' 'ls -l --quote-name' 'ls -lr' 'ls -lR' 'ls -lS' 'ls -l --sort=none' 'ls -l --sort=time' 'ls -l --sort=size' 'ls -l --sort=extension' 'ls -l --sort=version' 'ls -l --time=atime' 'ls -l --time=access' 'ls -l --time=use' 'ls -l --time=ctime' 'ls -l --time=status' 'ls -l --time-style=full-iso' 'ls -l --time-style=long-iso' 'ls -l --time-style=iso' 'ls -l --time-style=locale' 'ls -l --time-style=+%H:%M' 'ls -lt' 'ls -lu' 'ls -ltu' 'ls -lU' 'ls -lX' 'ls -lZ' 'ls -l --context' 'ls --version')

for (( i=0; i<${#ls_commands[@]}; i++ ))
do
    echo "====================== ${ls_commands[$i]} ======================"
    data=`${ls_commands[$i]}`
    echo "$data"
    echo "===================================================="
    echo ""
done

Sample output for this script is below.

[root@localhost ~]# sh ls_command_examples.sh

====================== ls -l ======================
total 20
-rw-------. 1 root root 1032 Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Documents
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1431 Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1859 Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Music
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Public
drwxr-xr-x. 2 root root    6 Oct 10 05:22 scripts
drwxr-xr-x. 2 root root    6 Oct 10 02:11 Student_data1
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Templates
-rw-r--r--. 1 root root  134 Oct  5 06:55 test_1.txt
drwxrwxrwx. 2 root root    6 Sep 30 12:32 test_2
drwxr-xr-x. 4 root root   36 Sep 26 12:36 test_data
-rw-r--r--. 1 root root  187 Oct  5 12:38 test_file.txt
-rw-r--r--. 1 root root    0 Oct  8 02:07 test.py
drwxr-xr-x. 2 root root    6 Sep 21 05:04 Videos
drwxr-xr-x. 3 root root   16 Sep 26 12:42 youu
====================================================

====================== ls -l --block-size=M ======================
total 1M
-rw-------. 1 root root 1M Sep 21 04:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Desktop
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Documents
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Downloads
-rw-r--r--. 1 root root 1M Sep 21 05:02 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root 1M Oct 10 05:51 ls_command_data.sh
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Music
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Pictures
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Public
drwxr-xr-x. 2 root root 1M Oct 10 05:22 scripts
drwxr-xr-x. 2 root root 1M Oct 10 02:11 Student_data1
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Templates
-rw-r--r--. 1 root root 1M Oct  5 06:55 test_1.txt
drwxrwxrwx. 2 root root 1M Sep 30 12:32 test_2
drwxr-xr-x. 4 root root 1M Sep 26 12:36 test_data
-rw-r--r--. 1 root root 1M Oct  5 12:38 test_file.txt
-rw-r--r--. 1 root root 0M Oct  8 02:07 test.py
drwxr-xr-x. 2 root root 1M Sep 21 05:04 Videos
drwxr-xr-x. 3 root root 1M Sep 26 12:42 youu
====================================================

Leave a Comment