In this tutorial, we will cover ls command in Linux with 14 useful practical examples.
ls command in Linux/UNIX list the directory contents. When we issue ls command without any option, it displays the information about files and directories of present working directory in alphabetical order.
ls command syntax
$ ls <options> <file | directory>
Without any further delay, let’s jump into ls command useful examples.
1) List the contents of current working directory
When we run ls command without any flag from current working directory then it will list the files and directories as shown below,
$ ls Desktop Documents Downloads Music myscript.sh Pictures playbook.yaml Public snap Templates Videos $
2) List contents in long listing format
To list the contents of a directory in long listing format, use -l option in ls command.
If you have noticed the above output carefully, ‘ls -l’ also shows file’s and directory’s permissions, modification time and size too.
3) List the contents of specific directory
Run ‘ls -l <directory-name>’ command to list the contents. In following example, we are listing the contents of /var/log/apt/ directory.
$ ls -l /var/log/apt/
If you wish to list directory directory permissions only then use ‘-ld’ option. Example is shown below
$ ls -ld /var/log/apt/ drwxr-xr-x 2 root root 4096 Oct 14 06:58 /var/log/apt/ $
4) Display file types of directory contents (ls -F)
To list file types with ls command then use ‘-F’ option, in the following example, we are listing the file types from present working directory
$ ls -F Desktop/ Documents/ Downloads/ Music/ myscript.sh* Pictures/ playbook.yaml Public/ snap/ Templates/ Videos/ $
In the output above, following will be appended for respective type,
- ‘/’ – directory
- normal files – nothing
- * – executable file
- @ – link file
5) List contents sorted by time (ls -lt)
Use ‘-t’ flag in ls command to list the contents of a directory sorted by time.
$ ls -lt
To list files sorted by modification time in reverse order, run
$ ls -ltr
6) Listing contents in human readable format (ls -lh)
-‘h’ option in ls command is used to list file’s size in human readable format (2K, 34M or 5G).
$ ls -lh
7) List hidden files (ls -la)
‘-a’ option in ls command is used to list all files including hidden files of directory. To list hidden files in long listing format use ‘ls -la’ command
$ ls -la /home/linuxtechi/
8) Listing files and directories recursively (ls -R)
Let’s assume we wish to list the files and directories of /etc directory recursively then use ‘-R’ option in ls command. example is shown below
$ ls -R /etc/
9) List files sorted by their size (ls -lhs)
Use ‘-lhs’ option in ls command to list file sorted by size (human readable size like K, M & G), example is shown below:
$ ls -lhS
Above command will list larger files first based on their size.
10) List files & directories inode numbers (ls -li)
To list inode numbers of file and directory using ls command then use ‘-i’ option,
$ ls -li
11) Format ls command output
Output of ls command can be formatted using ‘–format’ flag.
$ ls --format=WORD
following format supported by ls
- across -x
- commas -m
- horizontal -x, long -l
- single-column -1
- verbose -l
- vertical -C
$ ls -m or $ ls --format=commas
Listing output in single column, run
$ ls -1
12) List uid and gid of file and directory (ls -n)
To list file’s and directory’s UID and GID with ls command, use ‘-n’ option, example is shown below
$ ls -n
13) View default aliases of ls command
Type the alias and grep command on the terminal to display the default aliases set for ls command.
$ alias | grep ls alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto' $
14) Enable timestamps in ls command output
$ ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
Note : For more detailed options of ls command, please refer ls man page.
That’s all from this tutorial, I hope you have found it informative. Kindly post you queries and feedback in below comments section.
Also Read : 14 grep command examples in Linux
Also Read : Linux Zip and Unzip Command with Examples
1 thought on “14 Useful ls Command Examples in Linux”
Please try and be comprehensive. The -F flag also displays | (pipe) when the file is a… pipe. Example:
$ mkfifo foo
$ ls -F foo