Exploring the Power of File Listing: A Comprehensive Guide to the Linux ls Command
The ls command serves as a versatile tool for listing the contents of directories, offering a multitude of options and parameters to tailor the output according to specific requirements and preferences. From basic file and directory listing to advanced sorting, formatting, and filtering capabilities, ls provides a rich array of functionalities designed to accommodate diverse use cases, ranging from routine file navigation to complex data analysis and system diagnostics.
Some common options for the
ls
command in Linux along with descriptions for each option:
Option | Description |
---|---|
-a, --all |
Do not ignore entries starting with . . |
-A, --almost-all |
Do not list implied . and .. . |
--author |
With -l and -g , list author of each file. |
-b, --escape |
Print C-style escapes for nongraphic characters. |
--block-size=SIZE |
Use SIZE-byte blocks. |
-B, --ignore-backups |
Do not list implied entries ending with ~ . |
-c |
With -lt , sort by, and show, ctime (time of last modification of file status information). |
-C |
List entries by columns. |
--color[=WHEN] |
Colorize the output. WHEN can be never , always , or auto . |
-d, --directory |
List directory entries instead of contents. |
-D, --dired |
Generate output designed for Emacs’ dired mode. |
-f |
Do not sort. |
-F, --classify |
Append indicator (one of */=>@) to entries. |
--file-type |
Likewise, except do not append * . |
--format=WORD |
Across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C. |
-g |
Like -l , but do not list owner. |
--group-directories-first |
Group directories before files. |
-h, --human-readable |
With -l and -s , print sizes like 1K, 234M, 2G, etc. |
--si |
Likewise, but use powers of 1000 not 1024. |
-H, --dereference-command-line |
Follow symbolic links listed on the command line. |
-i, --inode |
Print the index number of each file. |
-l |
Use a long listing format. |
-L, --dereference |
When showing file information for a symbolic link, show information for the file the link references. |
--quoting-style=WORD |
Use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape. |
-n, --numeric-uid-gid |
Like -l , but list numeric user and group IDs. |
-N, --literal |
Do not treat . or .. specially. |
-o |
Like -l , but do not list group information. |
-p, --indicator-style=WORD |
Append indicator with style WORD to entry names: none (default), slash (-p), file-type (–file-type). |
-q, --hide-control-chars |
Print ? instead of non graphic characters. |
--show-control-chars |
Show non graphic characters as-is (default unless program is ‘ls’ and output is a terminal). |
--color=WHEN |
Control coloring: always , never , or auto . |
-r, --reverse |
Reverse order while sorting. |
-R, --recursive |
List subdirectories recursively. |
-s, --size |
Print the allocated size of each file, in blocks. |
-S |
Sort by file size. |
--sort=WORD |
Sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X). |
--time=WORD |
Show time as WORD instead of default modification time: atime, access, use, ctime, or status. |
--time-style=STYLE |
With -l , show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT. |
-t |
Sort by modification time. |
-T, --tabsize=COLS |
Assume tab stops at each COLS instead of 8. |
-u |
With -lt , sort by, and show, access time. |
-U |
Do not sort; list entries in directory order. |
-v |
Natural sort of (version) numbers within text. |
-w, --width=COLS |
Assume screen width instead of current value. |
-x |
List entries by lines instead of by columns. |
-X |
Sort alphabetically by entry extension. |
-Z |
Print any security context of each file. |
-
Option: -a, –all
Use case: List all files including hidden ones.
Source files:
1 2
ls .hiddenfile regularfile1 regularfile2
Result:
1 2
ls -a .hiddenfile regularfile1 regularfile2
(notice: The
.hiddenfile
is now visible.)Explanation: The
-a
option lists all entries including hidden ones. Hidden files are those whose filenames begin with a dot (.
).
-
Option: -A, –almost-all
Use case: List all files excluding
.
and..
.Source files:
1 2
ls .hiddenfile regularfile1 regularfile2
Result:
1 2
ls -A .hiddenfile regularfile1 regularfile2
(notice: The
.
and..
are not listed.)Explanation: The
-A
option lists all entries except for.
and..
.
-
Option: –author
Use case: List files with author information.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -l --author -rw-r--r-- 1 user user 0 Apr 17 12:00 user regularfile1
(notice: The author
user
is listed after the permissions and before the group.)Explanation: The
--author
option with-l
or-g
lists the author of each file.
-
Option: -b, –escape
Use case: Print C-style escapes for nongraphic characters.
Source files:
1 2
ls file_with_space file_with_newline
Result:
1 2
ls -b file_with_space file_with\nnewline
(notice: The newline is represented as
\n
.)Explanation: The
-b
option prints C-style escapes for nongraphic characters.
-
Option: –block-size=SIZE
Use case: List file sizes using specified block size.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 4096 Apr 17 12:00 regularfile1
Result:
1 2
ls -l --block-size=KB -rw-r--r-- 1 user user 4K Apr 17 12:00 regularfile1
(notice: The file size is displayed in kilobytes.)
Explanation: The
--block-size=SIZE
option changes the block size used for file size display.
-
Option: -B, –ignore-backups
Use case: List files ignoring those ending with
~
.Source files:
1 2
ls regularfile1 regularfile2 backupfile~
Result:
1 2
ls -B regularfile1 regularfile2
(notice: The
backupfile~
is not listed.)Explanation: The
-B
option excludes files that end with~
.
-
Option: -c
Use case: List files sorted by ctime.
Source files:
1 2 3
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1 -rw-r--r-- 1 user user 0 Apr 16 12:00 regularfile2
Result:
1 2 3
ls -ltc -rw-r--r-- 1 user user 0 Apr 16 12:00 regularfile2 -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
(notice: The files are now sorted by ctime.)
Explanation: The
-c
option with-lt
sorts files by ctime (time of last modification of file status information).
-
Option: -C
Use case: List files in columns.
Source files:
1 2
ls file1 file2 file3
Result:
1 2
ls -C file1 file2 file3
(notice: The files are listed in columns.)
Explanation: The
-C
option lists files by columns.
-
Option: –color[=WHEN]
Use case: Colorize the output.
Source files:
1 2
ls directory/ file1 file2
Result:
1 2
ls --color=always \033[0;34mdirectory\033[0m/ \033[0;32mfile1\033[0m \033[0;32mfile2\033[0m
(notice: The output is colorized.)
Explanation: The
--color
option colors the output.WHEN
can benever
,always
, orauto
.
-
Option: -d, –directory
Use case: List directory names instead of contents.
Source files:
1 2
ls directory/ file1 file2
Result:
1 2
ls -d directory/
(notice: Only the directory name is listed.)
Explanation: The
-d
option lists directory names instead of their contents.
-
Option: -D, –dired
Use case: Generate output designed for Emacs’ dired mode.
Source files:
1 2
ls file1 file2
Result:
1 2 3
ls -D 2024-04-17 12:00 file1 2024-04-17 12:00 file2
(notice: The output is formatted for Emacs’ dired mode with timestamps.)
Explanation: The
-D
option generates output formatted for Emacs’ dired mode, showing timestamps.
-
Option: -f
Use case: List files without sorting.
Source files:
1 2
ls file3 file1 file2
Result:
1 2
ls -f file3 file1 file2
(notice: The files are listed in the order they appear in the directory.)
Explanation: The
-f
option lists files in their natural order without sorting.
-
Option: -F, –classify
Use case: Append indicator to entries.
Source files:
1 2
ls directory/ executable* link@ pipe| regularfile
Result:
1 2
ls -F directory/ executable* link@ pipe| regularfile
(notice: Indicators are added to each entry indicating their type.)
Explanation: The
-F
option appends a character to each entry to indicate its type:/
for directories,*
for executables,@
for symbolic links,|
for pipes.
-
Option: –file-type
Use case: Append indicator to entries without
*
.Source files:
1 2
ls directory/ executable link@ pipe| regularfile
Result:
1 2
ls --file-type directory/ executable@ link@ pipe| regularfile
_(notice: Indicators are added to each entry but without the
_
.)*Explanation: The
--file-type
option is similar to-F
, but it doesn’t append*
to executable files.
-
Option: –format=WORD
Use case: Format output in a specific layout.
Source files:
1 2
ls file1 file2
Result:
1 2
ls --format=comma file1, file2
(notice: The output is in comma-separated format.)
Explanation: The
--format=WORD
option changes the layout of the output.WORD
can beacross
,commas
,horizontal
,long
,single-column
,verbose
, orvertical
.
-
Option: -g
Use case: List files in long format without owner.
Source files:
1 2
ls -l -rw-r--r-- 1 user group 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -lg -rw-r--r-- 1 group 0 Apr 17 12:00 regularfile1
(notice: The owner
user
is omitted.)Explanation: The
-g
option lists files in long format without the owner information.
-
Option: –group-directories-first
Use case: Group directories before files.
Source files:
1 2
ls directory/ file1 file2
Result:
1 2
ls --group-directories-first directory/ file1 file2
(notice: Directories are listed first.)
Explanation: The
--group-directories-first
option lists directories before files.
-
Option: -h, –human-readable
Use case: Display file sizes in human-readable format.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 4096 Apr 17 12:00 regularfile1
Result:
1 2
ls -lh -rw-r--r-- 1 user user 4.0K Apr 17 12:00 regularfile1
(notice: The file size is displayed in a human-readable format.)
Explanation: The
-h
option with-l
displays file sizes in a human-readable format (e.g., 1K, 234M, 2G).
-
Option: –si
Use case: Display file sizes using powers of 1000.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 1024 Apr 17 12:00 regularfile1
Result:
1 2
ls -l --si -rw-r--r-- 1 user user 1.0K Apr 17 12:00 regularfile1
(notice: The file size is displayed using powers of 1000.)
Explanation: The
--si
option with-l
displays file sizes in powers of 1000 instead of 1024.
-
Option: -H, –dereference-command-line
Use case: Follow symbolic links listed on the command line.
Source files:
1 2
ls -l lrwxrwxrwx 1 user user 5 Apr 17 12:00 link -> file1
Result:
1 2
ls -lH -rw-r--r-- 1 user user 0 Apr 17 12:00 file1
(notice: The symbolic link
link
is dereferenced tofile1
.)Explanation: The
-H
option follows symbolic links listed on the command line.
-
Option: -i, –inode
Use case: Display the index number (inode) of each file.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -li 123456 -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
(notice: The inode number
123456
is displayed before the file details.)Explanation: The
-i
option with-l
displays the inode number of each file.
-
Option: -l
Use case: Use a long listing format.
Source files:
1 2
ls regularfile1 regularfile2
Result:
1 2 3
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1 -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile2
(notice: The files are listed in long format with detailed information.)
Explanation: The
-l
option displays files in a long listing format, providing detailed information such as permissions, number of links, owner, group, size, and timestamp.
-
Option: -L, –dereference
Use case: Display information of the file the symbolic link refers to.
Source files:
1 2
ls -l lrwxrwxrwx 1 user user 5 Apr 17 12:00 link -> regularfile1
Result:
1 2
ls -lL -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
(notice: The symbolic link
link
is dereferenced toregularfile1
.)Explanation: The
-L
option displays information for the file the symbolic link points to, rather than the link itself.
-
Option: –quoting-style=WORD
Use case: Use a specific quoting style for entry names.
Source files:
1 2
ls file with space 'file with quotes'
Result:
1 2
ls --quoting-style=shell 'file with space' 'file with quotes'
(notice: Entry names are quoted using single quotes.)
Explanation: The
--quoting-style=WORD
option changes the quoting style for entry names.WORD
can beliteral
,locale
,shell
,shell-always
,c
, orescape
.
-
Option: -n, –numeric-uid-gid
Use case: List numeric user and group IDs.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -ln -rw-r--r-- 1 1000 1000 0 Apr 17 12:00 regularfile1
(notice: The owner and group are displayed as numeric IDs.)
Explanation: The
-n
option with-l
displays numeric user and group IDs instead of names.
-
Option: -N, –literal
Use case: Do not treat
.
and..
specially.Source files:
1 2
ls -a . .. regularfile1
Result:
1 2
ls -aN . .. regularfile1
(notice:
.
and..
are not treated specially.)Explanation: The
-N
option prevents treating.
and..
as special entries and lists them as regular entries.
-
Option: -o
Use case: List in long format without group information.
Source files:
1 2
ls -l -rw-r--r-- 1 user group 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -lo -rw-r--r-- 1 user 0 Apr 17 12:00 regularfile1
(notice: The group information is omitted.)
Explanation: The
-o
option lists files in long format without the group information.
-
Option: -p, –indicator-style=WORD
Use case: Append indicator with specified style to entry names.
Source files:
1 2
ls directory executable link pipe regularfile
Result:
1 2
ls -p directory/ executable* link@ pipe| regularfile
(notice: Indicators are added to each entry with specified styles.)
Explanation: The
-p
option appends a character to each entry to indicate its type, with the specified style.WORD
can benone
(default),slash
, orfile-type
.
-
Option: -q, –hide-control-chars
Use case: Print
?
instead of non-graphic characters.Source files:
1 2
ls file1 file^with^caret
Result:
1 2
ls -q file1 file?with?caret
(notice: Non-graphic characters are replaced with
?
.)Explanation: The
-q
option replaces non-graphic characters with?
.
-
Option: –show-control-chars
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
**Use case:** Show non-graphic characters as-is. **Source files:** ```bash ls file1 file^with^caret ``` **Result:** ```bash ls --show-control-chars file1 file^with^caret ``` _(notice: Non-graphic characters are displayed as-is.)_ **Explanation:** The `--show-control-chars` option displays non-graphic characters as-is, which is the default behavior unless the program is 'ls' and the output is a terminal.
Certainly, let’s continue with more
ls
command options:
-
Option: –color=WHEN
Use case: Control coloring of the output.
Source files:
1 2
ls directory/ executable file1
Result:
1 2
ls --color=always \033[0;34mdirectory/\033[0m \033[0;32mexecutable\033[0m file1
(notice: The output is colorized.)
Explanation: The
--color=WHEN
option controls the coloring of the output.WHEN
can bealways
,never
, orauto
.
-
Option: -r, –reverse
Use case: Reverse the order while sorting.
Source files:
1 2
ls file3 file1 file2
Result:
1 2
ls -r file3 file2 file1
(notice: The files are listed in reverse order.)
Explanation: The
-r
option reverses the order of sorting.
-
Option: -R, –recursive
Use case: List subdirectories recursively.
Source files:
1 2 3 4 5 6 7 8 9 10
ls -R .: directory1/ directory2/ ./directory1: file1 ./directory2: file2
Result:
1 2 3 4 5 6 7 8 9
ls -R .: directory1/ directory2/ file1 directory1: file1 directory2: file2
(notice: The subdirectories and their contents are listed recursively.)
Explanation: The
-R
option lists subdirectories and their contents recursively.
-
Option: -s, –size
Use case: Print the allocated size of each file, in blocks.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 4096 Apr 17 12:00 regularfile1
Result:
1 2
ls -ls 4 -rw-r--r-- 1 user user 4096 Apr 17 12:00 regularfile1
(notice: The file size is displayed in blocks.)
Explanation: The
-s
option with-l
displays the allocated size of each file in blocks.
-
Option: -S
Use case: Sort files by size.
Source files:
1 2 3
ls -l -rw-r--r-- 1 user user 1024 Apr 17 12:00 regularfile1 -rw-r--r-- 1 user user 512 Apr 17 12:00 regularfile2
Result:
1 2 3
ls -lS -rw-r--r-- 1 user user 1024 Apr 17 12:00 regularfile1 -rw-r--r-- 1 user user 512 Apr 17 12:00 regularfile2
(notice: The files are sorted by size in descending order.)
Explanation: The
-S
option sorts files by size, largest first.
-
Option: –sort=WORD
Use case: Sort files by specified criteria.
Source files:
1 2
ls file3 file1 file2
Result:
1 2
ls --sort=size file3 file2 file1
(notice: The files are sorted by size.)
Explanation: The
--sort=WORD
option sorts files by the specified criteria.WORD
can benone
(-U),size
(-S),time
(-t),version
(-v), orextension
(-X).
-
Option: –time=WORD
Use case: Show time as specified instead of default modification time.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -lt --time=atime -rw-r--r-- 1 user user 0 Apr 17 11:00 regularfile1
(notice: The access time (
atime
) is displayed instead of the default modification time.)Explanation: The
--time=WORD
option displays the time as specified.WORD
can beatime
,access
,use
,ctime
, orstatus
.
-
Option: –time-style=STYLE
Use case: Show times using specified style.
Source files:
1 2
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -l --time-style=full-iso -rw-r--r-- 1 user user 0 2024-04-17 12:00:00.000000000 +0000 regularfile1
(notice: The time is displayed in the
full-iso
style.)Explanation: The
--time-style=STYLE
option displays times using the specified style.STYLE
can befull-iso
,long-iso
,iso
,locale
, or+FORMAT
.
-
Option: -t
Use case: Sort files by modification time.
Source files:
1 2
ls file2 file1 file3
Result:
1 2
ls -lt file3 file2 file1
(notice: The files are sorted by modification time, newest first.)
Explanation: The
-t
option sorts files by modification time, newest first.
-
Option: -T, –tabsize=COLS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
**Use case:** Assume tab stops at each COLS instead of 8. **Source files:** ```bash ls file1 file2 ``` **Result:** ```bash ls -T 4 file1 file2 ``` _(notice: Tab stops are assumed at each 4 columns.)_ **Explanation:** The `-T, --tabsize=COLS` option changes the tab stops to the specified number of columns.
Sure, let’s continue with more
ls
command options:
-
Option: -u
Use case: Sort files by access time.
Source files:
1 2 3
ls -l -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1 -rw-r--r-- 1 user user 0 Apr 16 12:00 regularfile2
Result:
1 2 3
ls -lu -rw-r--r-- 1 user user 0 Apr 16 12:00 regularfile2 -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
(notice: The files are sorted by access time, newest first.)
Explanation: The
-u
option sorts files by access time.
-
Option: -U
Use case: Do not sort; list entries in directory order.
Source files:
1 2
ls file3 file1 file2
Result:
1 2
ls -U file3 file1 file2
(notice: The files are listed in directory order.)
Explanation: The
-U
option lists files in the order they appear in the directory, without sorting.
-
Option: -v
Use case: Natural sort of (version) numbers within text.
Source files:
1 2
ls file2 file10 file1
Result:
1 2
ls -v file1 file2 file10
(notice: The files are sorted naturally considering the version numbers.)
Explanation: The
-v
option sorts files considering the version numbers within the text.
-
Option: -w, –width=COLS
Use case: Assume screen width instead of current value.
Source files:
1 2
ls file1 file2 file3 file4 file5 file6
Result:
1 2
ls -w 20 file1 file2 file3 file4 file5 file6
(notice: The output is formatted according to the specified screen width of 20 columns.)
Explanation: The
-w, --width=COLS
option sets the screen width to the specified number of columns.
-
Option: -x
Use case: List entries by lines instead of by columns.
Source files:
1 2
ls file1 file2 file3
Result:
1 2 3 4
ls -x file1 file2 file3
(notice: The files are listed one per line.)
Explanation: The
-x
option lists entries one per line, instead of in columns.
-
Option: -X
Use case: Sort alphabetically by entry extension.
Source files:
1 2
ls file3.txt file1.jpg file2.doc
Result:
1 2
ls -X file2.doc file1.jpg file3.txt
(notice: The files are sorted by their extensions.)
Explanation: The
-X
option sorts files alphabetically by their extensions.
-
Option: -Z, –context
Use case: Print any security context of each file.
Source files:
1 2
ls -lZ -rw-r--r-- 1 user user 0 Apr 17 12:00 regularfile1
Result:
1 2
ls -lZ -rw-r--r-- 1 user user system_u:object_r:user_home_t 0 Apr 17 12:00 regularfile1
(notice: The security context of the file is displayed.)
Explanation: The
-Z
option prints any security context of each file.