In the previous article, by the time we got to the microservices series: The ELK Build Guide for Distributed Logging, we had basically covered the microservices series and had a hard time deciding what to write next.

After some thought, I decided to write a few articles about Linux commands. After all, I often deploy projects on Linux servers. You may ask, shouldn’t deployment be an operation and maintenance job? Sorry, small companies do not have professional operation and maintenance, we develop our own. So it’s worth familiarizing yourself with some common Linux commands.

Of course, the main purpose is to write down their own, the impression will be more profound, and convenient to consult later, after all, the collection of other people’s articles are basically in the collection folder to eat ash ha ha, it is better to write down their own hands. This post probably won’t have too many commands for now, but it should be updated.

I. Files and directories

Post a picture of the meaning of the various directories in Linux system, from the 20,000 word system summary, with you to implement Linux command freedom?

1. The CD command

One of the most basic and commonly used commands to switch directories. The parameter can be absolute or relative path.

 cd /            Switch to the root directory
 cd ~            Switch to /root
 cd.Switch to the upper-level directory
 cd /usr/local/  # switch to /usr/local (absolute path)
 cd ./java/      # Switch to the Java directory in the current directory. Represents the current directory, or simply CD Java /
 cd ../java/     Switch to the Java directory in the upper directory, ". Indicates the directory at the previous level
Copy the code

2. The ls command

Select * from ‘list’ where you want to view the contents of the current folder.

 -a      # list all files in the current folder, along with hidden files. File)
 -l      The ll command is equivalent to the shortcut key for listing the details of the files in the current folder, including file properties and permission data, etc
 -h      List file sizes in readable format (GB, kB, etc.)
 -R      List with the contents of subdirectories (recursively)
 -i      Sort files by the time they were last modified
Copy the code

Note: These parameters can be used in combination, for example

 ls -lh      View the file size while listing the file details
 ls -alht    You should be able to infer the meaning of this combination command
Copy the code

3. The cp command

Copy is used to copy files. The common parameters are as follows:

 -a      # copy the features of the file together
 -p      # copy with the attributes of the file instead of using the default, similar to -a, often used for backups
 -i      If the target file already exists, the operation will be asked before overwriting
 -r      # recursive continuous replication, used for directory replication behavior
 -u      The target file will only be copied if it differs from the source file
Copy the code

Such as:

 cp -a file1 file2           Copy file file1 to file file2 with all the features of the file
 cp file1 file2 file3 dir    Copy files file1, file2, file3 to dir
Copy the code

4. The mv command

Move is used to move files, directories, or rename files.

 -f      If the target file already exists, it will be overwritten without being asked
 -i      If the target file already exists, it will ask whether to overwrite it
 -u      If the target file already exists and is newer than the target file, it will be updated
Copy the code

Such as:

 mv file1 file2 file3 dir    # move files file1, file2, file3 to dir
 mv file1 file2              Rename file file1 to file2
Copy the code

5. The rm command

Remove is used to delete files or directories. In Linux, there is no recycle bin and it is difficult to recover after deletion. Therefore, exercise caution when performing this operation.

 -f      Ignore nonexistent files and no warning message will appear
 -i      # Interactive mode, before deleting will ask the user whether to operate
 -r      # Recursive delete, most commonly used for directory deletion, is a very dangerous parameter
Copy the code

Such as:

 rm -i file      # delete the file file
 rm -rf dir      # Force to delete all files in dir
Copy the code

6. The PWD command

Displays the path to the current directory

7. Which command

View the directory where the command executable file resides, for example:

 which java
Copy the code

8. The du command

Disk usage is used to display the size of a directory or file.

 -h      # Improve the readability of information in units of K, M, and G
 -a      # list the size of the file in the directory
 -s      # display total size, not specific information
 -b      Display directory or file size in bytes
Copy the code

Such as:

9. The cat command

Displaying the entire contents of a file at once, better suited for viewing small files, concatenate, or more officially, concatenate files and print them to a standard output device. Common parameters are as follows:

 -n      # Number all output lines starting with 1
 -b      # is similar to -n, except that blank lines are not numbered
Copy the code

Such as:

 cat -n info.log     Display the entire contents of info.log with the line number
 cat -n textfile1 > textfile2    Textfile2: TextFile2: textFile2: textFile2: textFile2: textFile2: textFile2
Copy the code

10. Use less

Pagination displays file contents, which is better for viewing large files.

 less info.log
Copy the code

Quick operation:

  • Space bar: forward a page (a screen);
  • bKey: Back a page;
  • Enter key: forward line;
  • yKey: back one line;
  • Up and down keys: Go back or forward a line.
  • dKey: forward half a page;
  • uKey: back half a page;
  • qKey: Stop reading file, abortlessCommand;
  • =Key: display the contents of the current page is the number of lines in the file and some other details about the contents of the page;
  • hKey: Display help document;
  • /Key: After entering the search mode, pressnKey to jump to a matching item, pressNThe key jumps to the previous match item and can also enter a regular expression match.

Such as:

 ps -ef | less       # ps Displays process information via less pagination
Copy the code

There is also a command that corresponds to “more” and “less”, except that “more” cannot be backed up. In daily use, “less” is sufficient.

11. The find command

For finding files, it will go through your actual hard drive to find them, and it allows us to do follow-up with each file we find, which is very powerful.

Find < where > < what > < do >Copy the code
  • Where: Specifies which directory to look for, and all subdirectories of that directory will also be looked for.
  • What: What to look for, depending on the name of the file, its size, or its most recent access.
  • What to do: After the file is found, you can proceed. If this parameter is not specified,findThe command displays only the files found.

The use of find is relatively complex and takes a lot of parameters. I don’t want to list them all, but I’ll just post a few examples of how it works:

 find -name "info.log"           Find the file named info.log in the current directory and subdirectories
 find / -name "info.log"         Find info.log on the entire hard drive
 find /root/ytb -name "*.log"    Find *. Log and other files in the specified directory
Copy the code

Others still have according to the file recent access time search, operation search results and so on generally also do not use, do not stick.

12. The file command

This command is relatively simple and is used to identify file types. Such as:

13. The tar command

The command is used to package or decompress files. The common parameters are as follows:

 -c          Create a new package file
 -t          See which file names are included in the contents of the packaged file
 -x          # solution package or decompression function, can match - C (capital) specifies the directory in which the decompression, note - C, -t, -x cannot at the same time appear in the same command
 -z          Compress/uncompress with gzip support
 -v          # Display the file name being processed during compression/decompression
 -f filename # filename is the file to be processed
Copy the code

Tar has a wide range of parameters, which can be easily used in the following examples:

 tar -czvf test.tar.gz info.log      Zip the info.log file to test.tar.gz
 tar -zxvf test.tar.gz               Zip test.tar.gz
 tar -tzvf test.tar.gz               # list the contents of the compressed file
Copy the code

In addition to tar, Linux also has zip/unzip commands to compress and unzip.zip files (usually from Windows).

If you don’t have these two commands, you can install them:

 yum install zip 
 yum install unzip 
Copy the code

14. The head of command

Display the first few lines of the file (default is 10)

 head gc.log
Copy the code

Parameters:

  • -nSpecify the number of rowshead -n 5 gc.log

15. The tail command

Display the last few lines of the file (default is 10)

 tail gc.log
Copy the code

Parameters:

  • -nSpecify the number of rowstail -n 5 gc.log
  • -fIt checks the file every second for updates,tail -f gc.log

In real development, we often go to the server to check the system error log

Tail -f -n 500 info.log # View the last 500 lines of the log in real timeCopy the code

2. Users and permissions

I’ve actually used both of these things, such as ElasticSearch and Kibana on Linux that require a non-root user to launch.

1. The su command

Used to change the identity of another user, meaning switch user.

 su root         Switch to user root
Copy the code

2. Run the useradd + passwd command

  • useraddAdding a New User
  • passwdChanging a User password

Both commands require root privileges

 useradd es          Add user es to the directory. Add user es to the directory
 passwd es           Change the password of user es
Copy the code

3. The userdel command

To delete a user, you need the permission of user root

 userdel lion        # delete the user name from /home
 userdel lion -r     # delete the corresponding folder under /home
Copy the code

4. The sudo command

Run commands as root

5. Chown command

To change the owner of a file, you need root to run it.

  • -r Recursively sets subdirectories and subfiles
 chown -R es:es /usr/local/ elasticsearch - 8.0Select * from elasticSearch-8.0 folder where users and groups are located
Copy the code

6. The chmod command

To modify the access permission, use the following parameters:

  • -r Changes the file access permission recursively

There’s something deeper about this command, and it’s a little more complicated, and I don’t want to go into the details, but it’s usually used like this:

 chmod 777 info.log              Give info.log the highest privileges
 chmod -R 777 /usr/local/java    # recurse to /usr/local/java/ with the highest permission
Copy the code

3. Text operation

In terms of text operations, the most commonly used command is grep, which is used to find the process number when you need to kill a process during a deployment project.

1. The grep command

The grep command is used to find the qualified strings in the file, in other words, to find the keyword in the file and display the line of the keyword.

Such as:

 grep export /etc/profile
Copy the code

Common parameters:

  • -i Ignore case,grep -i path /etc/profile
  • -n Display line number,grep -n path /etc/profile
  • -v Display only those lines where the search text is not present,grep -v path /etc/profile
  • -r Recursive search,grep -r hello /etcLinux also has a rgrep command that is equivalent togrep -r

Redirect, pipeline

To learn about redirection, we need to understand standard input, standard output, and standard error output in Linux.

The name of the code The operator
Standard input (STDIN) 0 Or < < <
Standard output (STDOUT) 1 >, >>, 1> or 1>>
Standard error output (STDERR) 2 > 2 or more > >

1. Output redirection>

> redirects to a new file. When we run the JAR package, we often redirect the log to a specific file in a specific directory, for example:

 nohup java -jar cloud-system.jar > cloud-system.log &
Copy the code

Note:

Note that if the output file does not exist, it will create a new one. If the output file already exists, it will be overwritten. Be careful not to overwrite other important files when you do this.

2. Output redirection>>

This means redirection to the end of the file, which apends content to the end of the file, unlike the > command, which overwrites the content of the file. Similarly, if the file does not exist, it will be created.

So, for our project startup log, it is best to use the >> command, suggested

 nohup java -jar cloud-system.jar >> cloud-system.log &
Copy the code

3. Output redirection2 >

Standard error output, for example:

 nohup java -jar cloud-system.jar > cloud-system.log 2> error.log &
Copy the code

After startup, the standard output is written to the file cloud-system.log, and 2> error.log is written to the error.log file when an error occurs.

4. Output redirection2 > >

Standard error output (appended to the end of the file) is similar to >>.

5. Output redirection2 > &1

Both standard output and standard error output are redirected to the same place

 nohup java -jar cloud-system.jar > info.log 2>&1 &
Copy the code

6. The pipe|

To use two commands together, with the output of one as the input of the other.

To be honest, the pipe command is quite powerful, so let’s look at an example

【 典 型 范 例 1】

 ls | grep gc.log.00
Copy the code

This command is used to check whether file gc.log.00 exists in the current directory.

Use the ls output as the input of the grep command.

【 example 2】

 cat gc.log | less
Copy the code

Pipe the output of cat as input to less so that the output of CAT can be displayed one screen at a time.

【 典 型 范 例 3】

 ps -ef | grep cloud-system.jar
Copy the code

The ps -ef command is used to list all processes. This command is used to find processes associated with cloud-system.jar

Five, the process

1. The ps command

Process Status is used to display processes in the current system. The ps command displays a static list of processes that is not updated over time. It is a snapshot of the process at the time the ps command is run.

Common parameters:

  • -efList all processes;
  • -uLists the processes this user is running;
  • -auxthroughCPUAnd memory usage to filter processesps -aux | less ;
  • -aux --sort -pcpuCPUUsing descending order,-aux --sort -pmemRepresents a descending order of memory usage
  • -axjfDisplays processes in a tree structure,ps -axjfIt andpstreeThe effect is similar.

We use this command in the pipeline [Example 3], which is very common.

 ps -ef | grep cloud-system.jar
Copy the code

2. The top command

Gets a dynamic list of processes.

  • The processes shown are processor by processor%CPUIn order of usage.

3. Kill command

Kill + PID to end a process.

 kill 11056          End the process whose process id is 956
 kill11056, 11057,# End multiple processes
 kill4568-9# Force to end the process
Copy the code

4. & command

Start the background process. Its disadvantage is that the background process is associated with the terminal. Once the terminal is shut down, the process automatically ends.

 npm run dev &
Copy the code

5. The nohup command

It is used to run commands without hanging up in the background of the system. Exiting the terminal does not affect the running of the program. English full name: No hang up,

The nohup command, by default (when not redirected), outputs a file named nohup.out to the current directory

 nohup npm run dev
Copy the code

The nohup command can also be used in conjunction with & to start the background and exit the terminal without interruption.

 nohup npm run dev &
Copy the code

6. The jobs command

The background process status of the terminal is displayed.

Note:

Jobs is to view the status of the background process of the current terminal. If you exit the terminal and connect to the terminal again, you will not see the background process of the last closed terminal, but the process is still in fact.

Six, systems,

1. The halt command

To shut down the system, you need to be root.

 halt
Copy the code

2. Reboot command

To restart the system, you need to be root.

 reboot
Copy the code

3. poweroff

Run directly to shut down, do not need root identity.

 poweroff
Copy the code

Write here temporarily, should continue to add later.

20, 000 word system summary, with you to implement Linux command freedom