Managing files in Linux: ls command

M

The ls command (list) displays the file names in a directory.

The syntax of the ls command is simple:

ls [options] [files]

Both the command options and the list of files are optional. If we omit to write the list of files, the ls command will display the contents of the current directory (we will use the pwd command first to display the directory we are in).

$ pwd

$ ls

If we want to display the contents of a directory other than the current directory, we need to mention its name:

$ ls /home/mvps/myVPS

We can tell the command ls to display a certain type of files; for example, I told the command ls to show me all the text files (*.txt) in the current directory and then to show me the contents of the /home directory:

$ ls *.txt /home/mvps

/home/mvps

When set, the ls command displays the files in alphabetical order in a case-insensitive manner (it does not matter if they are uppercase or lowercase).

The most used ls command options – Detailed listing

One of the most used options of the ls command is -l, which creates a “long exit”. Permissions are displayed, the number of links, the owner, the group, the file size, the date when it was created and file name.

Displaying all files

Normally, the -ls command omits the files whose names start with a dot (.). These files are, in most of the cases, configuration files that are normally not of interest to a regular user and are therefore hidden.

Adding the -a or –all parameter allows you to display these files.

Displaying the file type

The -F or –classify parameter adds an indicator code at the end of the file name, so we know the type of that file. These codes are:

/ Directory

* Executable

| Pipe

= Socket

@ Symbol Link

Displaying the colours

If the terminal that we use is not able to display the colours (each color corresponds to each file type), then we can add to the -ls command the –color parameter. But nowadays, most terminals show each file type in another color, so we can quickly distinguish between plain and executable files.

The recursive display

The -R or –recursive option tells the ls command to display the contents of each directory in recursive mode. If the target directory contains subdirectories, the ls command displays both the names of the subdirectories and their contents. The result can be a huge list if the target directory has many subdirectories that contain many files.

$ ls -R
./vps:

The ls command has many other options; those presented by me are the most used. If you want to master this command, type man ls or info ls in the terminal.

The command options ls can be combined, preceded by a single dash -; so we will write:

1s -1F instead of 1s -1 -F

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories