ls Command Examples in Linux
At some point in time, you must have used the ls command if you use UNIX/Linux systems. ls is the most commonly used command by users of UNIX/Linux and in this post, we will discuss some of the examples of the Linux command.
Suppose I want to list the files in a directory. I could enter the ls (“list”) command, which would do just that. But if I want to list the files in a particular way—say, in a way that included their sizes and modification dates—I could add a flag to the ls command.
The flag that ls uses to indicate a “long” listing (including sizes and dates) is -l. So if I enter ls -l (note the space before the flag), I get the kind of listing I want.
Syntax
The syntax of the ls command is:
# ls [options] [file/directory names]
Some of the most commonly used options with the ls command are displayed in the table below:
Option | Used To |
---|---|
-l | Display long list including permissions, owner, group, size, etc. |
-F | Display nature of file (executable, directory etc) |
-a | Display all files, including “hidden” files |
-H | Recursively display all subdirectories |
-d | Display info about symbolic links |
-L | Display all files including symbolic links |
ls COLORS
In the Bash shell, when you execute the ls command, you may have noticed that the results sometimes appear in different colors. These colors distinguish different types of files. By default, some of the colors are:
- Default color: Normal/text file
- Blue: Directory
- Sky blue: Symbolic link or audio file
- Green: Executable file
- Yellow with black background: Device
- Pink: Image file
- Red: Archive file
- Red with black background: Broken link
ls Command Examples
1. To list the directory content:
# ls
2. To list all the directory contents:
# ls -a
# ls --all
3. To skip listing implied . and ..:
# ls -A
# ls --almost-all
4. To print the author of each file:
# ls --author -l
5. To print in specified block size:
# ls --block-size=1024
6. To skip listing entries ending with ~:
# ls -B
# ls --ignore-backups
7. To show the ctime for the files:
# ls -c
8. To list entries by columns:
# ls -C
9. To list the content in color:
# ls --color
10. To list the directory entries only:
# ls -d
# ls --directory
11. To not to sort the output:
# ls -f
13. To append the indicator:
# ls -F
# ls --classify
14. To specify the format:
$ ls --format=verbose
$ ls --format=long
$ ls --format=commas
$ ls --format=horizontal
$ ls --format=across
$ ls --format=vertical
$ ls --format=single-column
$ ls --format=context
15. To long list:
# ls --full-time
16. To list group and not the owner:
# ls -g
17. To group directories before files:
# ls --group-directories-first
18. To print in human readable format:
# ls -h
# ls --human-readable
19. To list the size in powers of 1000 and not 1024:
# ls --si
20. To follow symbolic links:
# ls -H
# ls --dereference-command-line
21. To follow each command line symbolic link that points to a directory:
# ls --dereference-command-line-symlink-to-dir
22. To skip listing implied entries matching shell PATTERN:
# ls --hide=PATTERN
23. To print the index number of each file:
# ls -i
# ls --inode
24. To skip listing implied entries matching shell PATTERN:
# ls -I
# ls --ignore=PATTERN
25. To list the entries with block size of 1K:
# ls -k
# ls --block-size=1K
26. To get comma separated list of entries:
# ls -m
27. To list entries with numeric user and user IDs:
# ls -n
# ls --numeric-uid-gid
28. To print raw entry names:
# ls -N
# ls --literal
29. To skip listing group entries:
# ls -o
30. To append indicator to directories:
# ls -p
# ls --indicator-style=slash
31. To print the ? instead of non graphic characters:
# ls -q
# ls --hide-control-chars
32. To enclose entry names in double quotes:
# ls -Q
# ls --quote-name
33. To reverse order while sorting:
# ls -r
# ls --reverse
34. To list subdirectories recursively:
# ls -R
# ls --recursive
35. To print the allocated size of each file, in blocks:
# ls -s
# ls --size
36. To sort by file size:
# ls -S
37. To sort by WORD instead of name:
# ls --sort=WORD
38. To sort by modification time:
# ls -t
39. To assume tab stops at each COLS instead of 8:
# ls -T
# ls --tabsize=COLS
40. To not to sort:
# ls -U
41. For natural sort of (version) numbers within text:
# ls -v
42. To assume screen width instead of current value:
# ls -w
# ls --width=COLS
43. To list entries by lines instead of by columns:
# ls -x
44. To sort alphabetically by entry extension:
# ls -X
45. To list one file per line:
# ls -1
46. To display the security context:
# ls --lcontext
47. To display security context so it fits on most displays:
# ls -Z
# ls --context
48. To display only security context and file name:
# ls --scontext
49. To display the help:
# ls --help
50. To output version information:
# ls --version