Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

ls command in Linux/Unix

Ls Command In Linux/Unix

ls command

   
ls is a Linux shell command that lists directory contents of files and directories.

ls syntax

   
$ ls [options] [file|dir]
   

ls command options

   
          In computing, ls is a command to list files in Unix and Unix-like operating systems. ls 
is specified by POSIX and the Single UNIX Specification. It is one of the the most frequently
 used command in unix.

Here’s the listing of example usage of “ls” command:

1. To list directory contents:

root@kali:~#  ls
1.txt  2.txt hello  sample

2. To display One File Per Line (ls -1):

root@kali:~#  ls -1
1.txt
2.txt
hello
Sample

3. To display total information about Files/Directories(ls -l):

root@kali:~#  ls -l
total 8
-rw-rw-r-- 2 Root Root    12 Jul 1 23:11 1.txt
-rw-rw-r-- 2 root root    0 Jul 1 23:11 2.txt
drwxrwxr-x 2 root root 4096  Jul 6 19:25 hello
drwxrwxr-x 2 root root 4096  Jul 6 19:25 sample

4. To show all the hidden files(ls -a OR ls -A):

root@kali:~#  ls -a
.
..
Ada-payment                 .goutputstream-4HV4PW
Backups                     .goutputstream-4TTDSW
.bash_history                Fedora-Info.txt
.bash_logout                 .lftp
.bash_profile                libiconv-1.11.tar.tar

Here “.” means current directory and “..” means parent directory are shown.So if you don’t want 
them to display use “-A” option.

root@kali:~#  ls -A
Ada-payment                 .goutputstream-4HV4PW
Backups                     .goutputstream-4TTDSW
.bash_history                Fedora-Info.txt
.bash_logout                 .lftp
.bash_profile                libiconv-1.11.tar.tar

6. To display files Recursively (ls -R):

root@kali:~#  ls -R SAN/
SAN/:
1.txt  2.txt hello  sample

SAN/hello:

SAN/sample:

Here all the contents of the directory SAN are shown.

7. To display file size in human readable form(ls -lh file/directory name):

Here the size is displayed in the readable format. K stands for KB , M for MB , G for GB.

root@kali:~#  ls -lh fixing
-rw-rw-r-- 1 root root 291 Jul  6 01:36 fixing

8. To order files based on last modified time(ls -lt):

root@kali:~#  ls -lt
total 12
-rw-rw-r-- 1 root root    0 Jul 7 00:47 xyz
-rw-rw-r-- 1 root root    12 Jul 7 00:47 him
drwxrwxr-x 2 root root    4096 Jul 7 00:46 sample.txt
drwxrwxr-x 2 root root    4096 Jul 6 19:25 hello
drwxrwxr-x 2 root root    4096 Jul 6 19:25 sample
-rw-rw-r-- 2 root root    10 Jul 1 23:11 1.txt
-rw-rw-r-- 2 root root    20 Jul 1 23:11 2.txt

9. To Order Files Based on Last Modified Time in Dec-ending Order(ls -lrt):

root@kali:~#  ls -lrt
total 12
-rw-rw-r-- 2 root root    0 Jul 1 23:11 2.txt
-rw-rw-r-- 2 root root    0 Jul 1 23:11 1.txt
drwxrwxr-x 2 root root 4096 Jul  6 19:25 sample
drwxrwxr-x 2 root root 4096 Jul  6 19:25 hello
drwxrwxr-x 2 root root 4096 Jul  7 00:46 sample.txt
-rw-rw-r-- 1 root root    0 Jul 7 00:47 him
-rw-rw-r-- 1 root root    0 Jul 7 00:47 xyz

10. To Stream output format; files are listed across the page, separated by commas(ls -m):

root@kali:~#  ls -m
1.txt, 2.txt, hello, him, sample, sample.txt, xyz

11. To List all subdirectories(ls *):

root@kali:~#  ls *
1.txt  2.txt him  xyz

hello:

sample:

sample.txt:

12. To Display File Inode Number(ls -i):

root@kali:~#  ls -i -1
592260 1.txt
592260 2.txt
612447 hello
543095 him
612445 sample
612009 sample.txt
543097 xyz

Here “-1” is added as option to print the inode numbers one in a line.

13. To sort files with Size(ls -S):

root@kali:~#  ls -lS
total 12
drwxrwxr-x 2 root root 4096 Jul  6 19:25 hello
drwxrwxr-x 2 root root 4096 Jul  6 19:25 sample
drwxrwxr-x 2 root root 4096 Jul  7 00:46 sample.txt
-rw-rw-r-- 2 root root    20 Jul 1 23:11 1.txt
-rw-rw-r-- 2 root root    10 Jul 1 23:11 2.txt
-rw-rw-r-- 1 root root    5 Jul 7 00:47 him
-rw-rw-r-- 1 root root    0 Jul 7 00:47 xyz

14. To list directories only(ls -d */):

root@kali:~#  ls -d */
hello/  sample/ sample.txt/

Note


This post first appeared on LOVE TO LEARN AND FUN, please read the originial post: here

Share the post

ls command in Linux/Unix

×

Subscribe to Love To Learn And Fun

Get updates delivered right to your inbox!

Thank you for your subscription

×