This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has
sudo
access and that you precede each of the privileged commands withsudo
This is one of the most basic commands of linux. It is used to check the contents of a directory (files in a directory). It displays the content of a directory in alphabetical order.
ls
is a command to list computer files in Unix and Unix-like operating systems. When invoked without any arguments, ls lists the files in the current working directory.
In this tutorial, we will show you different ways to check and display the contents of a directory using ls
command in linux command line.
General Syntax
Following is the general syntax of the ls
command
root@codesposts:~$ ls [options] [file|dir]
Displaying Only Names Of Files
If you run the ls
command without any option, you will get the names of the files in the current working directory as an output. But, you will not be able to see the details like file type, file size and date modified etc.
root@codesposts:~$ ls
dos hello.c
Displaying Hidden Files/Directories
If you want to display the hidden files or directories in a Directory, you can use the option -a
with ls
command. The hidden files or directories will have the names starting with a dot (.)
root@codesposts:~$ ls -a
. .. .Xauthority .xsession dos hello.c
Displaying Detailed Output
If you want to display the detailed output (file or directory, size, modified date and time, file or folder name and owner of file and its permission) you can use the option -l
with the ls
command.
root@codesposts:~$ ls -l
total 8
drwxr-xr-x 3 root root 163 Aug 21 2011 dos
-rw-r--r-- 1 root root 242 Jul 15 2017 hello.c
Hiding Current And Previous Directory Entries (.) and (..) In Output
If you want the current directory (.) and previous directory (..) entries to don’t show up in the output, you can use the option -A
with the ls
command.
root@codesposts:~$ ls -
.Xauthority .xsession dos hello.c
Displaying Details In Human Readable Format
If you want to display the output of the command to be in a human readable format, you can combine the options -lh
with the ls
command.
root@codesposts:~$ ls -lh
total 8
drwxr-xr-x 3 root root 163 Aug 21 2011 dos
-rw-r--r-- 1 root root 242 Jul 15 2017 hello.c
Highlighting Directories With / At The End Of Name
If you want to mention the directories with a character “/” at the end of the the name, you can use the option -F
with the ls
command.
root@codesposts:~$ ls -F
dos/ hello.c
Displaying Output In Reverse Order
If you want to display the output of the command in reverse alphabetical order, you can use the option -r
with the ls
command.
root@codesposts:~$ ls -r
hello.c dos .xsession .Xauthority .. .
Sorting Output On The Basis Of File Extension
If you want to sort the output of the command alphabetically on the basis of the file extension, you can use the option -X
with the ls
command.
root@codesposts:~$ ls -X
asm-1.9 asm.com debug.com hpigot.com hpigot.s
Sorting Output On The Basis Of Last Modified
If you want to sort the output of the command alphabetically on the basis of the last modified time of the file, you can use the option -t
with the ls
command.
root@codesposts:~$ ls -t
total 36
-rw-r--r-- 1 root root 4043 Aug 21 2011 hpigot.s
-r-xr-xr-x 1 root root 15224 Aug 21 2011 debug.com
-rwxr-xr-x 1 root root 77 Aug 21 2011 hpigot.com
drwxr-xr-x 2 root root 460 Aug 20 2011 asm-1.9
-rwxr-xr-x 1 root root 7297 Apr 21 2009 asm.com
Sorting Output On The Base Of File Size
If you want to sort the output of the command on the basis of the file size, you can use the option -lS
with the ls
command.
root@codesposts:~$ ls -lS
total 36
-r-xr-xr-x 1 root root 15224 Aug 21 2011 debug.com
-rwxr-xr-x 1 root root 7297 Apr 21 2009 asm.com
-rw-r--r-- 1 root root 4043 Aug 21 2011 hpigot.s
drwxr-xr-x 2 root root 460 Aug 20 2011 asm-1.9
-rwxr-xr-x 1 root root 77 Aug 21 2011 hpigot.com
It will sort the output on the basis of file size in the descending order i.e. largest file first.
To get the smallest file first, you can combin the option -r
with the ls
command.
Displaying Sub-directory Tree Recursively
If you want to display the tree of the sub-directories, you can use the option -R
with the ls
command.
root@codesposts:~$ ls -R
.:
. .. .Xauthority .xsession dos hello.c
./dos:
. .. asm-1.9 asm.com debug.com hpigot.com hpigot.s
./dos/asm-1.9:
. direct.s expr.s message.s symbols.s
.. display.s input.s output.s symtab.i
Changelog dos.i license.txt readme.txt symtab.s
asm.s equ.s lister.s support.s
Displaying Inodes With The File Name
If you want to display the inode number with the file name, you can use the option -i
with the ls
command.
root@codesposts:~$ ls -i
645 . 650 asm-1.9 648 debug.com 646 hpigot.s
643 .. 647 asm.com 649 hpigot.com
Displaying Author Of The File
If you want to display the name of the author of each file in the long/detailed format of the output of the command, you can use the option --author
with the ls
command.
root@codesposts:~$ ls -l --author
Displaying Contents Of A Specific Directory
If you want to display the files in a specific directory you can run the command like this
root@codesposts:~$ ls -l /name-of-directory
root@codesposts:~$ ls -l /abc
This command will display the contents of the directory “abc”
Displaying C-style escapes for non-graphic characters
If the current directory contains a file with a name having a new-line character, the command will display that particular file name containing a question mark (?), which signifies a non-printable character.
But you can use the option -b
to print C-style escape characters for non-printable characters.
root@codesposts:~$ ls -b
Displaying File Size In Blocks
If you want to display the size of the files in blocks other than bytes, you can use the option --block-size=
with the command
root@codesposts:~$ ls -l --block-size=k
Displaying Only Filename And Filesize In The Output
If you want to display only the filename and the file size, you can combine the option -h
with the option -l/-s
.
root@codesposts:~$ ls -l -h
total 8
4 dos 4 hello.c
Displaying UID And GID Of Files
If you want to display the UID and GID of the files, you can use the option -n
with the ls
command.
root@codesposts:~$ ls -n
total 20
drwx------ 3 0 0 135 Oct 24 2017 .
drwxrwxrwx 19 0 0 457 Aug 11 15:28 ..
-rw------- 1 0 0 0 Jul 8 2017 .Xauthority
-rwxr-xr-x 1 0 0 28 Jun 24 2017 .xsession
drwxr-xr-x 3 0 0 163 Aug 21 2011 dos
-rw-r--r-- 1 0 0 242 Jul 15 2017 hello.c
Avoiding Backup Files In The Output
If you want the command not to list the backup files in the output, you can use the option -B
with the ls
command.
root@codesposts:~$ ls -B
Displaying Version Of ls Command
If you want to display the version of the command, you can use the option --version
with the ls
command.
root@codesposts:~$ 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.
Displaying Help Page
If you want to display the help page of the command, you can use the option --help
with the ls
command.
root@codesposts:~$ ls --help