Article Directory
- One, Linux command basics
- 2. Basic commands
-
-
- 1. pwd command ------ view the current working directory (absolute path)
- 2. Switch working directory: -cd
- 3. List the contents of the directory: ls
- 4. Use the ls -l command to view the directory and you will get a list of 7 fields
- 5.ls combined with wildcards
- 6. Statistic directory and file space occupation--du
- 7. Create a new directory: -mkdir
- 8. Create an empty file: touch
- 9. Create a link file: -ln
- 10 Copy files or directories: cp
- 11. Delete files or directories: rm
- 12. Move files or directories ----- mv
- 13. Batch modify file name: rename
- 14. Find the directory where the command file is stored: -which
- 15. Find files or directories: -find
-
One, Linux command basics
1) Linux special program----Shell
The shell is a special program running in the linux system. It
acts as a "translator" between the user and the kernel, and converts the input commands into computer language (binary). The
shell will automatically load the
shell program when the user logs in to the linux system. Bash is used by default in the system, and the bash file is located in /bin/bas
1-2, the role of Shell
The shell is independent of the kernel. It is a bridge between the kernel and the application. The kernel is the heart of the Linux system. It resides in the computer's memory from the power-on self-check until the computer is shut down. The user's application program is stored on the computer's hard disk and is only transferred to the memory when needed. Shell is an application program. When a user logs in to the linux system, the shell will be called to the memory for execution
2) Classification of linux commands
2.1 linux commands are divided into internal commands and external commands
Internal command | External instructions |
---|---|
Some special instructions integrated in the shell interpreter program, also known as built-in instructions | A script file or binary program that can complete the specified function in the linux system |
There is no separate corresponding system file | Each external command corresponds to a file in the system |
Automatically load into memory, can be used directly | The corresponding file location must be known, and /bin, /usr/bin, etc. can be executed after being loaded by Shell |
2.2 View internal commands
help
enable
type command
2.2 Disable and re-enable internal commands
Disable the internal command
enable -n command
Re-enable the internal command
enable command
3) Linux command line format
3-1. Common format
Command word [option] [parameter] (*****When entering a command, pay attention to the case of English characters)
Command word (command for operation)
~ Make the most critical part of the entire command
~ Only determine a command
option (function of the command)
- Short format options: use the "-" symbol to guide (multiple single-character options can be combined together)
- Long format option: use the "–" symbol to guide the
parameter (object to be executed)
command word processing object
can be file name, directory (path) name or user name, etc. The
number can be zero to multiple
[root@localhost ~ ]#: {Current user@host name location directory} Identity authority
root: current login system user
@: separator
localhost: host name
~: current directory location
#: administrator
$: ordinary user
3-2. Auxiliary operation of editing Linux command line
Tab key: automatic completion
Backslash "\": forced line break
Ctrl + U key combination: empty to the beginning of the line
Ctrl + K key combination: empty to the end of the line
Ctrl + L key combination: clear the screen
Ctrl + C key combination: cancel this command Edit
Ctrl+a key combination: switch to the beginning of the line
Ctrl+e key combination: switch to the end of the line
4). How to get command help
Internal command help
View the help information of the
command in the Shell Command--help
Applicable to most Linux external commands
Use man command to read manual
Use the "↑", "↓" (ENTER) arrow keys to scroll a line of text.
Use the Page Up and Page Down (space) keys to turn pages.
Press the Q or q key to exit the reading environment.
Press the "/" key to find the content, and the n key to find down, , N key to find up
2. Basic commands
1. pwd command ------ view the current working directory (absolute path)
The pwd command is used to display the location of the user's current working directory. The working directory is the default starting point for the user to manipulate files or other subdirectories. Use the pwd command without adding any options or parameters. For example, when the root user executes the pwd command in the /root directory, if the output information is "/root", it indicates that the current working directory is located in /root
Example:
1[root@localhost ~]# pwd
2/root
2. Switch working directory: -cd
■Change the user's working directory to another location
format | Description |
---|---|
cd target location | Switch to target location |
cd ~ | If you do not specify the target location, switch to the home directory of the current directory |
cd | If you do not specify the target location, switch to the home directory of the current directory |
cd - | Switch to the directory where the cd command was executed last time |
■Absolute path: Take "/" as the starting point.
■Relative path: Take the current position as the starting point of the
relative path.
Manifestations | Description | Example |
---|---|---|
Use the directory name or file name directly | Indicates the location of subdirectories and files in the current working directory | grub.conf |
Start with a period "." | Indicates that the current working directory is used as the starting point | ./grub.conf |
Start with two periods ". ." | Indicates to take the upper level directory (parent directory) of the current directory as the starting point | . ./vmlinuz |
Start with "~Username" | Indicates that the home directory of the specified user is used as the starting point | ~teacher |
Start with "~" | Indicates that the current user's home directory is used as the starting point | ~ |
3. List the contents of the directory: ls
■Display the contents of the directory, including subdirectories and file-related attribute information
ls+[选项] +[文件或目录…]
The ls command is mainly used to display the contents of a directory, including related attribute information of subdirectories and files. The parameter used can be a directory name or a file name, allowing multiple parameters to be used in the same command at the same time.
■Common options
command | effect |
---|---|
-l | Display a list of files and directories in long format (Long), including detailed information such as permissions, sizes, and last update time. The ll command has the same function as ls -l |
-a | Display the information of all (All) subdirectories and files, including hidden directories and hidden files whose names begin with a dot ".". |
-A | It is basically similar to the -a option, but there are two special hidden directories that will not be displayed, that is, "." for the current directory and "..." for the parent directory. |
-d | Display the attributes of the directory (Directory) itself, instead of displaying the contents of the directory. |
-h | Display the size of the directory or file in a more human way. The default size unit is byte (B). After using the -h option, it will be displayed in units of KB, MB, etc. This option needs to be used in conjunction with the -l option. |
-R | Display all contents in the specified directory and its subdirectories in a recursive manner. |
- -color | In the character mode, different files are distinguished by color. It is turned on by default. (Usually, dark blue indicates directories, white indicates general files, green indicates executable files, yellow indicates device files, red indicates compressed files, and light blue indicates file shortcuts.) |
-i | Display the inode numbers of files and directories. |
4. Use the ls -l command to view the directory and you will get a list of 7 fields
The first field: file attribute word The
first character represents the type of file:
"-" indicates that the file is a normal file,
"d" indicates that the file is a directory,
"l" indicates that the file is a soft link file, similar to windows The following shortcuts:
The 9 letters after the first character indicate the permission bits of the file or directory:
r means read (view, download)
w means write (add, change, delete, upload)
x means execution (run files, switch directories)
The first three represent the permissions of the file owner
The middle three represent the permissions of the group to which the file belongs
The last three represent the permissions of other users
The second field: the number of file hard links
If a file is not a directory, this field indicates the number of hard links that the file has.
If it is a directory, the second field indicates the number of subdirectories contained in the directory.
If a new empty directory is created, the second field of this directory It is 2, which means that there are two subdirectories in the directory. The
third field: the owner of the file/directory
The fourth field: the group of the file/directory
Fifth field: the space occupied by the file (in bytes by default)
If it is a directory, it means the size of the directory, not the total size of the directory and the files under it.
Everything in a Linux system is a file, so a directory is also a kind of file.
Sixth field: file/directory last accessed or modified time
Seventh field: file name
If it is a symbolic link (soft link file), then there will be a "->" arrow symbol, followed by the file name it points to
5.ls combined with wildcards
"?"
represents any character, and there is only one
"*"
representing any number of characters, and 0 or 1 or more
"[]"
can match any character in the character group
6. Statistic directory and file space occupancy-du
Count the size of the disk space occupied by the specified directory (or file)
du 【选项】 【文件或目录…】
Common options
-a: | Including all files when calculating disk space usage, not just the results |
---|---|
-h: | Display the statistical results in a more user-friendly way (the default count is KB, but the unit is not displayed) |
-s: | Only the total size of the occupied space (summary) is counted, not the size of each subdirectory and file |
- -max-depth=n | Count the total size of all directories less than or equal to the nth level |
Note: Options -a and -s cannot be used at the same time.
Example
du -a
du - -max-depth=1 统计出目录中子目录的大小
Count the size of the space occupied by the /var/log directory
du -sh /var/log/
7. Create a new directory: -mkdir
■Example 1
Create a subdirectory named public_html in the current directory
mkdir public_html
■Example 2
Create directory /aa, create subdirectory bb under /aa directory, create subdirectory under /aa/bb directory
mkdir -p /aa/bb/cc
可以ls -R递归显示创建了aa里面有bb,bb里面有cc。
8. Create an empty file: touch
Time stamps
for updated files are often used to create multiple new empty folders
Example
创建两个空文件,文件名分别为aa.txt和bb.txt
touch aa bb
touch {
a,b}.txt
Of course, it can also be used to
create a file in three ways:
screen content capture to generate a new file:
In the system:
Redirection symbol>: Write the content output on the screen to a new file, or overwrite the content of the file with the same name (high-risk command) The content output on the screen can be written to a new file with the help of> (to achieve backup), so It is often used for backup (grep, ps aux, etc. can be used)
Append symbol>>: add content at the end of the specified file
Import symbol <:Use scenario: mysql database script import and use
In the development language:
<< Bit Operation Symbol
The vi editor generates a new file:
Compared with vi, vim can automatically check the grammar, change the color to remind the grammar wrong, you can write shell and python scripts
Write nothing after the new file in vim:
q-Exit without saving, this file has not been touched, this new file will not be generated
wq-save and exit, a new file will be generated (empty)
Generate files with the underlying principles of file stream IO (character stream (text), byte stream (picture))
9. Create a link file: -ln
Create link files for files or directories, similar to shortcuts in Windows systems
Link file type
Soft link (also called symbolic link)
hard link
ln [-s](创建软链接需要-s,硬链接不需要) 源文件或目录 链接文件或目标位置
创建软链接要用绝对路径,移动和删除都会导致软链接失效
The difference between soft link and hard link
file type | Soft link file | Hard link file |
---|---|---|
After deleting the source file | Invalid still available | (The file occupies the same space as the source file) |
Scope of application | Suitable for files or directories | Only applies to files |
Save location | Can be in a different file system from the source file | Must be in the same file system (that is, the same partition) as the source file |
Relationship with source files | Equivalent to shortcut | It is equivalent to giving a file an alias |
inode number | Different inode numbers | Same inode number |
■When a file has multiple hard links, modifying the content of the file will affect all file names; but deleting a file name will not affect the access of other file names. Deleting a file name will only reduce the "number of hard links" by 1.
■It should be noted that hard links to directories are not allowed.
10 Copy files or directories: cp
Rebuild a copy of the file or directory (source) that needs to be copied, and save it as a new file or directory.
Commonly used options
■-f: When overwriting a file or directory with the same name on the target, it will not be reminded, but copy it directly
■-i: Overwrite the same name Remind the user to confirm when files or directories are
copied. ■-p: Keep the permissions, owner, and time stamp of the source file when copying.
■-r: You must use this option when copying directories, which means that all files and subdirectories are copied recursively.
Note: When copying multiple files or directories, the target location must be a directory, and the target directory must already exist
11. Delete files or directories: rm
rm [选项] 要删除的文件或目录
Options | Description |
---|---|
-f | Do not remind when deleting files or directories, but directly delete them forcibly |
-i | Remind the user to confirm when deleting a file or directory (y confirm to delete, n not to delete) |
-r | You must use this option when deleting a directory, which means the number of entire directories to be deleted recursively (use with caution) |
Note: Do not directly delete the existing directories or configuration files in the system to avoid unexpected failures; when deleting files in the directory, it is recommended to switch to the directory and execute it first.
12. Move files or directories ----- mv
Move the specified file or directory to the location.
If the target location is the same as the source location, it is equivalent to performing a rename operation
mv [选项] 源文件或目录 目标文件或目录
13. Batch modify file name: rename
The mv command can only rename a single file, and you can use the rename command to modify file names in batches
rename 旧字符 新字符 目标文
Example
rename jpg gif *.jpg ##把所有jpg格式文件改成gif格式
14. Find the directory where the command file is stored: -which
■The search range is determined by the environment variable PATH (echo $PATH)
which 命令|程序名 默认当找到第一个目标后不再继续查找
which -a 命令|程序名 在所有搜索路径中查找
As shown in the figure: history is an internal command, so the corresponding program cannot be found with which, and there is no separate file for the internal command; which can check the file storage directory of the external command, such as ls
使用which查找内部命令时,将找不到对应的程序
15. Find files or directories: -find
■Using recursive method, according to the target's name, type, size and other different attributes for fine search
find [查找范围](文件或子目录的目录位置) [查找条件表达式](条件类型)
Common search condition types
Lookup type | Keyword | Description |
---|---|---|
Find by name | -name | Search based on the name of the target file, allowing the use of "*" and "?" wildcards |
Find by file size | -size | Search according to the size of the target file. Generally use the "+" and "-" signs to set more than or less than the specified size as the search condition. Commonly used capacity units include kB (note that k is lowercase), MB, GB |
Find by file owner | -user | Search based on whether the file belongs to the target user |
Search by file type | -type | Search according to file type. File types include ordinary files (f), directories (d), block device files (b), character device files (c) and other block device files (b): devices that read data in blocks (hardware, Memory, CD-ROM) Character device file (c): A device that reads data according to a single character (keyboard, mouse) |
Use find to achieve multiple search conditions
. Use logical operator
"-a" between expressions to indicate that and ## indicates that the
"-o" needs to be satisfied at the same time, or ## indicates that only one of them needs to be satisfied
1|find /boot -size +1024k -a -name "vmlinuz"
2|find /boot -size +1024k -o -name "vmlinuz"
The exec usage of find-The exec
parameter is followed by a Linux command, which is ended with a semicolon ";". Since the semicolon has different meanings in various systems, a backslash escape is added in front of the semicolon symbol"".
{} represents the file name found by the previous find.
The find command matches all ordinary files in the current directory, and uses the ls-l command in the -exec option to list them
find /boot -name "vm*" -a -type f -exec ls -l {
} \;