Listing Files in Linux

Listing Files in Linux

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:

SymbolFile TypeDescription
ddirectoryA file used to store other files.
regular fileIncludes readable files, images files, binary files, and compressed files.
lsymbolic linkPoints to another file.
ssocketAllows for communication between processes.
ppipeAllows for communication between processes.
bblock fileUsed to communicate with hardware.
ccharecter fileUsed 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:

Leave a Reply
Your email address will not be published. *

This site uses Akismet to reduce spam. Learn how your comment data is processed.