preface

After translating the official TypeScript documentation, I built a blog with VuePress and automatically deployed GitHub and Gitee Pages, but eventually decided to build my own site, using Linux commands. So this article to write a basic enough Linux commands, will cover the blog build series of articles used in a variety of commands, easy to query and learn to use.

0. Owner, Group, Others, Root

Linux is a multi-user system that classifies file visitor identities into three categories:

File Owner

When creating a user, Linux creates a home directory for the user. The directory is /home/

. You can use CD ~ to quickly access the home directory. If you want to put a private file, you can put it in your home directory and set it to be viewable only by yourself.

Group

Each user has a user group, which is convenient for assigning permissions to a group of users. When a user is created, a user group with the same name is automatically created.

If a user belongs to multiple user groups, the user needs to switch between user groups to obtain the rights of other user groups.

Others

A user who is neither the file owner nor a member of the group to which the file belongs is someone else.

Super user (Root)

The Root user is a special user who can access all files.

1. Adduser adds the user and passwd changes the password

Add a user named git
adduser git
Git user password
passed git
Copy the code

However, because the created user has low permissions, sometimes we need to raise rights for the user. In this case, we can do as follows:

The sudoers configuration file will be opened
sudo visudo
Copy the code

Note that using this command is safer than using sudo vim /etc/sudoers to edit the sudoers configuration file, in addition to checking the syntax and locking the file during multi-user editing.

After opening the sudoers configuration file, we add the following configuration line:

# Allow git to run any commands anywhere
git ALL=(ALL:ALL) ALL 
Copy the code

Git ALL=(ALL:ALL) ALL:

  • Git represents the user name for which the rule is applied
  • The first oneALLIndicates that the rule applies to all hosts
  • The secondALLIndicates that the rule applies to all users
  • The thirdALLIndicates that the rule applies to all groups
  • The fourthALLIndicates that the rule applies to all commands

After we save and exit, the Git user will get root privileges.

2. Ls Lists files and directories

  1. lsLists files and directories
[root@iZ2ze learn-typescript.git]# ls
branches  config  description  HEAD  hooks  index  info  objects  refs
Copy the code
  1. ls -la-aShow all files and directories (including hidden) and-lDisplay detailed list composition:
[root@iZ2ze learn-typescript.git]# ls -laDrwxrwxr-x 7 git git 132 12月 15 12:33. DRWX ------ 3 git git 127 12月 15 14:51.. Drwxrwxr-x 2 git git 6 12月 15 12:21 branches -rw-rw-r-- 1 git git 66 12月 15 12:21 config-rw-rw-r -- 1 git git 73 12月 15 12月 15 13:10 hooks -rw-rw-r-- 1 git git 23 月 15 12:21 HEAD drwxrwxr-x 2 git git 4096 月 15 13:10 hooks -rw-rw-r-- 1 Git git 217 12月 15 12:33 index DRWXRWXR -x 2 git git 21 12月 15 12:21 info DRWXRWXR -x 10 git git 90 12月 15 12:33 objects DRWXRWXR -x 4 git git 31 12月 15 12:21 refsCopy the code

Each line has seven columns. We use branches as an example to explain the meaning of each column:

drwxrwxr-x 2 git git 6 Sprinkling on December 15 branches
File type and permission information Number of links or level 1 subdirectories The owner of the Subordinate to the group File size, in bytes Last Modified time The file name

Take drwxrwxr-x as an example. There are 10 bits in the first column. The first bit indicates the file type.

The second to fourth digits indicate the owner permission. R indicates the read permission, W indicates the write permission, x indicates the executable permission, and – indicates no permission. The second to fifth digits are RWX, indicating that the owner can read, write, and execute.

Bits 5 through 7, representing group user permissions, also RWX.

The 8th to 10th digits indicate other user permissions. In this case, r-x indicates read and execute permissions, but no write permissions.

Here’s an extra point:

The default permission for creating folders as root is rwxr-xr-x:

[root@iZ2ze www]# mkdir test
[root@iZ2ze www]# ls -lDrwxr-xr-x 2 root root 6 December 17 23:53test
Copy the code

Rw -r–r– rw –r– rw –r– rw –r– rw –r– rw –r– rw –r

[root@iZ2ze www]# touch index.html
[root@iZ2ze www]# ls -l-rw-r--r-- 1 root root 0 12月 17 23:54 index. HTMLCopy the code

This is why we sometimes need to add execute permissions after creating files.

3. Chown Changes the owner or owner group of the file

Chown (change owner) syntax:

# -r: Changes the file ownership group recursivelyChown [-r] Indicates the name of the owner group. Chown [-r] Indicates the name of the owner groupCopy the code

Change the owner of index.html to git:

[root@iZ2ze www]# chown git index.html
[root@iZ2ze www]# ls -

-rw-r--r-- 1 git  root  0 12月 17 23:54 index.html
Copy the code

Change the owner and group of index.html to git:

[root@iZ2ze www]# chown git:git index.html
[root@iZ2ze www]# ls -l

-rw-r--r-- 1 git  git   0 12月 17 23:54 index.html
Copy the code

4. Chmod Changes the file permission

In addition to r w x, permissions can also be represented by numbers. The corresponding relationship between array and letters is as follows:

  • r:4
  • w:2
  • x:1

All of such correspondence, mainly for the convenience of deduction, such as we hope to write a file can be read, that we can set the permissions for 6 (4 + 2), also, if we know a permission for 3, we can deduce the permissions to write executable, because only 2 + 1 equals 3.

Chmod (change mode) ¶

# -r: Changes the file ownership group recursivelyChmod [-r] xyz file or directoryCopy the code

Where xyz represents the permission of Owner, Group and Others respectively. If we set the permission of a file like this:

chomd 750 index.html
Copy the code

We can know that Owner has 7 permission, which means read, write, and execute; Group has 5 permission, which means read, write, and execute; Others have 0 permission, which means read, write, and execute are unavailable. The corresponding letters are rwxr-x–.

In addition to this numeric approach, there is another way to change permissions using symbolic types:

In this way, the Owner, Group and Others are abbreviated as U (User), G and O respectively. A represents all identities, and + – = represents adding, removing and setting a permission. R, w and x continue to represent read, write and execute permission.

chomd u+x,g-x,o-x index.html
Copy the code

Owner adds the execution permission, Group and Others remove the execution permission.

Of course, we can also directly set permissions

chmod u=rwx,g=rx,o=r index.html
Copy the code

The file has the same permissions as -rwxr-xr–.

In addition, we can omit the ugoa identity content and write:

chmod +x index.html
Copy the code

In this case, a is used to add execute permission to all identities.

5. Su switches the identity

Change to a git user
su git
Copy the code

6. Whoami Displays the user name

# whoami 
root
Copy the code

7. PWD Displays the current directory

[git@iZ2ze www]$ pwd
/home/www
Copy the code

9. CD Switch to the working directory

# into the/home/WWW /
cd /home/www

Go to your home directory
cd ~

# enter two layers above the current directory:
cd ../..
Copy the code

10. Mkdir Create a directory

  1. mkdirCreate a directory:
mkdir new_folder
Copy the code
  1. mkdir -pCreate a directory recursively:
mkdir -p one/two/three
Copy the code

11. Touch create file

This command is used to modify the time attribute of a file or directory. If a file does not exist, the system creates a blank file

touch new_file
Copy the code

12. Echo Prints output

Echo is a Shell command used to print output:

# Display escape characters
echo "\"test content\""
Copy the code

Create or overwrite a file with “test content” :

echo "test content" > index.html
Copy the code

To append content, use >> :

[root@iZ2ze www]# echo "test content" > index.html
[root@iZ2ze www]# cat index.html
test content
[root@iZ2ze www]# echo "test content" >> index.html
[root@iZ2ze www]# cat index.html
test content
test content
Copy the code

13. Cat connect the file and print the output

View file contents:

cat ~/.ssh/id_rsa.pub
Copy the code

Empty the index.html content:

cat /dev/null > index.html
Copy the code

Write the contents of index.html to second.html:

cat index.html > second.html
Copy the code

Append the contents of index.html to second.html:

cat index.html >> second.html
Copy the code

Append index.html and second. HTML to third.html:

cat index.html second.html >> third.html
Copy the code

14. Cp Copies files or directories

Copy all files from directory website/ to directory static:

# -r: If the source file is a directory file, all subdirectories and files under that directory will be copied.Cp - r website/staticCopy the code

15. Mv move and rename

File name change:

mv index.html index2.html
Copy the code

Hidden files:

# add to the file name.
mv index.html .index.html
Copy the code

Moving files:

# Just move
mv  /home/www/index.html   /home/static/
# Move and rename
mv /home/www/index.html   /home/static/index2.html
Copy the code

Batch movement:

mv  /home/www/website/*  /home/www/static
Copy the code

16. Rm Deletes a file or directory

# The system will ask
rm file

# -f indicates the direct deletion
# -r indicates that all files in the directory are deleted

Delete all files and directories in the current directory
rm -r  * 

# run
rm -rf /*
Copy the code

17. vi/vim

Linux built-in VI document editor, Vim is a text editor developed from VI.

Basically, VI/VIM can be divided into three modes, namely Command mode, Insert mode and Last line mode. We introduce the three modes as we go along: we execute vim index.html, or create a file if it doesn’t exist:

vim index.html
Copy the code

The interface is as follows:

In command mode, any character entered is regarded as a command. The following commands are commonly used:

  • I Switch to input mode.
  • X Deletes the character at the current cursor position.
  • : Switches to the baseline command mode.

We press I to enter input mode:

In input mode, there is the — INSERT — flag in the lower left corner:

At this point we can do all kinds of typing, and when finished, press ESC to return to command mode:

Now the INSERT in the lower left corner has disappeared. If we want to save and exit, we first enter:, to enter the bottom line command mode:

In baseline command mode, common commands are:

  • W Save file
  • Q exit program

We type wq to save and exit, at which point we will find and create an HTML file.

18. SSH remote connection tool

Note that SSH listening is on port 22.

The basic syntax is:

ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]
Copy the code

Example listening port:

SSH -p 300 [email protected]Copy the code

Open debug mode:

-v verbose mode to print debugging information about the running stateSSH -v [email protected]Copy the code

series

Catalogue address: github.com/mqyqingfeng…

Wechat: “MQyqingfeng”, add me Into Hu Yu’s only readership group.

If there is any mistake or not precise place, please be sure to give correction, thank you very much. If you like or are inspired by it, welcome star and encourage the author.