The ls
command is used to list the contents of a directory. You’ve already seen it used a few times before in examples, but this page will help ensure you are comfortable with its use.
ls [OPTIONS] [FILE]
By default, when the ls
command is used with no options or arguments, it will list the files in the current directory:
To learn the details about a file, such as the type of file, the permissions, ownerships or the timestamp, perform a long listing using the -l
option to the ls
command. Below, a listing of the /var/log directory is used as an example, since it provides a variety of output:
Each line corresponds to a file contained within the directory. The information can be broken down into fields separated by spaces. The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions. The file types are:
Filetype
The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions. The file types are:
Symbol | File Type | Description |
---|---|---|
d | directory | A file used to store other files. |
– | regular file | Includes readable files, images files, binary files, and compressed files. |
l | symbolic link | Points to another file. |
s | socket | Allows for communication between processes. |
p | pipe | Allows for communication between processes. |
b | block file | Used to communicate with hardware. |
c | charecter file | Used to communicate with hardware. |
The first file alternatives.log is a regular file -, while the second file apache2 is a directory d.
Permissions
Permissions indicate how certain users can access a file. Keep reading to learn more about permissions.
Hard Link Count
This number indicates how many hard links point to this file.
User Owner
User syslog owns this file. Every time a file is created, the ownership is automatically assigned to the user who created it.
Group Owner
Indicates which group owns this file.
File Size
Directories and larger files may be shown in kilobytes since displaying their size in bytes would present a very large number. Therefore, in the case of a directory, it might actually be a multiple of the block size used for the file system. Block size is the size of a series of data stored in the filesystem.
Timestamp
This indicates the time that the file’s contents were last modified.
Filename
The final field contains the name of the file or directory.
Sorting
By default the output of the ls command is sorted alphabetically by filename. It can sort by other methods as well.
The -t
option will sort the files by timestamp:
The -S option will sort the files by file size:
The -r option will reverse the order of any type of sort. Notice the difference when it is added to the previous example: