8 Basic use of ls or list command

The list/ls command is one of the most frequently used commands in Linux. In this article, we’ll be discussing a basic list or ls command.

To understand this we need to create a directory name HelloWorld. Inside this directory need to create a directory and file.


mkdir HelloWorld
cd HelloWorld
touch index.html #creating file
mkdir js

1. List files using ls with no option

Without an option, we won’t be able to view details like file types, size, modified date and time, permission and links etc.


ls
index.html js

2. List files with option –l

With -l option we would be able to view details like file types, size, modified date and time, permission and links etc.


ls -l
total 0
-rw-r--r--  1 wayofwebs  staff   0 Sep 17 15:30 index.html
drwxr-xr-x  2 wayofwebs  staff  64 Sep 17 15:30 js

3. List files with option –a or view hidden files

With -a option, we would be able to View Hidden Files including hidden files starting with ‘.’.


ls -a
.          ..         index.html js

4. List Files with Human Readable Format with option -lh

With a combination of -lh option, we can show sizes in a human-readable format.


ls -lh
-rw-r--r--  1 wayofwebs  staff     0B Sep 17 15:30 index.html
drwxr-xr-x  2 wayofwebs  staff    64B Sep 17 15:30 js

5. List Files and Directories with ‘/’ Character at the end

Using -F option with ls command will add the ‘/’ Character at the end of each directory.


ls -F
index.html  js/

6. List Files in Reverse Order


ls -r
js         index.html

7. Recursively list Sub-Directories

ls -R option will list very long listing directory trees.


ls -R
index.html js
./js:

8. Sort Files by File Size

With a combination of -lS displays file size in order, will display big in size first.


ls -lS
total 0
drwxr-xr-x  2 wayofwebs  staff  64 Sep 17 15:30 js
-rw-r--r--  1 wayofwebs  staff   0 Sep 17 15:30 index.html

You May Also Like

About the Author: Pankaj Bisht