20 ls command usages and examples – Linux

ls – The linux command

ls command is one of the most frequently used command in Linux. Even-though, we may not use all the options available but here I am explaining the different options of ls command.

The command “ls” should be the most frequently using command in everyday system operations. This command has a lot of options to help operations via command line. In this article we are trying to explain those options in details with examples.

1. List all the contents in a directory

# ls

Example:

# ls
file1  file2  file3  file4  file5  simplyadir

2. Lists files with option -l (Long list)

#ls -l

Example:

# ls -l
total 24
-rw-r--r-- 1 root root   42 2014-01-29 09:25 file1
-rw-r--r-- 1 root root   42 2014-01-29 09:25 file2
-rw-r--r-- 1 root root   42 2014-01-29 09:37 file3
-rw-r--r-- 1 root root   42 2014-01-29 09:28 file4
-rw-r--r-- 1 root root   42 2014-01-29 09:28 file5
drwxr-xr-x 4 root root 4096 2014-01-29 11:49 simplyadir

3. Sort files by file size (S)

# ls -lS
total 24K
drwxr-xr-x 4 root root 4.0K 2014-01-29 11:49 simplyadir
-rw-r--r-- 1 root root   42 2014-01-29 09:25 file1
-rw-r--r-- 1 root root   42 2014-01-29 09:25 file2
-rw-r--r-- 1 root root   42 2014-01-29 09:37 file3
-rw-r--r-- 1 root root   42 2014-01-29 09:28 file4
-rw-r--r-- 1 root root   42 2014-01-29 09:28 file5

4. Shows version of ls command

# ls –version

# ls --version
ls (GNU coreutils) 8.5
Copyright (C) 2010 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.

5. The command orders the files in the current directory in the last modified format

# ls -lt

# ls -lt
-rw-r--r-- 1 root root 42 2014-01-29 09:37 file3
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file5
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file4
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file2
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file1

6. How to open the last edited file using ls -t

# vi `ls -t | head -1`

[It will open the last edited file, for me it is file3]

7. Displays single file per line

# ls -1

Example:

# ls -1
file1
file2
file3
file4
file5

8. Display all information about file/directory

Long-listing information about a file/directory using ls -l

#ls -l

-rw-r--r-- 1 arun techtips 42 2014-01-29 09:25 file1

Field Explanation

First character indicates the file type. The hyphen (-) in the first character indicates that it’s a normal file

The other possibilities for first character:

d – directory
l – symbolic link
s – socket
p – named pipe
b – block device file special
c – character device file special

The next 9 characters in the first field indicates file’s permission

rw-    => Indicates the user permission. In this example, the user has only read and write permission.
r–      => Indicates the group permission. Here, the group has only read permission
r–      => Indicates the other permission. Only read permission for other

The second field indicates the number of links. In the example mentioned above, 1 indicates there is only 1 link to the file

The third field indicates the owner of the file. Here, the file is owned by the user “arun”.

The fourth field indicates the group of the file. Here the file is in the group “techtips”.

The fifth field indicates the size of the file. 42 in the example indicates the file size.

The sixth field indicates the last modified date and time of the file. “2014-01-29 09:25” indicates that the file was las t modified on January 29, 2014 at 9.25AM

The last field is the name of the file. Here “file1” is the file name.

9. Display the file size in a human readable format

That is , the file size will be displayed as M for MB, G for GB and K for KB

# ls -lh filename

Example:

# ls -l file
-rw-rw-r-- 1 arun techtips 1909 2013-08-12 10:12 file

# ls -lh file
-rw-rw-r-- 1 arun techtips 1.9K 2013-08-12 10:12 file

10. Display directory information

#ls -ld

Using ls -l displays the contents inside a directory but to display the information of a directory ls -ld command can be used.

Example:

# ls -l test/
total 20
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file1
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file2
-rw-r--r-- 1 root root 42 2014-01-29 09:37 file3
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file4
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file5
# ls -ld test/
drwxr-xr-x 2 root root 4096 2014-01-29 09:42 test/

11. Orders the files in the current directory in the reverse order of last modified time

# ls -ltr

Example:

# ls -ltr
total 20
-rw-r--r--  1 root root 865 Sep 20 2011 myfile.txt
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file1
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file2
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file4
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file5
-rw-r--r-- 1 root root 42 2014-01-29 09:37 file3

12. Display hidden file using ls -a or ls -A

#ls -a

In Linux, hidden files starts with ‘.’
ls -a displays all hidden files icluding ‘.’ (current directory) and ‘..’ (parent directory)
ls -A displays the hidden files other than ‘.’ and ‘..’

Example:

# ls -al
total 28
drwxr-xr-x  2 root root 4096 2014-01-29 09:42 .
drwxr-xr-x 38 root root 4096 2014-01-29 09:19 ..
-rw-r--r--  1 root root   42 2014-01-29 09:25 file1
-rw-r--r--  1 root root   42 2014-01-29 09:25 file2
-rw-r--r--  1 root root   42 2014-01-29 09:37 file3
-rw-r--r--  1 root root   42 2014-01-29 09:28 file4
-rw-r--r--  1 root root   42 2014-01-29 09:28 file5
-rw-r--r--  1 root root  0 2014-01-29 11:43 .test
# ls -Al
total 20
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file1
-rw-r--r-- 1 root root 42 2014-01-29 09:25 file2
-rw-r--r-- 1 root root 42 2014-01-29 09:37 file3
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file4
-rw-r--r-- 1 root root 42 2014-01-29 09:28 file5
-rw-r--r-- 1 root root  0 2014-01-29 11:43 .test

13. Display files recursively

# ls -R

Example:

root@server:~# ls -l /root/test/simplyadir
total 8
drwxr-xr-x 2 root root 4096 2014-01-29 11:49 dir1
drwxr-xr-x 2 root root 4096 2014-01-29 11:50 dir2
root@server:~# ls -R /root/test/simplyadir
/root/test/simplyadir:
dir1  dir2

/root/test/simplyadir/dir1:
1.txt  2.txt

/root/test/simplyadir/dir2:
abc  xyz

14. To find the inode number

# ls -li

Example:

# ls -li file1
1990243 -rw-r--r-- 1 root root 42 2014-01-29 09:25 file1
# ls -li simplyadir
total 8
1995748 drwxr-xr-x 2 root root 4096 2014-01-29 11:49 dir1
1995749 drwxr-xr-x 2 root root 4096 2014-01-29 11:50 dir2

15. Prints question mark (?) instead of non graphic controllers

# ls -q

16. Display UID and GID instead of names

# ls -n

Example:

# ls -n
total 24
-rw-r--r-- 1 0 0   42 2014-01-29 09:25 file1
-rw-r--r-- 1 0 0   42 2014-01-29 09:25 file2
-rw-r--r-- 1 0 0   42 2014-01-29 09:37 file3
-rw-r--r-- 1 0 0   42 2014-01-29 09:28 file4
-rw-r--r-- 1 0 0   42 2014-01-29 09:28 file5
drwxr-xr-x 4 0 0 4096 2014-01-29 11:49 simplyadir

17. Visual classification of files with special characters

# ls -F
file1 file2 file3 file4 file5 simplyadir/ testing@

In the above output, / in the end represents it ‘s a directory
If nothing in the end means it’s a normal file
@ indicates a link file
* indicates an executable file

18. Show help page

# ls --help

19. ls command and its aliases

We have made alias for ls command, when we execute ls command it will take -l option by default and display long listing as mentioned earlier:

# alias ls="ls -l"

Long list the file with size in human understandable form

# alias ll="ls -lh"

Classify the file type by appending special characters

# alias lv="ls -F"

20. To remove an alias

# unalias ls

Thanks.. 🙂

More:
groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab

What is iptables in Linux? Introduction to iptables

We can call, it’s the basics of Firewall for Linux. Iptables is a rule based firewall system and it is normally pre-installed on a Unix operating system which is controlling the incoming and outgoing packets. By-default the iptables is running without any rules, we can create, add, edit rules into it.

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

4 thoughts on “20 ls command usages and examples – Linux

Leave a Reply

Your email address will not be published. Required fields are marked *