AMP AMP

ls Command in Linux with Examples

ls Command

ls command is most frequently used in Linux system. ls command is used for list information about files in the current working directory by default. ls command is one of the default shell command in Linux.

Syntax

ls < option>

Display the all files in working directory

This command displays all the files and directory in working directory including hidden files.

Example

[user1@linuxhelp Desktop]$ ls 
dir1 dir2 dir3 my.txt

Do not display the backup file

This command displays almost all files and directory. But do not list starting with “ .” and “ ...” files.

Example

[user1@linuxhelp Desktop]$ ls -B 
dir1 dir2 dir3 my.txt

Display the hidden files

Example

[user1@linuxhelp Desktop]$ ls -a 
.    dir1    dir2        .~lock.date.command.odt#    my.txt
..    dir2    .hide.txt    .~lock.untitled 1.odt#    

Display the files with color

This command will display you the file and directory in black color. Here we currently work in user home directory.

Example

[user1@linuxhelp Desktop]$ ls --color=never 
dir1 dir2 dir3 my.txt

Display the last changing directory

This command will show you last changing file and directory with ctime and sort by. Using with -lt it shows long listing and sort by time and date.

Example

[user1@linuxhelp Desktop]$ ls -ltc 
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir2
-rw-rw-r-- 1     User1 User1     1370    Feb 25 01:18  my.txt
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir3
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir1

Display the file and directory in working directory

This command will show the directory and files which presented in current working directory.

Example

[user1@linuxhelp Desktop]$ ls -D * 
my.txt
dir1:
my2.txt    my3.txt    my.txt
dir2:
a1    a2    a3    file.txt    kk.txt  mydir
dir3:
b1    b2    b3    file.txt

Display the long list file

ls command will show you long list it means it shows who is owner of the file and group and file size and ctime.

Example

[user1@linuxhelp Desktop]$ ls  -l 
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir1
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir2
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir3
-rw-rw-r-- 1        User1 User1     1370    Feb 25 01:18  my.txt

Display the inode number of the file

ls command will show you files and directory inode number in working directory.

Example

[user1@linuxhelp Desktop]$ ls  -i 
529387 dir1 529388 dir2 529389 dir3 521322 my.txt

Display the files in sort by file size

This command will display the sorted file by file size. The bigger file will be displayed at the first.

Example

[user1@linuxhelp Desktop]$ ls  -lS 
total 28
-rw-rw-r-- 1        User1 User1     10947   Feb 25 01:18  text.txt
drwxrwxr-x 1        User1 User1     4096    Feb 25 01:18  dir1
drwxrwxr-x 1        User1 User1     4096    Feb 25 01:18  dir2
drwxrwxr-x 1        User1 User1     4096    Feb 25 01:18  dir3
-rw-rw-r-- 1        User1 User1     1370    Feb 25 01:18  my.txt

Display the file size in human readable

ls command will shows you long list with file size and file size human readable it means file size in KB size.

Example

[user1@linuxhelp Desktop]$ ls -lh 
drwxrwxr-x 1     User1 User1     4.0K    Feb 25 01:18  dir1
drwxrwxr-x 1     User1 User1     4.0K    Feb 25 01:18  dir2
drwxrwxr-x 1     User1 User1     4.0K    Feb 25 01:18  dir3
-rw-rw-r-- 1     User1 User1     1.4K    Feb 25 01:18  my.txt
-rw-rw-r-- 1     User1 User1      11K    Feb 25 01:18  text.txt

Display the files in reverse order

ls command will display the files and directory in reverse order. Now you can see the difference between the normal order and reverse order.

Example

[user1@linuxhelp Desktop]$ ls  -rl 
-rw-rw-r-- 1        User1 User1     10947   Feb 25 01:18  text.txt
-rw-rw-r-- 1        User1 User1     1370    Feb 25 01:18  my.txt
drwxrwxr-x 1        User1 User1     4096    Feb 25 01:18  dir3
drwxrwxr-x 1        User1 User1     4096    Feb 25 01:18  dir2
drwxrwxr-x 1        User1 User1     4096    Feb 25 01:18  dir1

Display recursively file and sub files

This command will display the recursive file and it contains sub directory and it files. With -l long listing the file.

Example

[user1@linuxhelp Desktop]$ ls -lR 
total 28
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir1
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir2
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir3
-rw-rw-r-- 1     User1 User1     1370    Feb 25 01:18  my.txt
-rw-rw-r-- 1     User1 User1     10947   Feb 25 01:18  text.txt
./dir1:
total 32
-rw-rw-r-- 1     User1 User1     9864    Feb 25 01:18  file1.txt
-rw-rw-r-- 1     User1 User1     7422    Feb 25 01:18  file2.txt
.
.
-rw-rw-r-- 1     User1 User1       217   Feb 25 00:57 file.txt

Display the file sort by time and date

This command will display the list you file and directory in sort by time and date type. It means which file or directory will lastly create it is first in the list. With -l using it shows you long listing.

Example

[user1@linuxhelp Desktop]$ ls  -t 
-rw-rw-r-- 1     User1 User1     10947   Feb 25 01:45  text.txt
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:43  dir1
drwxrwxr-x 1     User1 User1     4096    Feb 25 01:18  dir2
-rw-rw-r-- 1     User1 User1     1370    Feb 25 01:09  my.txt
drwxrwxr-x 1     User1 User1     4096    Feb 25 00:57  dir3

Display the absolute path file

This command helps to view the absolute path and their list file and directory.

Example

[user1@linuxhelp Desktop]$ ls /home/Desktop 
Desktop    example.txt    Music      Templates    test.txt~   test4.txt
Documents  file.mp3    Picture   test1            test2.txt   Videos
Downloads  files        Public    test1.txt     test3.txt

Display list user home directory

This command will show you the current logged in user home directory.

Example

[user1@linuxhelp Desktop]$ ls  -l  ~ 
drwxr-xr-x 4 user1 user1 4096 Feb  25 01:45 Desktop
drwxr-xr-x 2 user1 user1 4096 Feb  18 12:22 Documents
drwxr-xr-x 2 user1 user1 4096 Feb  16 04:14 Downloads
.
.
drwxr-xr-x 2 user1 user1 4096 Feb  8 10:10 Videos

Display the files and directories with ‘ /’ character at the end

Using option &ndash F with ls command, will add the ‘ /’ Character at the end each directory.

Example

[user1@linuxhelp Desktop]$ ls  -F 
dir1/    dir2/    dir3/    my.txt  text.txt

Display the version of ls command

Using options --version to check what kind of version using in our ls commands.

Example

[user1@linuxhelp Desktop]$ ls --version 
ls (GNU coreutils) 8.4
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.

Display UID and GID of Files using ls commands

Using option -n with ls command it will display the UID and GID of the user and group.

Example

[user1@linuxhelp Desktop]$ ls -n 
drwxrwxr-x 2     509  514     4096    Feb 25 01:18  dir1
drwxrwxr-x 3     509  514     4096    Feb 25 01:18  dir2
drwxrwxr-x 2     509  514     4096    Feb 25 01:18  dir3
-rw-rw-r-- 1     509  514     1370    Feb 25 01:18  my.txt
-rw-rw-r-- 1     509  514     10947   Feb 25 01:18  text.txt

Display directory information using ls command

Using ls -l /etc/ command list files under directory /etc. Where -ld option displays information of /etc directory.

Example

[user1@linuxhelp Desktop]$ ls -l /etc/ 
total 2020
drwxr-xr-x.    3 root  root        4096    Jan    14 08:23  abrt
drwxr-xr-x.    4 root  root        4096    Jan    14 08:27  acpi
-rw-r&mdash r--.      1 root  root          51    Feb      1 01:23  adjtime
.
.
-rw-r&mdash r--.   1 root  root            51  Jul        24 2015  yum.conf
drwxr-xr-x.    3 root  root        4096    Feb        3  13:41 yum.repos.d

[user1@linuxhelp Desktop]$ ls &ndash ld /etc/ 
drwxr-xr-x.    116 root  root    12288     Feb    24 17:28  /etc/

To display the help page for ls commands

Using -help option in ls command it will show helper guide with examples.

Example

[user1@linuxhelp Desktop]$ ls --help 
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all               do not ignore entries starting with .
  -A, --almost-all        do not list implied. And..
  --author                with -l, print the author of each file
  --help                  display this help and exit
  --version               output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

By default, color is not used to distinguish types of files.  That is
equivalent to using --color=none.  Using the --color option without the
optional WHEN argument is equivalent to using --color=always.  With
--color=auto, color codes are output only if standard output is connected
to a terminal (tty).  The environment variable LS_COLORS can influence the
colors, and can be set easily by the dircolors command.

Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.

Report bugs to < bug-coreutils@gnu.org> .

Tag : ls command
FAQ
Q
How to list the file and directories to be in Recursively with their subdirectory?
A
You can use the option of "-R" with "ls" command to list the file and directories to be in Recursively with their subdirectory.
Q
How to list the file and directories to be in reverse order?
A
You will use the option of "-r" with "ls" command to list the file and directories to be in reverse order. For Ex: "ls -r".
Q
How to list the Files and Directories with ‘/’ Character at the end using the "ls" command?
A
You can use the option of "-F" with "ls" command to list the Files and Directories with ‘/’ Character at the end using the "ls" command.
Q
How will you figure out the author of each file?
A
We need to use option ‘–author‘ along with option ‘-l‘ to print the author name of each file.Syntax: "ls --author -l".
Q
How to view the hidden file available in the certain directory using the "ls" command on Linux?
A
You can use the option of "-a" with "ls" command to view the hidden file available in the certain directory using the "ls" command on Linux. For Ex: "ls -a".