Linux - common commands ls

Posted by Benny Johnson on Sun, 05 Dec 2021 00:55:19 +0100

Directory processing command ls

LS (English meaning list) is used to view the files in the directory. The basic format is LS - parameter option; It should be clear that the parameters and options behind the LS command are optional. It is OK to directly execute ls. The main function of parameters and options is to adjust the command function. Next, we will use code to specify the function of parameters and options, and introduce several common parameters

[root@linux ~]# ls
anaconda-ks.cfg       Public video document music
initial-setup-ks.cfg  Template picture download desktop

[root@linux ~]# ls -a
.                .bash_profile  .dbus                 .tcshrc      Video desktop
..               .bashrc        .esd_auth             .viminfo     picture
anaconda-ks.cfg  .cache         .ICEauthority         .Xauthority  file
.bash_history    .config        initial-setup-ks.cfg  public         download
.bash_logout     .cshrc         .local                Template         music

-A option: compared with ls, ls -a displays more files after execution, and more files begin with a dot. At this time, some students may wonder what - A is for? Why do you add a parameter a to add so many files for no reason? In fact, a is very easy to understand. The original English meaning of a is all, that is, all files, including hidden files. I believe you are not familiar with hidden files. There are many hidden files in Windows system, such as viruses. Although you know they are on your computer, you can't find them, However, unlike windows, Linux is not a virus. The system helps us hide these files. We just hope we don't tamper with these files. Therefore, - A plays an obvious role in viewing all files (including hidden files)

[root@linux ~]# ls
anaconda-ks.cfg       Public video document music
initial-setup-ks.cfg  Template picture download desktop

[root@linux ~]# ls -l
 Total consumption 8
-rw-------. 1 root root 1608 11 June 26-19:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 1656 11 June 26-19:47 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 11 June 26-13:57 public
drwxr-xr-x. 2 root root    6 11 June 26-13:57 Template
drwxr-xr-x. 2 root root    6 11 June 26-13:57 video
drwxr-xr-x. 2 root root    6 11 June 26-13:57 picture
drwxr-xr-x. 2 root root    6 11 June 26-13:57 file
drwxr-xr-x. 2 root root    6 11 June 26-13:57 download
drwxr-xr-x. 2 root root    6 11 June 26-13:57 music
drwxr-xr-x. 2 root root    6 11 June 26-13:57 desktop

-l option: let's say the conclusion directly. The original English meaning of l is long, that is, to display the file information in long format. Next, we will divide the results into seven parts and explain them one by one

1.-rw-r--r -- in fact, it can be divided into - / rw-/r--/r -- these four parts. The first part represents the type of file. If - represents binary file, if d represents directory, and if l represents soft link (to be explained later); The second, third and fourth parts are actually the same, but they represent different permissions. The second part represents the permissions of the owner, the third part represents the permissions of the group, and the fourth part represents the permissions of others; Some friends may have fainted after hearing this. Let's take a simple example. When I bought a game console in college, I was the owner of the game console. I can play games or watch movies, but my roommates knew that they wanted to play after I bought the game console, so I told them the password, At this time, they can be regarded as a group -- > roommate group. For the game console, I am the owner and they are the group, but can they play, and can other dormitories play? Of course not, because others don't know the password. They are others for the game console  . After the example is finished, let's go on to say the role of rwx. r(read) means that you have read permission on the file and can view the file, w(write) means that you have write permission on the file and can modify the file, and x(execute) means that you have execute permission on the file and can run the file. Let's remember this first. In fact, rwx has different meanings for files and directories, but we won't talk about it first, I'll explain later. Here's a general impression

2. There's nothing to say about this. The reference count represents the number of times the file is referenced. Just understand

3.root means that the owner of this file is root

4.root means that the group of this file is root

5. File size

6. Last modification time of the document

7. File name

rw-r--r--
user ownergroup ownerOthers
r read w write (- indicates no)r read  r read  
[root@linux ~]# ls -lh
 Total consumption 8.0K
-rw-------. 1 root root 1.8K 12 March 21:19 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 12 March 21:22 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 12 March 21:22 public
drwxr-xr-x. 2 root root    6 12 March 21:22 Template
drwxr-xr-x. 2 root root    6 12 March 21:22 video
drwxr-xr-x. 2 root root    6 12 March 21:22 picture
drwxr-xr-x. 2 root root    6 12 March 21:22 file
drwxr-xr-x. 2 root root    6 12 March 21:22 download
drwxr-xr-x. 2 root root    6 12 March 21:22 music
drwxr-xr-x. 2 root root    6 12 March 21:22 desktop

-H option: we have introduced the seven parts, and we already know that the fifth part represents size, but is it byte size or in K or M? We don't know, and we have to calculate the size of bytes by ourselves. This display is not humanized at all, so we have the - h parameter (originally human in English). After using - H, we display our customary units (k, M)

[root@linux /]# ls -ld /root
dr-xr-x---. 14 root root 4096 12 April 20:12 /root

-d option: we've been talking about files. If we want to view directory information, we find that we can't view it. That's because to view directory information, we need to add the - d option. In addition, careful partners may also find that the above command has one more option. We've been talking about parameters and didn't mention options. In fact, the options are very simple, If the current location is where you need to view, the option can be left blank. ls -l views the files in the current directory by default. If you want to view the files in / root in / home directory, you must add / root directory, that is, ls -l /root. The parameter is used to adjust the function and the option is used to adjust the location to be executed

  Summary: ls is used to view files in the directory, - a is used to view all files, including hidden files, - l is used to display file information in long format, - h is used for user-friendly display, and - d is used to view directory information. In addition, parameters can be used together, that is, ls -lh, ls -la, etc. Finally, there are far more parameters, But we can't master them all. We just need to master the commonly used commands, which is very important, because ls can be said to be the most commonly used command for learning Linux. That's all for today. Learn more and practice more in order to make progress faster and encourage each other!

 

Topics: Linux Operation & Maintenance CentOS bash