One of the most underrated Linux commands is ls; it is, of course, used for listing the contents of a directory. However ls has a number of options and each of these options can change the output that ls produces - making it a very versatile tool.
The Standard ls Usage
The most usual usage of ls is just to list the files in a directory:
This output can be changed from horizontal to vertical by using the -1 (that's the number one, not lowercase L) option:
Using ls to Show Hidden Files
Any file with the prefix (a full-stop or period) is normally hidden from the display; the -a option is used to reveal the hidden files:
The Long Listing Format of ls
More than just a simple list of files can be displayed; the long listing format shows:
- the file permissions
- number of hard links to the file
- file owner
- file group
- size
- date and time of file creation
- file name
The long listing format is initiated by using the -l (that's lowercase L, not the number one) option:
And the long listing format can be used with the -a option to show the details of all of the hidden files as well:
Another useful form of the long format is the file classification (-F) - this appends a symbol onto the file name according to the file:
- /: directory
- *: executable
- @: symbolic link
- =: socket
- %: whiteout
- |: FIFO
For example:
Learning more about ls
All of the other options can be read about by using the Linux man command:
Summary
At it's simplest level ls is very useful for listing the contents of a directory, but has a number of options, for example:
- -1: display vertically rather than horizontally
- -a: show hidden files
- -l: long listing format
- -F: show file classification
The options can be used on their own or in conjunction completely change the way that the ls output looks.