ls command linux

ls Command in Linux

Commands in the GNU/Linux terminal greatly simplify any interaction with the system. Among them are frequently used ones, such as viewing a list of files, disk space, replacing characters, etc. In this article, the ls command is considered. It shows a list of files of the specified directory, as well as their additional attributes, such as the name of the owner, creation date, and others. It work is independent of the distribution.

Syntax and Parameters of ls Command

The ls command has a fairly large set of options, providing the most detailed information about system objects. The general syntax is as follows:

ls [flags] [/path/to/directory]

Square brackets mean that these parameters are not required for typing, but expand the capabilities of the command. If you enter ls without parameters, a list of file names of the current directory will be displayed, placed by columns from left to right and separated by a tab character. The size of the column is determined by the number of objects and the size of the terminal window.

ls command

Flags filter and customize the output. Many of them have short and long forms. By combining flags, short ones can be written together after one dash, while long ones are always written separately. A small list of options is presented in the table below.

Flag Description
-a, –all all files of the specified directory are shown, including hidden ones (the name of which begins with a dot “.”)
-A, –almost-all all files are displayed, including hidden ones, but links of current and previous directories are not displayed
–author creators of objects are shown; used with the detailed list option
-b, –escape object names include escape sequence
–block-size=SIZE the sizes of objects are displayed in the specified unit of measure (for example, –block-size=M indicates the size in megabytes); used with the detailed list option
-B, –ignore-backups do not show backup files; their names contain at the end the tilde symbol “~”
-c sorting objects by time (without date) of their modification or creation (ctime), starting with new ones; with the -l flag shows ctime and sorts objects alphabetically; with the -lt flag sorts by ctime and shows it
-C output in the form of columns (as without parameters)
–color[=WHEN] colors the displayed objects in different colors depending on their type; the WHEN parameter may contain the values ‘always’, ‘auto’ and ‘never’ (in many distributions the default is ‘auto’
-d, –directory output only directories without showing their contents; efficiently combined with recursive inference
-D, –dired output mode as in Emacs
-f sorting and color formatting are disabled
-F, –classify indicates the type of object in the form of adding to it one of the special characters: * / => @ |
–file-type same as –classify, but without *
–format=WORD additionally displays of the list of objects, where WORD can take one of the following values: across or horizontal (tabular view, but the output is line-by-line), commas (one line, objects separated by commas), vertical (analogue of the -C flag)
-full-time detailed information is displayed in the detailed view mode with the time in ISO format
-g detailed viewing without specifying owners of objects
–group-directories-first directories are located first, and then all other files
-G, –no-group group names are not shown
-h, –human-readable the sizes of objects are shown in an understandable and convenient way for a person
-i, –inode displayed inodes of objects
-r reverse sorting
-R recursive mapping of directories and their contents
-S sorting by size, starting from the larger
-t sort by last modified time

All flags of ls can be found on the corresponding help page.

Regarding links to the current and previous directory. They are indicated by “.” and “..” respectively and are often used, for example, when the absolute path to an object is inconvenient to indicate.

Using ls

Below are examples of using this command in combination with various parameters.

Show root directory contents:

ls /

ls command in linux

Show contents of several directories (for example, / var and /):

list only directories linux

Detailed view mode of the specified directory:

ubuntu ls

Show a list of files in the current directory, separated by commas, sorted by time and date of the last change:

linux ls options

Add Your Comment