ls Useful Command Examples For Beginner On Linux

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.
[root@command test]# touch file{1..3}
[root@kcommand test]# cd ../
[root@command ~]# ls test/
file1 file2 file3
[root@command ~]#

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.  
[root@command ~]# 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.

[root@command ~]# 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. 

[root@command ~]# fallocate -l +100m test/abc.img
[root@command ~]# 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.

[root@command ~]# 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.
[root@command ~]# 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.

[root@command ~]# cd test/
[root@command test]# touch .hidd
[root@command test]# ls
abc.img book file1 file2 file3
[root@command test]# cd
[root@command ~]# ls -a test/
. .. abc.img book file1 file2 file3 .hidd

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

[root@command 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.

[root@command 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.

[root@command 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.

[root@command 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.

[root@command 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.

[root@command 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.

[root@command ~]# 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
[root@command ~]# ls -F test/
abc.img book/ file1 file2 file3

You’re done

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment