[Learning Linux from scratch] Common commands and operations

 Hello, hello, everyone~ I am your old friend: protect Xiao Zhou ღ  


This issue brings you common Linux commands and operations. There are three main categories : file operations, directory operations, network operations, creating files touch, creating directories mkdir, deleting files or directories rm, text editor vim, viewing file content cat, file overwrite write echo, file directory copy cp, file directory move or rename mv, and network related commands, ps + grep command, view the specified process, netstat + grep view the port number of the process and the process using the specified port number , sudo performs tasks with special permissions, we can cooperate with some other commands to release the port~ For more command operations, please read below~


This issue is included in the blogger's column : Linux_Protect Xiao Zhouღ's Blog-CSDN Blog

Suitable for programming beginners, interested friends can subscribe to view other "JavaEE Basics".

Stay tuned for more highlights: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★*

The Linux operating system is not a graphical operation interface like windows, but needs to be operated by entering commands or some shortcut keys. Linux also has its own graphical operation interface, but in actual work, it is generally not used.

The advantage of the graphical operation interface is that it is more suitable for the public and easy to operate.

The advantage of the command line operating system is that the execution is more efficient, and it is also a production tool for major enterprises.

  1. The command line data transmission bandwidth is very low, and the graphical interface is essentially a docking command behind it.
  2. The system resource usage is very low and does not need to support graphics processing.
  3. The command line is suitable for implementing batch and repeated operations through scripting languages.

Next, I will bring you the commands that are commonly used on Linux.

After logging in to the cloud server using the terminal software Xshell:

Note: For the Linux operating system, sometimes entering a command without a prompt means that the operation is successful! !


1. Directory operation

1.1 ls (list lists all current files)

Syntax: ls [options] [directory or file]

Function: list lists the files in the current directory/specified directory, which is equivalent to double-clicking a certain folder in Windows.

Common options:

  • -a List all files in the directory, including hidden files starting with .
  • -d Display directories as files instead of the files under them. Such as: ls -d specifies the directory
  • -k Indicates the size of the file in k bytes. ls –alk specifies the file
  • -l Lists detailed information on files.
  • -r Sort directories in reverse.
  • -t Sort by time.
  • -R List files in all subdirectories. (recursive)

These options, also called "command line arguments" require at least one space between arguments and arguments.

Because the blogger here is a new server, no redundant files have been created.

ls followed by a specific directory, you can see the contents of the specified directory.

 Blue indicates directories, white indicates files, and red generally indicates compressed packages or some special files

 ls - l can view the detailed information of the directory:

Linux also provides the ll operation, which is equivalent to ls -l 


1.2 cd (change directory switch directory)

In a Linux system, files and directories on disk are a tree, and each node is a directory or file. This is more like Windows.

Syntax: cd [directory name]

Function: Change the working directory, change the current working directory to the specified directory. (Win double-click other folders)

cd is followed by the directory/path that needs to be switched (relative or absolute paths can be used)

An absolute path on Linux is a path starting with "/"

Example:

cd ..  : 返回上级目录

cd~  或直接 cd : 回到 home 目录

cd- :返回最近访问的目录

Several special directories:

  • / is called the root directory
  • . is called the current directory
  • .. is called the parent directory of the current directory

Absolute path vs relative path
A form like: /usr/share/tomcat/logs/ starting with the root directory is called an absolute path.
A shape like: ./xiaohu starting with . or .. is called a relative path (usually ./ omit not to write)

Extended shortcut keys:

Use the tab key to achieve auto-completion.

For example, if I type the command cd /xiao, and then click tab, it will be automatically completed as cd /xiaohu 

All of our Linux commands can be completed using tabs to speed up efficiency.

Use ctrl + c to re-enter

If you make a wrong command or directory, you can use this shortcut key to cancel the current command.

In xshell, you can use ctrl + l to clear the screen, or directly type the clear command


 1.3 pwd (display the current absolute path)

Display the directory the user is currently in

Sometimes there are too many cd times, and you can’t remember which path you are currently in. You can use pwd to view the current complete path.


1.4 mkdir (create directory)

Syntax: mkdir [options] dirname

Function: Create a directory named "dirname" under the current directory

Common options:

-p, --parents can be a path name. At this time, if some directories in the path do not exist yet, after adding this option, the system will
automatically create those directories that do not exist, that is, multi-level directories can be created at one time

For example:

 You can also simply create a directory


1.5 rm (delete directory or file)

Syntax: rm [options] [dirname / dir]

Function: delete file or directory

Common options:

  • -f Even if the file is read-only, it can be deleted directly
  • -i Confirm whether to delete one by one before deleting
  • -r delete directory and all files under it (recursively)

Note that you can directly rm test.txt to delete files , but you cannot directly delete directories. To delete a directory, you need to use rm -r to delete recursively, and you need to delete all files and subdirectories in the directory at the same time.

This is when there are few files in the directory. If there are hundreds or thousands of files in the directory, it is really a pain to delete. At this time, we can use  rm -rf [dirname] where f means forced deletion. No confirmation is required.

You can also use rm -rf * The * here represents a wildcard character, which can match all files in the current directory, that is, select all.

Precautions! ! ! : There is no concept of recycle bin on Linux, so please be careful when deleting files, and never run rm - rf / "/" represents the top node in the entire file system, once this command is issued The operating system can be deleted for you, especially on the company server in the future! ! !


2. File operation

2.1 touch (create an empty file)

Syntax: touch [options] file

Function: The touch command parameter can change the date and time of a document or directory, including storage time and change time, or create a new file that does not exist.

[root@iZ0jlcqjnf4x7vvdkcnu6wZ ~]# ls
usr  xiaohu
[root@iZ0jlcqjnf4x7vvdkcnu6wZ ~]# mkdir test
[root@iZ0jlcqjnf4x7vvdkcnu6wZ ~]# ll
total 12
drwxr-xr-x 2 root root 4096 Jul 20 10:00 test
drwxr-xr-x 3 root root 4096 Apr 25 12:43 usr
drwxr-xr-x 3 root root 4096 May 27 16:04 xiaohu
[root@iZ0jlcqjnf4x7vvdkcnu6wZ ~]# ll
total 12
drwxr-xr-x 2 root root 4096 Jul 20 10:00 test
drwxr-xr-x 3 root root 4096 Apr 25 12:43 usr
drwxr-xr-x 3 root root 4096 May 27 16:04 xiaohu
[root@iZ0jlcqjnf4x7vvdkcnu6wZ ~]# cd test/
[root@iZ0jlcqjnf4x7vvdkcnu6wZ test]# ll
total 0
[root@iZ0jlcqjnf4x7vvdkcnu6wZ test]# touch test1.txt
[root@iZ0jlcqjnf4x7vvdkcnu6wZ test]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 20 10:00 test1.txt
[root@iZ0jlcqjnf4x7vvdkcnu6wZ test]# 

In the above command, I used the mkdir command to create the test folder in the root directory , and then created the test1.txt text file in the test folder .


2.2 cat (read file content)

Syntax: cat [select] [file]

Common options:

-n number all lines of output

cat test1.txt

 The cat command is suitable for reading simple and short files. cat is the abbreviation of concatenate, not the meaning of cat~

For example, because the data has not been written yet, I will demonstrate it in the write command~ 


2.3 echo (write file)

The echo command is suitable for operating on simple files, but it is inconvenient if the files are too complicated.

Syntax: echo "content" > [filename]

echo + > means redirection, write the content to a certain file, if the ">" is forgotten, then he will print the content to be written on the console.

In addition, using echo + > redirection for the same file multiple times will overwrite the original content and cannot be appended.

Summary: Commands such as echo and cat are only suitable for simple file operations. If the file is too complicated, it is not appropriate. The echo and cat commands are more suitable for operations such as batch processing of automated scripts.


2.3 vim (text editor)

vim is suitable for editing and processing complex files, which can be understood as a Windows notepad~

Here are the 3 most basic usages:

  1. open a file
  2. edit file
  3. save and exit

1. Create file/open file

vim [filename]

The following is the input: the page display of vim test1.txt 

You can see that it also shows the "hello" we inserted earlier using echo. It is now in normal mode and cannot be edited~ 

2. Enter insert mode 

By default, vim is in normal mode (normal mode). The keys of the keyboard in normal mode represent shortcut keys for some special functions. If you want to edit the file correctly, use the i key (inset) to enter the insert mode, and then you can edit the text like Notepad Already~~

3. Save

The file cannot be saved in the insert mode, you need to return to the normal mode first, press ESC to return to the normal mode, enter :w in the normal mode , and then press Enter to save the file.

4. Exit You
cannot exit in insert mode, you need to return to normal mode first.
Enter :q in normal mode and press Enter to exit.
You can also directly use :wq to perform a save and exit at the same time.

 


Three, cp (copy) and mv (cut)

3.1 copy

cp- copy means to copy

Syntax: cp [options] source file or directory copy to: target file or directory

Function: Copy files or directories

Explanation:   The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear 


Common options:

  • -f or --force Forcibly copy files or directories, regardless of whether the destination file or directory already exists
  • -i or --interactive ask user before overwriting files
  • -r Recursive processing, processing files and subdirectories under the specified directory together. If the form of the source file or directory does not belong to a directory or a symbolic link, it will be treated as an ordinary file
  • -R or --recursive recursive processing, the files and subdirectories in the specified directory will be processed together

For example, copy the test1.txt file to the current directory and name it test2.txt

Copy operation is sometimes dangerous. If the copied target file already exists, it will be "overwritten"! ! !

At this time, we can add the option -i, cp -i source file target file, if there is coverage, the user will be asked.

When the cp command copies a directory, the -r option must be added, which means recursive copying.


3.2 mv

Syntax: mv [options] source file or directory move to destination file or directory

Function:

1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command will rename the file or move it to
a new directory.
2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source
directory it will rename the given source file or directory to The given target filename.
3. When the second parameter is the name of an existing directory, there can be more than one source file or directory parameter, and the mv command will
move the source files specified by each parameter to the target directory.

 Commonly used options:
-f: force means to force, if the target file already exists, it will not be asked but overwritten directly
-i: if the target file (destination) already exists, it will ask whether to overwrite! 

  • When mv is moving, you can rename the target file, and rename it while moving. If the source file or directory to be moved is in the same directory as the target file or directory, it is simply a name change at this time.
  • There is no separate command for renaming in Linux, it is done with the ,mv command.
  • There is no need to add the -r option to mv to move directories.
  • The execution of the cp command is relatively slow and requires large-scale reading and writing of the hard disk. The execution of the mv command is very efficient and can be completed almost instantly, because: the "path" of each file in the file system in the operating system is For a simple attribute, mv simply modifies the attribute of this path, and the time complexity is O(1).
  • Linux does not have the concept of a recycle bin, so you can use the mv command to simulate the effect of the recycle bin. If you want to delete something, you don’t use the rm command directly, but use the mv command to move the item to be deleted to a separate directory as a recycle bin. .


4. Network settings

4.1 grep (string matching)

Syntax: grep [parameter]... [file]...
Function: It is used to find whether the specified string is contained in the file, and display the corresponding line.

options:

  • -n<number of lines> number of lines to display
  • -w Match whole word. Requires the exact same result for the whole word, not just part of a word.
  • -r Search recursively. You can search all files in multi-level directories.
  • --color highlight found results
  • --include Specifies to look for certain files
  • --exclude specifies to exclude certain files

Example query: "hello" in the test1.txt file in the test directory 

grep string matching can quickly find out whether a specific string is contained in a file.

grep can also search in many files at once:  grep -n "hello" * -R 

* means all files in the current directory, - R means recursive search, if there is a directory in the current directory, it will automatically enter the directory to search.

The grep command can not only search the content in the file, but also can be used in conjunction with other commands.


4.2 ps (view process)

Syntax: ps [parameter]...
Function: Used to view the processes running on the current system

options:

  • a displays all processes in a terminal
  • u User-oriented format to display program status
  • x displays all programs, not distinguished by terminal

The content viewed directly using the ps command is very limited, so the ps command is often paired with some fixed options.

Use ps aux to list all processes on the system:

But more often we don't care about all the processes, but only care about the processes we want. In this case, we need to cooperate with the grep command to quickly filter. ps aux | grep [process to query]

Pipeline is a very classic way in Linux~


4.3 netstat (query network status)

Syntax: netstat [parameters]...
Function: View the network status on the system.

options:

  • -a show all sockets that are or are not listening
  • -n display numeric addresses instead of resolving hosts, ports or usernames
  • -p displays the PID and name of the process the socket belongs to

Example:

  • netstat -anp
  • netstat -anp | grep "process name"
  • netstat -anp | grep "port number"

But more is that we only need to pay attention to part of the information, which needs to be queried with the grep command~~

When each networked application starts, it will bind a port number as the identification of the local application~

For example:

Query the port number of the mysql service:

Query whether port 3306 is occupied by other processes~~


4.4 sudo (perform tasks with special permissions, release ports)

We may encounter some special ports being occupied during project deployment, and release the occupied ports:

1. Find the process ID that occupies port 8080:

sudo lsof -i :8080

2. End the process that occupies the port. You can use  killthe command to end the process. For example, if the output from the previous step shows a process ID of 1234, you can end the process with the following command:

sudo kill 1234

3. Confirm that the port is released, and you can use lsofthe command again to verify whether the port has been released.

sudo lsof -i :8080


Alright, here we go, 【Learn Linux from scratch】Common commands and operation  bloggers have finished sharing, I hope it will be helpful to everyone, welcome to criticize and correct if there is anything wrong. 

Thank you to everyone who read this article, and more exciting events are coming: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★* 

When I met you, all the stars fell on my head ...

Guess you like

Origin blog.csdn.net/weixin_67603503/article/details/131686789