Linux file and directory listing (ls command)

Linux file and directory listing (ls command)

To find out what files are on your system, use the list command ( ls ).



1. Basic list function

1. Basic form of ls command

The most basic form of the ls command displays the files and directories in the current directory
Insert image description here

The list output by the ls command is sorted alphabetically (sorted by column rather than by row). If the user is using a terminal emulator that supports color, the ls command can use different colors to distinguish different types of files.

2. ls command -F parameter

If you do not have a color terminal emulator installed, you can use the ls command with the -F parameter to differentiate files and directories
Insert image description here

3. ls command -a parameter

Use the ls command to display the files and directories in the current directory. In Linux, hidden files are often used to save some configuration information. These files are not displayed in the default ls command output, and we also call them hidden files.

注:在 Linux 中,隐藏文件通常是文件名以点号( . )开始的文件

To display hidden files together with ordinary files and directories, use the -a parameter.
Insert image description here

4. ls command-R parameter

The -R parameter is another parameter of the ls command, also called the recursive option. It can list files in subdirectories contained in the current directory. If there are many directories, the output will be very long.
Insert image description here

注:参数选项并不一定得像上面的例子中那样分开输入:ls -F -R。也可以直接合并输入:ls -FR


2. Display a long list (-l parameter)

In its basic output listing, the ls command does not output much information about each file. To display additional information, another commonly used parameter is -l. The -l parameter produces output in a long listing format, containing more information about each file in the directory.
Insert image description here

Output in long list format lists individual files or directories on each line. In addition to the file name, there is other useful information in the output. The first line of output shows the total number of blocks contained in the directory. After this, each line contains the following information about the file (or directory):

  • File type, such as directory (a), file (-), character file (c), or block device (b)
  • File permissions
  • The total number of hard links to the file
  • Username of the file owner
  • The group name of the group the file belongs to
  • The size of the file in bytes
  • The time the file was last modified
  • File name or directory name

3. Filter the output list (wildcard)

By default, the ls command outputs all non-hidden files in a directory. Sometimes this output will appear too much, which is not conducive to querying.

The ls command also supports defining filters on the command line. It uses filters to decide which files or directories should be shown in the output.

This filter is just a string that performs simple text matching. You can add this filter after the command line parameters you want to use:
Insert image description here

When the user specifies the name of a specific file as a filter, the ls command will only display information for that file.

Sometimes you may not know the exact name of the file you are looking for. The ls command recognizes standard wildcard characters and uses them in filters for pattern matching:

  • Question mark (?) represents a character
  • An asterisk ( * ) represents zero or more characters

Question marks can be used in filter strings to replace any single character.
Insert image description here

Similarly, an asterisk can replace zero or more characters
Insert image description here

Using asterisks and question marks in filters is called file globbing, and refers to the process of pattern matching using wildcards.

The official name of wildcards is metacharacter wildcards. In addition to asterisks and question marks, there are more metacharacter wildcards available for file extension matching. Square brackets can be used.
Insert image description here

In the above example, we used square brackets and two characters that may appear at specific positions: s or x.Square brackets indicate a character position and give multiple possible choices

You can list the characters to be selected like the example above, or you can specify a character or number range, such as letter range [a - z], number range [1 - 9]
Insert image description here

Additionally, you can use an exclamation point (!) to exclude unwanted content
Insert image description here


If the article is helpful to you friends, please welcome me! ! !

In addition, if there are any errors in the article, you are welcome to criticize and correct me! ! !

Guess you like

Origin blog.csdn.net/qq_46286412/article/details/133270669