Linux CentOS 7 commonly used command learning: formal

1. Overview of shell and Linux commands

(1) Shell

1. A special program running in the Linux system
2. Acting as a "translator" between the user and the kernel
3. When the user logs in to the Linux system, a Shell program
is automatically loaded 4. Bash is the shell program used by default in the Linux system# # Shell overview
(bash file is located in /bin/bash)
Insert picture description here
process : accept the command input by the user- explain the command (shell) -pass to the system kernel for execution

(2) Overview of Linux commands

1. Linux command classification (internal commands and external commands)

Internal command External instructions
Some special instructions integrated in the Shell interpreter program, also known as built-in instructions Script file or binary program that can complete specific functions in Linux system
Part of Shell Commands outside of the Shell interpreter program
There is no separate corresponding system file Each external command corresponds to a file in the system
Automatically load into memory and can be used directly The corresponding file location must be known, and /bin, /usr/bin, /usr/local/bin, etc. can be executed after being loaded by Shell

Ways to view commands : help, enable, type commands
For example: [root@Local host etc]#type command You
can directly check whether the command is external or internal, internally embedded, and the location of external /bin/

2. Command format requirements

命令字 [选项] [参数]  # 对应的功能:操作 功能 要执行的对象

Note: English characters are case sensitive
1. Command word:

  • Is the most critical part of the entire command
  • Uniquely determine a command
    2. Option
  • Short format option: use "-" symbol to guide (short format: -letter)
  • Long format option: use "–" symbol to guide (long format: --complete word)
    3. Parameters
  • The processing object of the command word
  • It can be a file name, directory (path) name, or user name, etc.
  • The number can be zero or more

3. Auxiliary operation of Linux command line

  • Tab key: Automatically complete / double-click to help query directories or files with the same prefix. Vim text editing, cd to switch directories
  • Backslash "\": Forced line break (actually still on the same line) can no longer be tab completion. Often working on projects is to use additional functions to distinguish
  • Ctrl+U key combination: Clear to the beginning of the line
  • Ctrl+K key combination: empty to the end of the line
  • Ctrl+L key combination: clear 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

1. Internal command help; view the help information of Shell internal commands. The "–help" option of the command is suitable for most Linux external commands
. 2. Use the man command to read the manual page (man command)

  • Use the ""↑", "↓" (ENTER) arrow keys to scroll a line of text
  • Use Page Up and Page Down (Space) keys to turn pages
  • Press Q or q to exit the reading environment
  • Press "/" key to search for content, n key to search downwards, N key to search upwards

Two, command learning

(1) Current directory

1. View the current working directory-pwd (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.
E.g

 [root@localhost ~]# pwd
 /root

Change the command prompt to the detailed directory
vi /etc/profile file, and add at the end: export PS1='[u@h W]$ '

(2) Switch the user's working directory (~ represents the current user's home directory)

1.cd target location: switch to the target location
2.cd ~ or cd: if the target location is not specified, switch to the home directory of the current user
3.cd -: switch to the directory where the cd command was executed last time
4. Absolute path : The path starting from the root directory is called absolute path cd /
5. Relative path: the path relative path expression form starting from the current directory

Manifestations Description Example
Use the directory name or file name directly Indicates the location of subdirectories and files in the current working directory cd groub2/
Start with a period "." Indicates that the current working directory is used as the starting point cd ./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

Commonly used options of ls
1.-l: Display the 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.
Use the ls -l command to view a directory, and you will get a list of 7 fields rwxrwxr-- 1 root root 76207-29 18:19 exit
2.-a: Display all (All) subdirectories And file information, including hidden directories and hidden files whose names begin with a dot ".".

  • -A: The function is basically similar to the -a option, but there are two special hidden directories that will not be displayed, namely "." representing the current directory and "..." representing 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 humane <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: Different files are distinguished by color in character mode. It is turned on by default.
    Under normal circumstances, dark blue represents directories, white represents general files, green represents executable files, yellow represents device files, red represents compressed files, and sky blue represents soft connections

  • -i: Display the inode number of files and directories.
    Supplement : Use the ls -l command to view a directory and you will get a list of 7 fields rwxrwxr-- 1 root root 76207-29 18:19 exit

  • Field 1: File attribute word The
    first character represents the type of file.
    "-" indicates that the file is an ordinary file.
    "d" indicates that the file is a directory.
    "l" indicates that the file is a soft link file. Similar to shortcuts under windows, 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 execute (Run files, switch directories)
    The first three represent the permissions of the file owner (owner), the middle three represent the permissions of the group to which the file belongs, and 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 you create a new empty directory, the second field of this directory is 2, indicating that there are two subdirectories in the directory.

  • Field 3: File\Directory Owner

  • Field 4: The group of the file\directory owner

  • The fifth field: the space occupied by the file (soft recognition is in bytes). If it is a directory, it means the size of the directory, not the total size of the directory and the files below it. Everything in a Linux system is a file, so a directory is also a kind of file.

  • Field 6: File\Directory last accessed or modified time

  • Field 7: File name. If it is a symbolic link (soft link file), there will be an arrow symbol "->", followed by a
    wildcard in the file name it points to.
    Combined with the wildcard
    "?": Represents any character, there is And a character
    "*": represents any number of characters, can be 0 or 1 or more
    "[]": it can match any character in the character group [abc], [af]

(4) Set alias-alias

1. Simplify the commonly used and relatively long commands through the alias mechanism
alias command alias='command' (=no spaces on both sides of the equal sign, use single quotes
for the command) For example: set myls to the alias of ls -alh
alias myls=' ls -alh'
2. Cancel alias
unalias command alias

(5) Statistic directory and file space occupation-du

1. Count the size of the disk space occupied by the specified directory (or file)

 du [选项] [文件或目录…]

2. Common options

  • -a: Including all files when counting disk space usage, not just counting directories
  • -h: Display the statistical results in a more humane way (the default count is KB, but the unit is not displayed)
  • -s: Only the total size of the occupied space is counted, not the size of each subdirectory and file
  • Key points : --max-depth=n—— Count the total size of all directories that are less than or equal to the nth level.
    In the production environment, the system disk is occupied. Finding the cause of the abnormality requires finding the directory layer by layer.
    For example: Counting the size of the space occupied by the /var/log directory
 ​du -h --max-depth=1/var/log/

Note: Options -a and -s cannot be used at the same time

(6) Create a new directory mkdir

1. Create a new empty directory
mkdir [Options] Directory location and name

  • Add multiple directories at once (mkdir abc 123)
  • -p: create nested multiple directories at once

For example: create a subdirectory named public_html in the current directory

 mkdir public_html

For example: create a directory /aa, create a subdirectory bb under the /aa directory, and create a subdirectory cc under the /aa/bb directory

 mkdir -p aa/bb/cc

(7) Create an empty file touch

1. Update the time stamp of the file
2. Often used to create multiple new empty files
touch files...
For example:
create two empty files

touch aa bb

The file names are aa.txt and bb.txt:

touch {
    
    aa,bb}.txt

Create two different suffixes with the same file name

touch aa {
    
    jpg.png}

(8) Create a link file In

1. Create a link file -In creates a link file
for a file or directory, similar to the shortcut
link file type of the Windows system.
1. Soft link (also known as symbolic link ln -s) When
establishing a soft link, the source file needs to be represented by an absolute path . The location of the source file remains unchanged, and the location of the soft link file is useful no matter where it is located. Changing the file location by the relative path will be invalid.
Example: ln -s source file or directory...link file or target location

 ln -s /root/bb.txt   /opt/BB.txt

2. Hard link (for understanding only)
ln source file or directory...linked file or target location The
source file is represented by an absolute path, the hard link is relative to the creation of an alias file, and the source file is still useful after deletion
Insert picture description here

(9) Copy file or directory cp

Rebuild a copy of the file or directory (source) that needs to be copied, and save it as a new file or directory
1.cp [Options]...source file or directory...target file or directory...
2. cp common options

  • -f: Do not remind when overwriting the file or directory with the same name of the target, but directly force the copy
  • -i: remind the user to confirm when overwriting a file or directory with the same name as the target
  • -p: The permissions, owner, time stamp and other attributes of the file saved when copying remain unchanged
  • -r: This option must be used 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.
cp letc/host.conf /etc/host.conf.bak
cp host.conf{
    
     ,.bak}
mkdir yum_bak
cp -r /etc/ yum.repos.d /etc/ yum.conf yum_bak/

(10) Delete files or directories rm

1.rm [Options] The file or directory to be deleted...
2. Common options

  • -f: Do not remind when deleting files or directories, but directly delete them forcibly.
  • -i: remind users to confirm when deleting files or directories. (y means to delete, n means not to delete)
  • -r: This option must be used when deleting a directory, which means to delete the entire directory tree recursively (should be used with caution).
    Note: Do not directly delete the existing directories or configuration files in the system to avoid unexpected failures.
 rm -rf /etc/yum.repos.d/*

When deleting files in a directory, it is recommended to switch to the directory first and then execute the rm -rf command

 cd /etc/ yum.repos.d/
 rm -rf/*(谨慎使用)

(11) Move files or directories mv

1. Specify the file or directory transfer location
2. If the target location is the same as the source location, it is equivalent to performing a rename operation
mv [Options]...source file or directory...target file or directory

例:mv /aa /opt #移动根目录下aa目录到opt目录下`

3. The mv command can only rename a single file, and you can use the rename command to modify file names in batches

例: rename jpg gif *.jpg
   renaem  旧名称 新名称 目标文件

(12) Find the directory where the command file is stored

1. The search scope is determined by the environment variable PATH ( echo $PATH) There are five paths to
use and echo $PATHview: /user/local/sbin: /user/local/bin: /usr/sbin: /usr/bin: /root/bin (from left to Search in order from the right)
2. By default, when the first target is found, the search will not continue

which 命令/程序名 

3. Find in all search paths

which -a 命令/程序名 

例:which ls ,which history

(13) Find a file or directory find

1. Recursively, according to the target's name, type, size and other different attributes for fine search

find [查找范围]  [查找条件类型]

2. Common search condition types

Lookup type Keyword Description
Find by name -name Search based on the name of the target file (the name is enclosed in double quotation marks, "*" and "?" wildcards are allowed
Check by file size -size Search according to the size of the target file, generally use the "+" and "-" signs to set the size greater 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 the type of file. File types include ordinary files (f), directories (d), block device files (b): devices that read data in blocks (hard disk, memory, CD-ROM), and character device files (c) are single Device for reading data from characters (keyboard, mouse)

4. Use the find command to implement multiple search conditions, and
use logical operators directly in expressions

  1. "-A" means and (and) find /boot -size +1024k -a -name "vmlinuz*"
  2. "-O" means or (or) find /boot -size +1024k -o -name "vmlinuz*"

The exec usage of find
1. The Linux command is followed by the -exec parameter, which is a semicolon; as the end criterion, since the semicolon has different meanings in various systems, the backslash bar "" is added in front of the semicolon The escape character
{} represents the file name found by the previous find 2. 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 ./ -type f- exec ls -l {};
The priority of command execution
1. The first priority: the command of the specified path. Absolute path /usr/bin/ls or relative path cd /usr/bin —— cd ./ls
2. Second priority: the command specified by alias alias myls=”/usr/bin/ls -alh'
3. Third Priority: Internal command
4. Fourth priority: hash command
There will be a hash table under the linux system. When you first boot this hash table is empty. Whenever you execute a command, the hash table will record this entry The path of the command is equivalent to the level storage. The first time you execute the command, the shel1 interpreter will find the path of the command from the PATH path by default. When you use the command for the second time, the shel1 interpreter will first check the has table , There is no such command to search under the PATH path. The hash table can improve the call rate of the command.
5. The fifth priority: search through the search order defined by PATH

If none of the above sequence is found, it will report "Command not found..." error.

Guess you like

Origin blog.csdn.net/weixin_53567573/article/details/113500949