ls command

ls Useful Command Examples For Beginner On Linux

ls is a most useful command in Linux. I think This would be the first command we do run on the Linux machine. We’re going to show you ls command with examples in Linux where you can learn it and use it on your machine.

Examples 1: List the files inside a directory using ‘ls command.
[[email protected] test]# touch file{1..3}
[[email protected] test]# cd ../
[[email protected] ~]# ls test/
file1 file2 file3
[[email protected] ~]#

Check Out: How To Start Monitoring For Elasticsearch Cluster On Linux

In a similar way, you can also see the directory and files.

Examples 2: List the content of files a particular directory using ‘ls -l’ 
command.  
[[email protected] ~]# ls -l test/
total 0
drwxr-xr-x 2 root root 6 Apr 1 07:51 book
-rw-r--r-- 1 root root 0 Apr 1 07:47 file1
-rw-r--r-- 1 root root 0 Apr 1 07:47 file2
-rw-r--r-- 1 root root 0 Apr 1 07:47 file3

The directory will be indicated with “d” before the permission as shown above.

Check Out: How To Set Date and Time In Linux Using Command Line

Examples 3: you can use the ‘r’ option to see the files and directory in reverse order.

[[email protected] ~]# ls -lr test/
total 0
-rw-r--r-- 1 root root 0 Apr 1 07:47 file3
-rw-r--r-- 1 root root 0 Apr 1 07:47 file2
-rw-r--r-- 1 root root 0 Apr 1 07:47 file1
drwxr-xr-x 2 root root 6 Apr 1 07:51 book

Examples 4: you can use the ‘-S’ option to check the files and directory. Suppose I have created a file using fallocate command of 200MB. I will show the files in reverse order means the largest file size will be displayed in the last. 

[[email protected] ~]# fallocate -l +100m test/abc.img
[[email protected] ~]# ls -lrS test/
total 102400
-rw-r--r-- 1 root root 0 Apr 1 07:47 file3
-rw-r--r-- 1 root root 0 Apr 1 07:47 file2
-rw-r--r-- 1 root root 0 Apr 1 07:47 file1
drwxr-xr-x 2 root root 6 Apr 1 07:51 book
-rw-r--r-- 1 root root 104857600 Apr 1 08:02 abc.img

Check Out: How To Create Clone of Existing Virtual Machine In VMWare

We can simply use the below command as well and you can see the difference.

[[email protected] ~]# ls -lS test/
total 102400
-rw-r--r-- 1 root root 104857600 Apr 1 08:02 abc.img
drwxr-xr-x 2 root root 6 Apr 1 07:51 book
-rw-r--r-- 1 root root 0 Apr 1 07:47 file1
-rw-r--r-- 1 root root 0 Apr 1 07:47 file2
-rw-r--r-- 1 root root 0 Apr 1 07:47 file3
Examples 5: we want to check the owner and group including the
permission. You can use ‘ls -ld’ command.
[[email protected] ~]# ls -ld test/
drwxr-xr-x 3 root root 67 Apr 1 08:02 test/

Examples 6: Now check the hidden files with ‘-a’ option.

[[email protected] ~]# cd test/
[[email protected] test]# touch .hidd
[[email protected] test]# ls
abc.img book file1 file2 file3
[[email protected] test]# cd
[[email protected] ~]# ls -a test/
. .. abc.img book file1 file2 file3 .hidd

Examples 7: List all the information about the particular directory using ‘ls -l’ command.

[[email protected] test]# ls -l
total 102400
-rw-r--r-- 1 root root 104857600 Apr 1 08:02 abc.img
drwxr-xr-x 2 root root 6 Apr 1 07:51 book
-rw-r--r-- 1 root root 0 Apr 1 07:47 file1
-rw-r--r-- 1 root root 0 Apr 1 07:47 file2
-rw-r--r-- 1 root root 0 Apr 1 07:47 file3

Check Out: How To Install PostgreSQL Server On Ubuntu 19.10

Examples 8: List all the file and directory size in human-readable using ‘ls -lh’ command.

[[email protected] test]# ls -lh
total 100M
-rw-r--r-- 1 root root 100M Apr 1 08:02 abc.img
drwxr-xr-x 2 root root 6 Apr 1 07:51 book
-rw-r--r-- 1 root root 0 Apr 1 07:47 file1
-rw-r--r-- 1 root root 0 Apr 1 07:47 file2
-rw-r--r-- 1 root root 0 Apr 1 07:47 file3

Examples 9: List all the files and directory with their inode number using the ‘-i’ option.

[[email protected] test]# ls -li
total 102400
33740087 -rw-r--r-- 1 root root 104857600 Apr 1 08:02 abc.img
33740082 drwxr-xr-x 2 root root 6 Apr 1 07:51 book
33740070 -rw-r--r-- 1 root root 0 Apr 1 07:47 file1
33740480 -rw-r--r-- 1 root root 0 Apr 1 07:47 file2
33740481 -rw-r--r-- 1 root root 0 Apr 1 07:47 file3

Check Out: Top 13 CP Command Useful Examples For Beginner On Linux

Examples 10: Display the current version of ls command with ‘–version‘ options.

[[email protected] test]# ls --version
ls (GNU coreutils) 8.22
Copyright (C) 2013 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.

Examples 11: List uid and gid using the ‘-n‘ option. it will display 0 because it’s created by root.

[[email protected] test]# ls -ln
total 102400
-rw-r--r-- 1 0 0 104857600 Apr 1 08:02 abc.img
drwxr-xr-x 2 0 0 6 Apr 1 07:51 book
-rw-r--r-- 1 0 0 0 Apr 1 07:47 file1
-rw-r--r-- 1 0 0 0 Apr 1 07:47 file2
-rw-r--r-- 1 0 0 0 Apr 1 07:47 file3

Check Out: How To Create bucket In S3 Storage On AWS Cloud

Examples 12: Enable timestamp using ‘–time-style‘ with the format.

[[email protected] test]# ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
total 102400
-rw-r--r-- 1 root root 104857600 2020-04-01 04-01 08:02 abc.img
drwxr-xr-x 2 root root 6 2020-04-01 04-01 07:51 book
-rw-r--r-- 1 root root 0 2020-04-01 04-01 07:47 file1
-rw-r--r-- 1 root root 0 2020-04-01 04-01 07:47 file2
-rw-r--r-- 1 root root 0 2020-04-01 04-01 07:47 file3

Examples 13: Display the files recursively using the ‘-R‘ option.

[[email protected] ~]# ls -R test/
test/:
abc.img book file1 file2 file3

test/book:
recer

Check Out: How To Create Automatic Backup of Volumes And EC2 On Amazon cloud

Examples 14: We can use the ‘-F‘ option to determine the type of file instead of ‘ls -l‘ command.

  • /= Directory
  • Nothing — normal file
  • @= link file
  • — Executable file
[[email protected] ~]# ls -F test/
abc.img book/ file1 file2 file3

You’re done

LEAVE A REPLY

Please enter your comment!
Please enter your name here