Detailed explanation of ls command and common parameters

Detailed explanation of ls command and common parameters

♢ \color{red}{\diamondsuit}

Detailed explanation of ls command

ls command: full name list, display the file name and related attributes of the file. (files and subdirectories contained in the current working directory)

ls [-aAdfFhilnrRSt] 文件名或目录名称.. 
ls [--color={
    
    never,auto,always}] 文件名或目录名称..
ls [--full-time] 文件名或目录名称..
parameter meaning
-a all, display all files and directories (hidden files starting with . will also be listed)
-A Same as -a, but do not list "." (current directory) and "..." (parent directory)
-l Displays a list of contents under a directory in long format, including file permissions, number of links, owner name and group owner, file size, last modification date time, and file/directory name
-r reverse, output the sorting results in reverse, for example: the original file name is from small to large, and the reverse is from large to small
-R –recursive, listed together with the contents of subdirectories, equal to all files in this directory will be displayed
-S sort by file size. Sort by file size, not file name
-t sort by modification time, sort by file modification time (from the latest)
-d Only list the directory itself, not the file data inside the directory (commonly used)
-f List results directly without sorting (ls sorts by filename by default)
-F According to the file, directory and other information, give additional data structure, for example: *: stands for executable file; /: stands for directory; =: stands for socket file; |: stands for FIFO file
-g like -l, but doesn't list owners
-G, no-group, do not list any information about the group
–author Print out the author of each file
-n Like -l, replace names with numeric UIDs and GIDs
-h List file sizes in human-readable form (e.g. GB KB etc.)
-c Output the ctime of the file (the time when the file status was last changed), and sort by ctime
-C List items from top to bottom
--full-time Show full time format
--time Output access time or change permission attribute time (ctime) instead of content change time (modification time)
--color=never Do not give color display based on file properties
--color=always display color

The above are the more commonly used parameters that I have extracted. In fact, there are many more, you need to use ls --help or man ls or info ls to query

Example 1: List all files under the main folder (including attributes and hidden files)

[root@study ~]# ls -al ~
total 56
dr-xr-x---. 5 root root 4096 Jun 4 19:49 .
dr-xr-xr-x. 17 root root 4096 May 4 17:56 ..
-rw-------. 1 root root 1816 May 4 17:57 anaconda-ks.cfg
-rw-------. 1 root root 6798 Jun 4 19:53 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-rw-rw-. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 176 Jun 3 00:04 .bashrc_test
drwx------. 4 root root 29 May 6 00:14 .cache
drwxr-xr-x. 3 root root 17 May 6 00:14 .config

You can see several files starting with ., as well as directory files (.) (…) .config

Example 2: Continuing from the above question, the color is not displayed, but the type (type) represented by the file name is displayed at the end of the file name

[root@study ~]# ls -alF --color=never
total 56
dr-xr-x---. 5 root root 4096 Jun 4 19:49 ./
dr-xr-xr-x. 17 root root 4096 May 4 17:56 ../
-rw-------. 1 root root 1816 May 4 17:57 anaconda-ks.cfg
-rw-------. 1 root root 6798 Jun 4 19:53 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-rw-rw-. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 176 Jun 3 00:04 .bashrc_test
drwx------. 4 root root 29 May 6 00:14 .cache/
drwxr-xr-x. 3 root root 17 May 6 00:14 .config/

Example 3: Completely display the modification time of the file (modification time)

[root@study ~]# ls -al --full-time
total 56
dr-xr-x---. 5 root root 4096 2015-06-04 19:49:54.520684829 +0800 .
dr-xr-xr-x. 17 root root 4096 2015-05-04 17:56:38.888000000 +0800 ..
-rw-------. 1 root root 1816 2015-05-04 17:57:02.326000000 +0800 anaconda-ks.cfg
-rw-------. 1 root root 6798 2015-06-04 19:53:41.451684829 +0800 .bash_history
-rw-r--r--. 1 root root 18 2013-12-29 10:26:31.000000000 +0800 .bash_logout
-rw-r--r--. 1 root root 176 2013-12-29 10:26:31.000000000 +0800 .bash_profile
-rw-rw-rw-. 1 root root 176 2013-12-29 10:26:31.000000000 +0800 .bashrc
-rw-r--r--. 1 root root 176 2015-06-03 00:04:16.916684829 +0800 .bashrc_test
drwx------. 4 root root 29 2015-05-06 00:14:56.960764950 +0800 .cache
drwxr-xr-x. 3 root root 17 2015-05-06 00:14:56.975764950 +0800 .config

As you can see, the "Time" field above changes to the full format. Generally speaking, ls -al only lists the time in the current short format, and sometimes the year is not listed,
so that --full-timeyou can check the more correct full time format.

In fact, there are many usages of ls, including viewing files

Note: Sometimes you will see that the input llis equal to the input ls -lis the same.

What do the displayed columns mean after executing ls -l in Linux?

for example

drwxr-xr-x   2 root root  4096 Mar 14 04:03 bin
drwxr-xr-x   4 root root  1024 Feb 15 14:45 boot

insert image description here
The picture comes from [Bird Brother Linux Private Kitchen Basic Edition-Fourth Edition].pdf

Detailed explanation of each column information in LinuxThe picture comes from the blogger Linux ls -l The meaning of each column of the output information

Column 1: The first letter d indicates the file type . '-' (minus sign): ordinary file; d: directory file; l (lowercase l character): link file; b: block device file; c: character device file; s: data interface file; p: data transmission file (FIFO, pipe);.
The next 9 characters are about file permissions (r: read, w: write; x: execute executable permission, -: no permission). The positions of these three permissions will not change.
If there is no permission, it will appear Minus sign [-] only.
The first 3 rwx characters are the owner permissions of the file , the second set of rx are the group user permissions (the permissions of the owner in the group) , and the last rx are other user permissions .
Column 2: How many links point to the file.
Column 3: The owner of the file/folder.
Column 4: All group names for this file/folder.
Column 5: The size of the file/folder in bytes. The size of directories is always 4096 bytes.
Column 6: Indicates the last modification time of the file.
Column 7: indicates the file name or directory name.

If you are still a little confused after reading the above, then use the following examples to deepen your understanding
.

-rwxr-xr--

It can be divided into four parts: [-][rwx][rx][r--]

  • -: represents this file type file;
  • rwx: the permission of the owner, in this example, it is readable, writable, and executable;
  • rx: permissions of users in the same group, readable and executable in this example;
  • r--: Other user permissions, in this case readable, which means read-only.

Example: Assuming that test1, test2, and test3 belong to the group testgroup, if there are the following two files, please explain the owners of the two files and their related permissions?

-rw-r--r-- 1 root root 238 Jun 18 17:22 test.txt
-rwxr-xr-- 1 test1 testgroup 5238 Jun 19 10:25 ping_tsai

Answer:
The owner of the file test.txt is root, and the group it belongs to is root. As for the permissions, only the root account can access this file, and others can only read this file; the
owner of the other file ping_tsai is test1, and the group it belongs to is testgroup. Among them, test1 has readable, writable and executable rights for this file; while test2 and test3 in the same group and test1 are also group accounts of the testgroup, they can only be read, executable but not writable (that is, they cannot be modified) );
As for other people who have not joined the testgroup, they can only read, not write or execute!

Example question: Continuing from the previous question, if the directory is as follows, can members of the testgroup group and others (others) enter this directory?

drwxr-xr-- 1 test1 testgroup 5238 Jun 19 10:25 groups/

Answer:
The file owner test1[rwx] can do any work in this directory;
and the accounts of the testgroup [rx], such as test2 and test3, can also enter this directory to work, but they cannot write in this directory As
for other's permission [r–] although there is r, but because there is no x permission, users of others cannot enter this directory!

If you use the -n parameter, what you see is like this, what does 0 mean?
If you are confused, then you need to understand the UID and GID of linux

drwxr-xr-x   2 0 0  4096 Mar 14 04:03 bin
drwxr-xr-x   4 0 0  1024 Feb 15 14:45 boot
drwxr-xr-x   6 0 0  4096 Jan 12 10:26 data
drwxr-xr-x  10 0 0  3520 Apr 26 15:38 dev

[root@study ~]# ls -al .bashrc
-rwxr-xr-x. 1 root root 176 Dec 29 2013 .bashrc

Brainstorming, organizing is not easy

National High Speed ​​Network Center: http://ftp.twaren.net/Linux/CentOS/7/isos/
Official website: http://isoredirect.centos.org/centos
Official website: http://mirror.centos.org/centos /7/iso

Reference article source:
[Niaoge Linux Private Kitchen Basic Edition]
Linux ls command
Linux ls -l The meaning of each column of the output information

Guess you like

Origin blog.csdn.net/weixin_41948075/article/details/123472356