The ls
command is essential for navigating the Linux or UNIX file system. To understand how to use it effectively, you need to be familiar with basic navigation in the Linux OS and the additional options available.
The file system of Linux distributions is structured like a tree. At the base is the root directory, denoted by a slash (/
), which contains all the information in the OS. From the root directory, "branches" or other directories spread out.
You can specify a path to a folder using either a full (absolute) or relative path.
A full (absolute) path specifies the location of files and directories starting from the root directory. This path always starts with a slash. Example of a full path:
/home/hostman/file1/
A relative path specifies the location of objects relative to the current position (current directory). In the beginning of the address line, you might see:
~
— home directory;
.
— indicates the current directory;
..
— indicates the parent directory.
The Linux ls
command displays information about directories and files located in folders. The command name comes from the word "list." By specifying additional options, you can customize the output format as needed.
Syntax of the command:
ls [OPTIONS][FILE|FOLDER]
In place of [OPTIONS]
, you specify the desired option and then refer to a file [FILE]
or directory [FOLDER]
. If you do not specify a directory name, you will get a list of the contents of the directory you are currently working in by default.
In some cases, there is no need to enter the folder itself. You can use a simplified version by specifying the path in the context of the ls
command:
ls /PATH/TO/FILE
Methods for specifying the path are described above.
-a
– display all contents of the directory, including hidden files (their names start with a dot).
-A
– display all files except hidden ones.
--author
– display the author information.
-b
– output the file name, even if it contains special characters that are not visible when printed.
-B
– do not display backup copies. Backup copies are recognized by the tilde ~
at the end of the name.
-c
– display files sorted by the time of the last modification. By default, new files are listed first.
-C
– display files in columns.
-d
– display information about the directory without showing its contents.
-f
– disable sorting.
-F
– enable visibility of the object type. The object type can be identified by the assigned symbol at the file name's end.
--full-time
– display complete information, including time in ISO format.
-i
– display the inode where the file is located.
-l
– display a long list with detailed information.
-g
– similar to the -l
option, but without displaying the owner's name.
-m
– separate list elements with a comma.
-1
(one) – display information about only one object per line.
-n
– output objects' names without quotes.
-h
– convert file size values to the required format. By default, file sizes are displayed in bytes without specifying the unit of measurement.
--color
– use or disable coloring of objects when displayed on the screen. You can set variables to three values, one of which is automatic color output:
--color=auto/always/never
-R
– display a list of subdirectories recursively.
-S
– display sorted files by size, from largest to smallest.
-Sr
– opposite of the previous command: files are displayed from smallest to largest.
-u
– sort objects by the time of the last access.
-p
– if the object is a directory, a slash is displayed at the end of the name.
-q
– a question mark if NPC control characters are in the file name.
-T
– set the tab width. By default, it is 8.
-w
– set the column width.
-v
– sort by file version number.
The ls -l
command in Linux allows you to display a long list of all folders. It is often combined with other options to show only selective information.
ls -lr
– display a list of all elements sorted in reverse order;
ls -lt
– display information according to the last modification date;
ls -l --author
– display the document's creator.
In this way, you can combine most options.
In addition to basic files, the ls- la
command displays hidden documents starting with a dot in the name.
There is a sorting switch that allows you to quickly sort the output by the desired parameter, such as size, time, or version.
The syntax of the command is as follows:
ls -l --sort=WORD/-x
After the equals sign, you enter the parameters: size, time, version, extension. You can also enter only the first letter instead of the whole word.
When using the ls
on Linux, files are arranged in columns by default. To display them vertically, horizontally, or list them with a comma, use the command:
--format=WORD/-x
(The function is set as a whole word or symbol)
Across
/-x
– arrange files alphabetically by column.
Horizontal
/-x
– sort output horizontally.
Vertical
/-C
– sort output vertically.
Commas
/-m
– list directory contents separated by a comma.
Long
/-l
– output detailed information about files in a long list.
Single-column
/-1
– arrange all files in one column.
To view file sizes in a specific unit of measurement, use the option:
--block-size=SIZE
After the equals sign, specify the first letter of the unit of measurement (K
, M
, G
, etc.). In this case, the multiplier is 1024. If you specify kilobytes or megabytes, the multiplier is 1000. For this, you can use the separate option --si
.
Let's see how the Linux ls
command is used in practice.
Open a terminal window.
Run the ls
command and specify the path if needed.
To navigate directories, use the cd
(change directory) command using the syntax:
cd [location]
Run the command with arguments; otherwise, it will return to the home directory.
Use the pwd
(Print Working Directory) command to find out the working directory. This command does not use arguments.
Now you can start testing the options you are interested in. Below are some examples of what the inputs should look like and what the output information should be.
First, enter the command in the terminal window without using arguments. In this case, you will get a list of directories and files in the usual format.
ls
Output:
Videos file.html lib Downloads
File7 Pictures Dir
Documents 020.Pcap Public
Display files in reverse order:
ls -r
Output:
Public 020.Pcap Documents
Dir Pictures File7
Downloads lib file.html Videos
Next, to display a long list, use the -l
option:
ls -l
Output:
drwxr-xr-x. 3 root root 1785 Jun 29 10:11 Videos
-rw-r--r--. 2 root root 989 Aug 10 12:38 file.html
-rw-r--r--. 2 root root 989 Aug 10 12:38 lib
drwxr-xr-x. 4 root root 1580 Jul 16 01:20 Downloads
-rw-r--r--. 1 root root 3948 Aug 09 03:01 File7
drwxr-xr-x. 3 root root 5170 May 28 13:40 Pictures
drwxr-xr-x. 4 root root 3580 Jun 14 17:45 Dir
drwxr-xr-x. 1 root root 28320 Jul 25 10:11 Documents
-rw-r--r--. 2 root root 1444 May 27 17:45 020.Pcap
drwxr-xr-x. 1 root root 32150 Jun 10 09:58 Public
Now let's interpret what was displayed on the screen using the long list option.
Column 1: The type of file. If the line starts with a hyphen, it is a regular file. If the line begins with the letter d, it is a directory.
Column 2: The next 9 letters and symbols represent access rights to these elements in the file system. The letter r gives the right to read the file, x the right to write to the file, xr the right to execute the file.
Column 3: The number indicates how many hard links point to this file.
Column 4: Displays the creator of the object and the file group.
Column 5: The file size.
Column 6: Time data when the last changes were made.
Column 7: The name of the element for which information is displayed.
Let's work with this list. Now display hidden files using the following option:
ls -a
Output:
.opera .gconf . Videos file.html
.libreoffice lib Downloads .cshrc File7 ..
Pictures .pki Dir 020.Pcap Public
Sort files by last modification date:
ls -lt
Output:
-rw-r--r--. 2 root root 1444 May 27 17:45 020.Pcap
drwxr-xr-x. 3 root root 5170 May 28 13:40 Pictures
drwxr-xr-x. 1 root root 32150 Jun 10 09:58 Public
drwxr-xr-x. 4 root root 3580 Jun 14 17:45 Dir
drwxr-xr-x. 3 root root 1785 Jun 29 10:11 Videos
drwxr-xr-x. 4 root root 1580 Jul 16 01:20 Downloads
-rw-r--r--. 2 root root 989 Aug 10 12:38 file.html
drwxr-xr-x. 1 root root 28320 Jul 25 10:11 Documents
-rw-r--r--. 1 root root 3948 Aug 09 03:01 File7
-rw-r--r--. 2 root root 989 Aug 10 12:38 lib
Test the -F
option to find out which objects are directories:
ls -F
Output:
Videos/ file.html Downloads/
File7 Pictures/ Dir/
Documents/020.Pcap Public/
We reviewed a basic usage of the ls
command in Linux for working with the file system. This tool is available in all operating system distributions and is much more efficient than viewing document properties through a graphical user interface.