Linxu basic commands (on)-file management

1. Overview of Linux commands

1.1 How to use commands

Linux command format:

command [-options] [parameter1] …

  • command : command name, English word or word abbreviation of corresponding function;
  • [-options] : Options, which can be used to control commands, or can be omitted, [] stands for optional;
  • parameter1 …: The parameters given to the command: it can be zero one or more.
    Give a chestnut:
    Insert picture description here

1.2 View the help document

1.2.1 --help

It is usually the help information that comes with the linux command,
such as: ls --help
Insert picture description here

1.2.2 man (look for a man if there is a problem, manual)

man is a manual provided by linux, which contains most of the commands and function instructions.
The manual is divided into many sections. When you use man, you can specify different chapters to browse.
Example: man ls; man 2 The
meaning of each section in printf man is as follows:

  1. Standard commands
  2. System calls (system calls, such as open, write)
  3. Library functions (library functions, such as printf, fopen)
  4. Special devices (description of the device file, various devices under /dev)
  5. File formats (file formats, such as passwd)
  6. Games and toys
  7. Miscellaneous (miscellaneous, conventions and protocols, etc., such as Linux file system, network protocol, ASCII code;
    environ global variable)
  8. Administrative Commands (administrative commands, such as ifconfig)
    man is searched in the order of chapter numbers in the manual.
    man sets the following function keys:
Function keys Features
space bar Display the next screen of the manual page
Enter key Scroll through one line of the manual at a time
b Roll back one screen
f Scroll forward one screen
q Exit the man command
h List all function keys
/word Search word string

Insert picture description here

1.3 Auto-completion

While typing out the first few letters of the command, press the tab key, and the system will automatically complete the command for us
Insert picture description here

1.4 History commands

After the system has executed some commands, you can press the up and down keys to scroll through the previous commands, and history will list the executed commands

Two, Linux file management commands

2.1 View file information: ls

Syntax : ls [option][directory or file]
Function : For a directory, this command lists all subdirectories and files in the directory. For files, the file name and other information will be listed.
Common options :

parameter meaning
-a Display all subdirectories and files in the specified directory, including hidden files
-l Display detailed information of files in a list
-h Cooperate with -l to display the file size in a humanized way

The Linux file or directory name can have up to 265 characters. "." represents the current directory , "..." represents the upper level directory , and the files beginning with "." are hidden files and need to be displayed with the -a parameter.

ls -l

Insert picture description hereInsert picture description here

2.2 Output redirection command:>

Linux allows the command execution result to be redirected to a file, and the content that should be displayed on the terminal is saved to the specified file.
Such as:

ls> test.txt (If test.txt does not exist, create it, and overwrite its content if it exists)
Note:>Output redirection will overwrite the original content, and >>output redirection will be appended to the end of the file.
Insert picture description here

2.3 Display the current path: pwd

Syntax : pwd
function : display the user's current directory
commonly used options : none
Insert picture description here

2.4 Split screen display: more

When viewing the content, when the information is too long to be displayed on one screen, a quick scroll will appear, making the user unable to see the content of the file. In this case, you can use the more command to display only one page at a time, press the space bar You can display the next page, press the q key to exit the display, and press the h key to get help.
Insert picture description here

2.5 Split screen display: less

  • The less tool is also a tool for paging display of files or other output. It should be said that it is a tool for linux orthodox to view the contents of files, and it has extremely powerful functions.
  • The usage of less is more flexible than more. At the time of more, we have no way to turn to the front, we can only look to the back
  • But if less is used, you can use the button functions such as [pageup][pagedown] to browse back and forth through files, which is easier to view the contents of a file!
  • In addition, you can have more search functions in less, not only can search downwards, but also search upwards.

Syntax : less [Parameter] File
function : less is similar to more, but you can use less to browse files at will, and more can only move backward, but not forward, and less will not load the entire file before viewing.
Options:

parameter meaning
-i Ignore case when searching
-N Display the line number of each line
/String Function to search down "string"
?String Function to search "string" upwards
n Repeat the previous search (related to/or ?)
N Repeat the previous search in reverse (related to / or ?)
q quit

2.6 Display beginning: head

The head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. The head is used to display the beginning of the file to the standard output, and the tail is supposed to look at the end of the file. .
Syntax : head [Parameter]... [File]...
Function : head is used to display the beginning of the file to the standard output. By default, the head command prints the first 10 lines of the corresponding file.
Options :

-n<number of lines> the number of lines displayed

Insert picture description here

2.7 End of display: tail

The tail command writes the file to the standard output from the specified point. Use the -f option of the tail command to easily view the log file that is changing, tail -f filename will display the last content of the filename on the screen, and not only refresh it , Allows you to see the latest file content.
Syntax : tail [Required parameter] [Select parameter] [File]
Function : Used to display the end content of the specified file. When the file is not specified, it will be processed as input information. Commonly used to view log files.
Options:

  • -f loop reading
  • -n<number of lines> display the number of lines

Insert picture description here

2.8 Piping:|

Pipe: The output of one command can be used as the input of another command through a pipe.
We can understand pipes in real life. One end of the pipe is plugged in and the other is taken out.
The left and right sides of this "| " are divided into two ends, the left end is plugged (written), and the right end is taken. East (read).
For example: to display 5-10 lines of information in a file, we can first use head to output the first 10 lines of information to the pipeline, and then use tail to display the last 5 lines of the first 10 lines of information
Insert picture description here

2.9 Clear screen: clear

The clear function is to clear the display on the terminal (similar to the cls clear screen function of DOS), and you can also use the shortcut key: Ctrl + l ("l"
is a character).

2.10 Switch working directory: cd

When using Unix/Linux, it is often necessary to change the working directory. The cd command can help users switch working directories.
Syntax : cd directory name
function : change the working directory. Change the current working directory to the specified directory.
For example:

command meaning
cd . . . Switch to the current directory
cd . . .. .. Switch to the parent directory
cd ~ Switch to the home directory of the current user (/home/user directory)
cd - You can enter the directory where you were last time

Insert picture description here

2.11 Create a directory: mkdir

Syntax : mkdir [options] dirname...
Function : Create a directory named "dirname" in 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 yet exist, that is, multiple directories can be created at a time;

Insert picture description here

2.12 Delete directory: rmdir

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.
Syntax: rmdir [-p][dirName]
Applicable object: All users with operating authority of the current directory
Function: Delete empty directory
Common options:
-p When the subdirectory is deleted, if the parent directory also becomes an empty directory, it will be associated with it. The parent directory is deleted together.
Insert picture description here

2.13 Delete file: rm

The rm command can delete files or directories at the same time

Syntax : rm [-firv] [dirName/dir]
Applicable objects : All users
Function : Delete files or directories
Common options :

parameter meaning
-f Even if the file attribute is read-only (that is, write-protected), delete it
-i Ask for confirmation one by one before deleting
-r Delete the directory and all files under it

Insert picture description here

2.14 View or merge file content: cat

Syntax: cat [Options] [File]
Function : View the contents of the target file
Common options :
-b Number of non-empty output lines
-n Number of all output lines
-s Do not output multiple blank lines
Insert picture description here

2.15 Text search: grep

Syntax : grep [options] Search string file
function : Search string in the file, print out the found line.
Common options :

parameter meaning
-i Ignore the difference in case, so the case is considered the same
-n By the way, output the line number
-v Reverse selection, that is, the line without the content of the "search string" is displayed

Insert picture description here

2.16 Find files: find

  • The find command under Linux searches for files in the directory structure and performs the specified operations.
  • The find command under Linux provides quite a lot of search conditions and is very powerful. Because find has powerful functions, it has many options, most of which are worth our time to understand.
  • Even if the system contains a network file system (NFS), the find command is also valid in the file system, and only you have the corresponding permissions.
    When running a very resource-consuming find command, many people tend to execute it in the background, because it may take a long time to traverse a large file system (here refers to a file system above 30G bytes).

Syntax : find directory name-option
function : used to search for files in the file tree, and make corresponding processing (may access the disk)
common options :

command meaning
find ./ -name test.sh Find all files named test.sh in the current directory
find ./ -name ‘*.sh’ Find all files with the suffix .sh in the current directory
find ./ -name “[A-Z]*” 查找当前⽬录下所有以⼤写字⺟开头的⽂件
find /tmp -size 2M 查找在/tmp ⽬录下等于2M的⽂件
find /tmp -size +2M 查找在/tmp ⽬录下⼤于2M的⽂件
find /tmp -size -2M 查找在/tmp ⽬录下⼩于2M的⽂件
find ./ -size +4k -size -5M 查找当前⽬录下⼤于4k,⼩于5M的⽂件
find ./ -perm 777 查找当前⽬录下权限为 777 的⽂件或⽬录

Insert picture description here

2.17 拷贝文件:cp

语法:cp [选项] 源文件或目录 目标文件或目录
功能: 复制文件或目录
说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息

常用选项:

参数 含义
-f 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 覆盖文件之前先询问用户
-r 递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
-v 显示拷贝进度

Insert picture description here

2.18 移动文件:mv

语法: mv [选项] 源文件或目录 目标文件或目录
功能:

  1. 视mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或将其移至一个新的目录中。
  2. 当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。
  3. 当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。

常用选项:

选项 含义
-f force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i If the destination file already exists, it will ask whether to overwrite it!

Insert picture description here

2.19 Archive management: tar

Syntax: tar [parameter] Package file name File
function : The data in the computer often needs to be backed up. Tar is the most commonly used backup tool in Unix/Linux. This command can archive a series of files to a large file In, you can also unzip the file to restore the data. The
tar command is very special. You can use "-" or not in front of the parameter.
Common options:

Options parameter
-c Parameter command to create a compressed file (create meaning);
-x Unpack the parameter command of a compressed file!
-t Check the files in the tarfile!
-with Does it also have the attributes of gzip? That is, do you need to use gzip compression?
-j Does it also have the attributes of bzip2? That is, do you need to use bzip2 compression?
-v Show files during compression! This is commonly used, but it is not recommended to be used in the background execution process!
-f Use the file name, please pay attention, you must immediately accept the file name after f! **Don't add parameters!
-C Unzip to the specified directory

Note: option f must be put at the end
Insert picture description here
Insert picture description here

2.20 File compression and decompression: zip, unzip

Syntax: zip Compress files. zip Directory or file
function: Compress directories or files into zip format
Common options:

-r Recursive processing, processing all files and subdirectories in the specified directory together
Insert picture description here
Insert picture description here

2.21 View command location: which

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/113070458