Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Welcome to comment on the deficiencies in the text!

40, file management in the most important ls command, and the file type, file permission identifier

This is the 40th article in our series on Linux Minimalism. Here are some useful facts about Linux.

Ls Is used to view file information in Linux. The reason this is the most important command is that at the heart of the entire Unix (or Linux) philosophy is “Everything is a file!”

Drivers, terminals, network cards, monitors, hardware devices, software programs, etc., are all files in Linux. Knowing how to view files gives you an insight into Linux.

For example, to print or view the current working directory:

[root_test@VM_0_15_centos test] $pwd
/home/root_test/test
Copy the code

The PWD command

Print work directory (PWD) print work directory (PWD)

The ls command

Ls (List files) Lists the files in the current directory. You can view the files and file information in the specified directory.

You can view the file name, type, permission, owning user and group, and modification time.

Ls Displays the files in the current directory

[root_test @ VM_0_15_centos ~] $ls axel - 2.4-9. El7. X86_64. RPMtest  Y6kkd1d
Copy the code

Ls Displays the files in the specified directory

Ls is followed by a directory to view the files in this directory.

For example, look at the file ls/in the root directory.

Look at the files in /etc/my.cnf.d.

[root_test@VM_0_15_centos ~]$ ls /etc/my.cnf.d
hostname  xzdiff  xzgrep
Copy the code

Note, ls column out of the file, different colors, indicating different permissions!

Ls Displays multiple directories or files

Ls can be followed by multiple files or directories with Spaces.

Check the current directory, root directory, root directory, and my.txt file as follows.

[root_test@VM_0_15_centos ~]$ ls ./ / /root test/my.txt
test/my.txt /: bin data etc lib lost+found mnt proc run srv sys usr boot dev home lib64 media opt root sbin swap_file tmp var ./: Axel - 2.4-9. El7. X86_64. RPMtest  Y6kkd1d
ls: cannot open directory /root: Permission denied
Copy the code

ls -lView a long list of files

Ls -l -l indicates long listing format.

Using this command, you can view a file in a list and in more detail.

$ ls -l
total 64
-rw-rw-r-- 1 root_test root_test 54908 Apr 16  2014 axel-2.4-9.el7.x86_64.rpm
drwxrwxr-x 2 root_test root_test  4096 Oct 26 21:52 test
-rw-rw-r-- 1 root_test root_test     0 Sep  3 17:40 Y6kkd1d
Copy the code

Ls-l long list format details

You can run the ls -l command to display the file information in the long list format, which contains more information about the file.

Let’s take the following two actions:

-rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64. RPM drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52test
Copy the code

Format,

The output in long list format lists individual files or directories on each line. Not only the file name, but also the file type (attributes), size, permissions and other details:

  • The first line shows the number of file system blocks in the directory. Represents the size of all files in the directory.

  • Each subsequent line displays the long-format information for the file.

Long format information is divided into 7 large columns and 10 small items.

File mode【 file mode 】 Number of links Owner name [user name] Group name [Group name] Number of bytes in the file The date of last modification 6. filename
File Type[File Type] Owner rights Permission of the owning group Other User rights Number of links The owner of the Subordinate to the group The size of the file in bytes Last Modified time The file name
rw- rw- r– 1 root_test root_test 54908 Apr 16 2014 Axel – 2.4-9. El7. X86_64. RPM
d rwx rwx r-x 2 root_test root_test 4096 Oct 26 21:52 test

  1. The first column has 10 bits. The first bit represents the document type, d represents the directory, and – represents a common file. The last nine characters correspond to the permissions of the three identities in the following order: owner, Group, and others. The permissions are readable, Writable, and ExcuTable in the following order: read, write, and execute permissions. For example, -rwxr-xr– indicates that the current document is a common file, and the owning users can read, write, and execute the file. Users in the same group can read and execute the file, while others have only the read permission.

  2. The second column represents the link count, indicating how many files are linked to the inode number. It represents the number of hard links.

The number of hard links does not represent the actual number of files.

Hard links (hard link, also known as a link) is a link made through an index node.

In Linux file systems, files stored in disk partitions are assigned a number, no matter what type, called an inode number (inode number).

For the inode see the last section attached: Introduction to inodes and Ls-Li

  1. The third column represents the owner, the user to which the file belongs

  2. The fourth column indicates the owning user group.

  3. The fifth column represents the document size in bytes.

  4. The sixth column represents when the document was last modified, not when it was created! Abbreviated month includes: month, day, hour, minute, etc.

  5. The seventh column represents the document name. Hidden files begin with a dot

About the number of file blocks and the first line-kThe problem with choice

The file size shown in the first line is 512 bytes by default, unless -k is specified, which is in 1024 bytes (1kB blocks).

What does the first line “Total” indicate while executing the ls -L command in Linux? [Closed], and the POSIX documentation says so.

Can be actually tested in plus-kAnd do not add-kIn the case of “option”, the number displayed by Total is the same, the specific reason is not found relevant information, I do not know whether the usage is wrong.

Without -k, the option is 64:

$ ls -l
total 64
-rw-rw-r-- 1 root_test root_test 54908 Apr 16  2014 axel-2.4-9.el7.x86_64.rpm
drwxrwxr-x 2 root_test root_test  4096 Oct 26 21:52 test
-rw-rw-r-- 1 root_test root_test     0 Sep  3 17:40 Y6kkd1d
Copy the code

Add the -k option to 64:

$ls-LK total 64 - RW-RW-r -- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64. RPM drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52test
-rw-rw-r-- 1 root_test root_test     0 Sep  3 17:40 Y6kkd1d
Copy the code

use--block-sizeOption sets the block size displayed in the first line

To see the display of block sizes in different units, use the –block-size option.

Ls -l –block-size=M will be displayed as a file size similar to MiB. If you want to use MB (10^6 bytes) instead of MiB (2^20 bytes), use –block-size=MB instead.

The actual test –block-size=B and –block-size=MB shows no difference.

$ ls -l --block-size=M
total 1M
-rw-rw-r-- 1 root_test root_test 1M Apr 16  2014 axel-2.4-9.el7.x86_64.rpm
drwxrwxr-x 2 root_test root_test 1M Oct 26 21:52 test-rw-rw-r-- 1 root_test root_test 0M Sep 3 17:40 Y6kkd1d $ ls -l --block-size=k total 64K -rw-rw-r-- 1 root_test RPM drwxrwxr-x 2 root_test root_test 4K Oct 26 21:52test
-rw-rw-r-- 1 root_test root_test  0K Sep  3 17:40 Y6kkd1d
Copy the code

Also, you can use any given numeric unit you want, such as –block-size=1M, –block-size= 2K.

$ ls -l --block-size=2k
total 32
-rw-rw-r-- 1 root_test root_test 27 Apr 16  2014 axel-2.4-9.el7.x86_64.rpm
drwxrwxr-x 2 root_test root_test  2 Oct 26 21:52 test
-rw-rw-r-- 1 root_test root_test  0 Sep  3 17:40 Y6kkd1d
Copy the code

about-hOption (Human readable)

Ls-lh can obtain long format lists and human readable file sizes.

$ ls -lh
total 64K
-rw-rw-r-- 1 root_test root_test  54K Apr 16  2014 axel-2.4-9.el7.x86_64.rpm
drwxrwxr-x 2 root_test root_test 4.0K Oct 26 21:52 test
-rw-rw-r-- 1 root_test root_test    0 Sep  3 17:40 Y6kkd1d
Copy the code

Ls -s Displays the disk usage

$ls -s total 64 60 axel-2.4-9.el7.x86_64.rpm 4test   0 Y6kkd1d
Copy the code

Ls -a Displays hidden files and folders, that is, all files in the specified directory

In Linux, files or filenames starting with a. Are hidden files.

The default ls view file does not show hidden files. You can view all files with the -a (–all) option.

$ls -- all.axel-2.4-9.el7.x86_64. rpm.bash_logout.bashrc.config.pki.viminfo.. .bash_history .bash_profile .cache .mozillatestY6kkd1d $ls axel - 2.4-9. El7. X86_64. RPMtest  Y6kkd1d
Copy the code

Combined with -l view, ls -al:

$ ls -al total 116 drwx------ 7 root_test root_test 4096 Oct 26 21:52 . drwxr-xr-x. 7 root root 4096 Apr 11 2018 .. -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64. RPM -rw------- 1 root_test root_test 10633 Oct 28 14:04 .bash_history -rw-r--r-- 1 root_test root_test 18 Dec 7 2016 .bash_logout -rw-r--r-- 1 root_test root_test 193 Dec 7 2016 .bash_profile -rw-r--r-- 1 root_test root_test 231 Dec 7 2016 .bashrc drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .cache drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .config drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 .mozilla drwxrw---- 3 root_test root_test 4096 Sep 2 11:41 .pki drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52test
-rw-------  1 root_test root_test  1985 Sep  3 09:44 .viminfo
-rw-rw-r--  1 root_test root_test     0 Sep  3 17:40 Y6kkd1d
Copy the code

ls -lr

Ls-r is usually not used directly but in combination with -l.

-r indicates the display in reverse order. By default, the file name is displayed in reverse order.

$ ls -r
Y6kkd1d  testAxel - 2.4-9. El7. X86_64. RPM $ls axel - 2.4-9. El7. X86_64. RPMtest  Y6kkd1d
Copy the code

Combining with the -l:

$ ls -l -r
total 64
-rw-rw-r-- 1 root_test root_test     0 Sep  3 17:40 Y6kkd1d
drwxrwxr-x 2 root_test root_test  4096 Oct 26 21:52 test
-rw-rw-r-- 1 root_test root_test 54908 Apr 16  2014 axel-2.4-9.el7.x86_64.rpm
Copy the code

-r-t can be displayed in reverse order by time (lS-L-R-t) :

$ls-lrt total 64 -rw-rw-r-- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64. RPM -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52test
Copy the code

All the preceding commands can be abbreviated, for example, ls-lr and ls-lrt.

Ls-r recursively displays files (all files)

-r Displays files in a folder recursively.

$ls-r.: axel-2.4-9.el7.x86_64. RPMtest  Y6kkd1d

./test:
my.txt
Copy the code

Command combination:

$ ls -alrtR .: Total 116-rw-rw-r -- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64. RPM -rw-r--r-- 1 root_test Root_test 231 Dec 7 2016 .bashrc -rw-r--r-- 1 root_test root_test 193 Dec 7 2016 .bash_profile -rw-r--r-- 1 root_test root_test 18  Dec 7 2016 .bash_logout drwxr-xr-x. 7 root root 4096 Apr 11 2018 .. drwxr-xr-x 4 root_test root_test 4096 Jun 21 2019 .mozilla drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .cache drwxrwxr-x 3 root_test root_test 4096 Sep 2 11:36 .config drwxrw---- 3 root_test root_test 4096 Sep 2 11:41 .pki -rw------- 1 root_test root_test 1985 Sep 3 09:44 .viminfo -rw-rw-r-- 1 root_test root_test 0 Sep 3 17:40 Y6kkd1d drwx------ 7 root_test root_test 4096 Oct 26 21:52 . drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52test
-rw-------  1 root_test root_test 10777 Oct 28 14:10 .bash_history

./.mozilla:
total 16
drwxr-xr-x 2 root_test root_test 4096 Jun 10  2014 plugins
drwxr-xr-x 2 root_test root_test 4096 Jun 10  2014 extensions
drwxr-xr-x 4 root_test root_test 4096 Jun 21  2019 .
drwx------ 7 root_test root_test 4096 Oct 26 21:52 ..

./.mozilla/plugins:
total 8
drwxr-xr-x 2 root_test root_test 4096 Jun 10  2014 .
drwxr-xr-x 4 root_test root_test 4096 Jun 21  2019 ..

./.mozilla/extensions:
total 8
drwxr-xr-x 2 root_test root_test 4096 Jun 10  2014 .
drwxr-xr-x 4 root_test root_test 4096 Jun 21  2019 ..

./.cache:
total 12
drwxrwxr-x 3 root_test root_test 4096 Sep  2 11:36 .
drwx------ 7 root_test root_test 4096 Oct 26 21:52 ..
drwxrwxr-x 2 root_test root_test 4096 Oct 28 10:17 abrt

./.cache/abrt:
total 12
drwxrwxr-x 3 root_test root_test 4096 Sep  2 11:36 ..
-rw------- 1 root_test root_test   11 Oct 28 10:17 lastnotification
drwxrwxr-x 2 root_test root_test 4096 Oct 28 10:17 .

./.config:
total 12
drwxrwxr-x 2 root_test root_test 4096 Sep  2 11:36 abrt
drwxrwxr-x 3 root_test root_test 4096 Sep  2 11:36 .
drwx------ 7 root_test root_test 4096 Oct 26 21:52 ..

./.config/abrt:
total 8
drwxrwxr-x 3 root_test root_test 4096 Sep  2 11:36 ..
drwxrwxr-x 2 root_test root_test 4096 Sep  2 11:36 .

./.pki:
total 12
drwxrw---- 2 root_test root_test 4096 Sep  2 11:41 nssdb
drwxrw---- 3 root_test root_test 4096 Sep  2 11:41 .
drwx------ 7 root_test root_test 4096 Oct 26 21:52 ..

./.pki/nssdb:
total 8
drwxrw---- 3 root_test root_test 4096 Sep  2 11:41 ..
drwxrw---- 2 root_test root_test 4096 Sep  2 11:41 .

./test:
total 8
drwx------ 7 root_test root_test 4096 Oct 26 21:52 ..
-rw-rw-r-- 1 root_test root_test    0 Oct 26 21:52 my.txt
drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52 .
Copy the code

File Type flag (File type flag)

  • – (regular file) Regular file

  • D (directory) Directory files

  • B (Block) Block special file, block device file. The most common block device is the hard disk, but many other block devices exist, such as floppy disk drives, Blu-ray readers, and flash memory.

  • C (character) special file, character device file, mostly refers to one-time read device, the typical character device is terminal (terminal can be divided into various types, some physical and some virtual) and keyboard.

  • L (linkage) Indicates the symbol link file

  • F (FIfio-pipe) pipe file, named pipe P, also referred to as P

  • S (Socket) Socket file, local socket

The easiest way to distinguish a block device from a character device is by the way data is accessed.

It is the block device that has random access to the data, and the character device that must be accessed in byte order.

It is recommended to learn about file types in Linux.

Representation of file permissions

  • R = 4, read.
  • W is equal to 2.
  • If x is 1, run.

Executable permission (x) for a directory means access to the directory.

A combination of RWX permissions that can be represented by the sum of their corresponding numbers.

For example, read-executable (rx=5=4+1), read-write (rw =6=4+2), read-write executable (RWX =7=4+2+1).

This is what the chmod command chmod 755, chmod 774, chmod 775, chmod 777 mean.

Sometimes you’ll see chmod 4755 as a four-digit representation. The difference from chmod 755 is that there is an extra bit at the beginning, 4, which means that other users have the same permissions as the owner when executing the file. (Not further confirmed)

Linux command chmod command set permission 777,775,774

Ps: About inode and LS-Li

Inode means “index node” in Chinese.

After each storage device or partition of a storage device (such as a hard disk, floppy disk, USB disk, etc.) is formatted as a file system, it should have two parts, one is an inode and the other is a Block, which is used to store data. Inodes are information used to store such data, including file size, owner, owning user group, and read and write permissions.

Inodes index information for each file, so you have inode values. The operating system is instructed to find the corresponding file the fastest by inode value.

When viewing a directory or file with ls, the -i argument displays the inode number.

$ls-li total 64 492775-rw-rw-r -- 1 root_test root_test 54908 Apr 16 2014 axel-2.4-9.el7.x86_64. RPM 499731 drwxrwxr-x 2 root_test root_test 4096 Oct 26 21:52test
492981 -rw-rw-r-- 1 root_test root_test     0 Sep  3 17:40 Y6kkd1d
Copy the code

The inode value of test is 499731. The inode value of Y6kkd1d is 492981.

This section is from Linux Daily Command (26) : Linux file properties.

reference

  • Linux Practical skills 100 lectures

  • How do I make ls show file sizes in megabytes?

  • Linux ls