ls command tutorial in linux/unix with examples and use cases
linux ls command – list directory contents
Linux ls command is the most commonly used command in our daily use of linux. ls can not only display the name of each file and directory and its associated information, but also display a sorted list of files and directories according to the default or specified rules.
ls [OPTION]... [FILE]...
- -a, –all
do not ignore entries starting with .
- -A, –almost-all
do not list implied . and ..
with -l, print the author of each file
with -l, scale sizes by SIZE when printing them; e.g., ‘–block-size=M’; see SIZE format below
- -B, –ignore-backups
do not list implied entries ending with ~
with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name;otherwise: sort by ctime, newest first
with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first
- -d, –directory
list directories themselves, not their contents
use a long listing format
fill width with a comma separated list of entries
- -r, –reverse
reverse order while sorting
- -R, –recursive
list subdirectories recursively
- -s, –size
print the allocated size of each file, in blocks
sort by file size, largest first
sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
sort by modification time, newest first
list one file per line.
Examples & Use Cases
List files and directories in the current directory
In the following example we will list all the contents of the directory, including hidden files and directories.
➜ ls -al
List files and directories sorted by time
In the following example we will list all the contents of the directory in reverse order of modification time.
➜ ls -lrt
List only directories
In the following example, we will list only the directories in the current directory.
➜ ls -dl */
Return only directory names and file names
In the following example, we will return only the directory name and file name in the current directory.
➜ ls -1