Linux-Common Commands Explained (Shell, the classification, format and usage of Linux commands, Linux common commands)


Preface

  • Linux commands are commands for managing Linux systems
  • For the Linux system, whether it is a central processing unit, memory, disk drive, keyboard, mouse, or user, it is a file (everything is a file), and the commands for Linux system management are the core of its normal operation.
  • There are two types of linux commands in the system: built-in Shell commands and Linux commands

One, Linux command basis

  • In the Linux system, any string entered in the character operation interface that can complete a specific operation and task can be called a "command"
  • Strictly speaking, "command" usually only represents the name of an instruction or program that realizes a certain function

1.Shell overview

  • The execution of Linux commands must rely on the Shell command interpreter

mark

  • User: The interface where we enter commands
  • The shell is actually a special program that runs in the Linux system. It acts as a "translator" between the user and the kernel. It is responsible for receiving and interpreting commands entered by the user, and translating the operations that need to be performed (translated into binary computer language ) Passed to the system kernel for execution
  • When the user logs in to the Linux system, the Shell program will be automatically loaded to provide the user with an operating system that can enter operating commands
  • Bash is the shell program used by default in the Linux system, and the file is located in /bin/bash
  • Kernel: It is the most critical component of the Linux operating system. It executes the translated commands and is used to implement core operations such as CPU and memory allocation, process scheduling, and device drivers. It is mainly hardware-oriented

2. Classification of Linux commands

2.1 The difference between internal commands and external commands

  • According to the relationship between Linux commands and Shell programs, generally divided into the following two types
Internal command External instructions
Some special instructions integrated in the Shell interpreter program (such as Bash) Script files or binary files that can complete specific functions in the 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 memory, no need to re-read files from the hard disk, users can use it directly The Linux system must know the file location corresponding to the external command before it can be loaded and executed by Shell
  • Linux defaults to adding directories for storing external commands and programs (such as /bin, /usr/bin, /usr/local/bin, etc.) to the user's "search path". When using external commands located in these directories, the user No need to specify a specific location, easy to use and increase efficiency
  • In most cases, there is no need to deliberately distinguish between internal commands and external commands. The method of use is similar.

2.2 The format of the Linux command line

  • When using internal or external commands, refer to a general command line format to use the format, which can facilitate understanding of the role and working mode of the Linux command line
  • General command line format:
    command word [option] [parameter]
  • Linux commands (including file names, etc.) are case sensitive, that is to say, if the command you enter is not the right case, the system will not respond as you expect, so English should be case-sensitive
  • In the actual use of the Linux command line, the terms "option" and "parameter" are often confused, and even the order is allowed to be reversed, but generally does not affect the execution effect of the command, so in many cases, strict distinction is not made.

2.2.1 Command word

  • The command word is the command name, which is the most critical part of the entire command
  • The only way to determine a command is to ensure that the input is correct, such as capitalization, etc.
    The operation is performed, such as new creation, deletion, renaming, etc.

2.2.2 Options

  • The role of the option is to adjust the specific function of the command and determine how the command is executed
  • Short format options: generally use the "-" symbol to guide the options, such as "-"
    multiple single-character options can be combined together, such as "-al" is equivalent to "-a -l"
  • Long format options: When using multiple-character options, generally use the "–" symbol to guide the options, such as "–help"
  • The long format options have clear meanings and are easy to remember. The short format options have a simple structure and quick input, and the two formats can be mixed
  • If the option is not used, the default function of the command word will be executed

2.2.3 Parameters

  • Command word processing object
  • It can be a file name, directory (path) name, or user name, etc.
  • According to the different command words used, the number of command parameters can be zero or more

3. Edit the Linux command line

When editing Linux commands, you can use the following auxiliary operations to improve input efficiency

3.1 Auxiliary operations for editing the Linux command line

button effect
Tab 键 Automatic completion (command word, file or directory), press twice, the system will output the available list
Backslash "\" If the input line of command is too long, the terminal will automatically wrap the line. Sometimes in order to display beautifully and facilitate viewing, you can use "\ forced line break (Note: Tab may not be possible), and continue typing after the ">" prompt that appears in the next line (+ Space+\) as a continuation of the previous line
Ctrl+U Empty to the beginning of the line
Ctrl+K Empty to the end of the line
Ctrl+L Clear screen
Ctrl+C Cancel this command edit
Ctrl+a Switch to the beginning of the line
Ctrl+e Switch to end of line

3.2 Command line prompt characters

例:[root@localhost~]#

  • root: the user currently logged in to the system
  • @: Separator
  • localhost: host name
  • ~: The current directory
  • #: means login as an administrator ($: means login as an ordinary user)

Two, Linux command help

1 Overview

  • help provides help for internal commands
  • man or info provide help for external commands
  • If your English is good enough, you can be proficient in linux without relying on anyone, as long as you can use man
  • Man is actually the help for viewing instruction usage. The most important thing to learn any UNIX operating system is to learn to use the auxiliary command man
  • man is the abbreviation of manual (manual), its description is very detailed, but because it is all in English, it looks very headache
  • It is recommended that you see man again when you need it. Normally, just remember some basic usage.

2. Use the help command

  • The help command is a built-in command in Linux Shell, its purpose is to view the help information of the command inside the Shell
  • When using the help command, you only need to add the name of the internal command as a parameter
  • For example: directly enter the "help pwd" command to view the help information of the shell internal command pwd (the pwd command is used to display the working directory of the current user)
[root@localhost ~]# help pwd
pwd: pwd [-LP]
    打印当前工作目录的名字。
    
    选项:
      -L	打印 $PWD 变量的值,如果它命名了当前的
    	工作目录
      -P	打印当前的物理路径,不带有任何的符号链接
    
    默认情况下,`pwd' 的行为和带 `-L' 选项一致
    
    退出状态:
    除非使用了无效选项或者当前目录不可读,否则
    返回状态为0。

2. Use the "- -help" option

  • For most Linux external commands, you can use a general command option "- -help" to display the format and options of the corresponding command word and other help information
  • If the command word does not have the "- -help" option, generally only a simple command format will be prompted
  • For example: enter the "ls --help" command to view the help information of the ls command (the ls command is used to display file or directory listing information)
[root@localhost ~]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all			不隐藏任何以. 开始的项目
  -A, --almost-all		列出除. 及.. 以外的任何项目
      --author			与-l 同时使用时列出每个文件的作者

......//省略部分内容(太多了,可以自己再打一遍慢慢看)

3. Use the man page

  • The manual page (Manual Page) is the most commonly used form of online help in Linux systems. Most of the external software provides detailed help manual pages for executing programs and configuration files during installation.
  • The information in these manual pages is organized in a characteristic format and read through a unified manual page browser program man
  • For example: enter "man command" to view the manual page information of the entered command (a lot, it's really special to see in detail, it will not be listed here)
  • When reading the man page, it will be displayed in full-screen text mode, and an interactive operating environment is provided
button Features
"↑" or "↓" Scroll up or down a line of text
"Page up" and "Page Dowd (or space bar)" You can page up or down to display
"Q" or "q" Exit the reading environment of man pages
"/"键 Search content, such as "/-v" to find the help information of the "-v" option. If you find too much content, you can also press the "n" key or the "N" key to select downward and upward points respectively

3. Basic operations of directories and files

1. Display the current working directory-pwd

  • Show the user's current working directory location
##示例:
[root@localhost ~]# pwd
/root        ##当前工作目录为管理员目录
[root@localhost ~]# cd /
[root@localhost /]# pwd
/        ##当前工作目录为根目录

2. Switch working directory -cd

  • Change the user's working directory to another location
format Description
cd target location Switch to target location
cd ~ (home directory) If you do not specify the target location, switch to the current user's home directory (home directory)
cd Same as above
cd - Switch to the directory where you were before the last cd command was executed
  • Usually use the target location (folder path) to be switched to as a parameter
  • If the target parameter is not specified, it will switch to the current user's home directory (home directory) by default
  • The home directory is the default working directory after Linux users log in to the system
  • When expressing the location of a certain directory (or file) in the Linux operating system, two different forms can be used according to the starting directory it refers to, called absolute path and relative path

2.1 Absolute path

  • Specify the exact location of the file in the entire file system, there will be no ambiguity, starting from the root directory "/" of the file system
  • Because there is only one root directory in the Linux operating system, no matter which directory it comes from, using an absolute path can very accurately indicate the location of a directory (or file).
  • But if the path is long, the input will be more cumbersome

2.2 Relative path

  • Given by the context of the call (current location), the "/" symbol is not used at the beginning,
    so it is easier and more convenient to use
  • Relative paths mainly include the following forms:
Manifestations Description
Use the directory name or file name directly Indicates the location of subdirectories and files in the current working directory
Start with a period "." Indicates that the current working directory is used as the starting point
Start with two periods "..." Indicates to take the upper level directory (parent directory) of the current directory as the starting point
Start with "~Username" Indicates that the home directory of the specified user is used as the starting point

2.3 Summary

  • Relatively speaking, the use of a relative path to indicate a directory (file) path is flexible and changeable, usually used to identify the location of a directory (file) "near" to the current directory
  • The absolute path is often used to indicate a relatively stable (not often changed) directory in the Linux operating system.
  • When choosing a relative path or an absolute path, you should choose according to the actual situation

3. List the contents of the directory -ls

  • Display the contents of the directory, including the relevant attribute information of subdirectories and files, etc.
    ls [Options] [Files or directories...]

3.1 Common options

Options effect
-l Display detailed information of files (directories)
-a 查看隐藏文件(目录)
-A 查看隐藏文件,除了表示当前目录“.”和父目录“. .”
-d 显示目录本身的属性,而不是显示目录中的内容
-h 更人性化的显示文件(目录)大小的单位信息
-R 递归显示(如果有子目录,则会把子目录中的文件依次展现)
–color 以颜色区分文件类型

黑色:数据文件
白色:一般文件
蓝色:目录/文件
红色:压缩包
绿色;可执行的文件/命令文件/脚本
淡蓝色:链接文件/快捷方式
黄色:设备文件/磁盘文件

执行不带任何选项、参数的ls命令,可以显示当前目录中包含的子目录、文件列表信息(不包含隐藏目录、文件)
ls

执行“ls -ld”命令可以只显示当前目录的详细属性,而不显示目录下的内容

执行“ls -lh”可以以更易读(带单元格式)的长格式来查看文件信息

3.2结合通配符

  • 使用ls命令时,还可以结合通配符“?”或“*”等以提高命令编写效率
  • "?"∶ 代表任意一个字符,且至少1个
  • "*"∶代表任意数个字符,可以是0个或者1个或者多个
  • "[]"∶表示可以匹配字符组中的任意一个字符
    [abc]、[a-f]
例:以下操作将以长格式列出/etc/目录下以“ns”开头、“.conf”结尾的文件信息

[root@localhost ~]# ls -lh /etc/ns*.conf
-rw-r--r--. 1 root root 1.8K 11月 25 18:47 /etc/nsswitch.conf

4.设置别名-alias

  • 通过别名机制来简化常用的、比较长的命令,以提高使用效率
    alias 命令别名=‘命令’(“=”两边不能有空格)
  • 示例:
    • 设置myls为ls -alh的别名
      alias myls=‘ls -alh’
    • 可以定义一个名为myls的命令别名,以后再执行“myls”时即等同于执行“ls-alh”命令
    • 这个相当于以前玩魔兽的改建,懂的都懂
  • 取消别名
    • unalias 命令别名

5.统计目录及文件空间占用情况-du

  • 统计指定目录(或文件)所占用磁盘空间的大小
    du [选项] [文件或目录….]

5.1 常用选项

选项 说明
-a 统计磁盘空间占用时包括所有的文件,而不仅仅只统计目录
-h 以更人性化的方式(默认以KB计数,但不显示单位)显示出统计结果
-s 只统计所占用空间总的大小,而不是统计每个子目录、文件的大小(选项-a和-s不可同时使用)
–max-depth=n 统计所有小于等于第n层的目录的总大小

5.2 示例:

如果需要统计出这个文件夹内所有文件总共占用的空间大小,可以结合“-sh”选项,将要统计的目录“/var/log”为参数,
[root@localhost ~]# du -sh /var/log        
7.6M	/var/log/

如果需要分别统计多个文件所占用的空间大小,可以结合“-ah”选项
使用目录作为参数时,最后将列出该目录总共占用的空间大小

6.创建新目录-mkdir

  • 创建新的空目录
    mkdir [选项] 目录位置及名称…
  • 如果需要一次性创建嵌套的多层目录,必须结合“-p”选项,否则只能在已经存在的目录中创建一层子目录
  • 示例
##在当前目录下创建名为 public_html 的子目录
[root@localhost ~]# ls
anaconda-ks.cfg       公共  视频  文档  音乐
initial-setup-ks.cfg  模板  图片  下载  桌面
[root@localhost ~]# mkdir public_html
[root@localhost ~]# ls
anaconda-ks.cfg       公共  图片  音乐
initial-setup-ks.cfg  模板  文档  桌面
public_html           视频  下载


##创建目录/aa,在/aa目录下创建子目录bb,在/aa/bb目录下创建子目录cc
[root@localhost ~]# mkdir -p /aa/bb/cc
[root@localhost ~]# cd /aa/bb/cc/
[root@localhost cc]# pwd
/aa/bb/cc

7.创建空文件-touch

  • touch命令本来用于更新文件的时间标记,但在实际使用中经常用于创建新的测试文件
  • 使用文件名作为参数,可以同时创建多个文件
  • 当目标文件已存在时,将更新该文件的时间标记,否则将创建指定名称的空文件
  • 格式
    touch 文件…
  • 示例
##创建两个空文件,文件名分别为aa.txt和bb.txt
[root@localhost /]# touch aa.txt bb.txt
[root@localhost /]# ls
aa.txt  boot  home   media  proc  sbin  tmp
bb.txt  dev   lib    mnt    root  srv   usr
bin     etc   lib64  opt    run   sys   var

##或
##touch {a,b}.txt

8.创建链接文件-ln

  • 为文件或目录建立连接文件,类似于windows系统的快捷方式
  • 链接文件类型
    • 软链接(又称为符号链接)
    • 硬链接
      软链接相当于快件方式
      硬链接相当于给文件起了个别名
  • 格式:
    ln [-s] 源文件或目录… 链接文件或目录位置
  • 示例:
    mark
    (链接文件颜色为淡蓝色)

拓展

  • 在Linux中,文件名和文件的数据是分开存储的,所以可能你只是删除了它,但是它还任然存在着
  • 当一个文件拥有多个硬链接时,对文件内容修改,会影响到所有文件名
    但是删除一个文件名,不会影响别的文件名的访问,只会使得"硬链接数"减 1
  • 需要注意的是不能对目录做硬链接,且在日常工作中几乎不会建立文件的硬链接,了解即可
文件类型 软链接文件 硬链接文件
删除源文件后 失效 仍然可用(文件占用空间与源文件相同)
使用范围 适用于文件目录 仅适用于文件
保持位置 与源文件可在不同的文件系统中 必须与源文件在同一个文件系统(即同一个分区)中
与源文件的关系 相当于快捷方式 相当于给文件起了一个别名
inode号码 inode号不同 inode号相同

9.复制文件或目录-cp

  • cd命令用于复制文件或目录,将需要复制的文件或目录(源)重建一份并保存为新的文件或目录(可保存在其他目录中)
  • 格式:
    cp [选项] …源文件或目录 …目标文件或目录…
  • 常用选项
选项 作用
-f 覆盖目标同名文件或目录时不进行提醒,而直接强制复制
-i 覆盖目录同名文件或目录时提醒用户确认
-p 复制时保持源文件的权限、属主及时间标记等树形不变
-r 复制目录时必须使用此选项,表示递归复制所有文件及子目录
  • 注∶复制多个文件或目录时,目标位置必须是目录,且目标目录必须已存在

10.删除文件或目录-rm(慎用!)

  • 删除指定的文件或目录
  • 格式
    rm [选项] 要删除的文件或目录…
  • 常用选项
选项 作用
-f 删除文件或目录时不进行提醒,而直接强制删除
-i 删除文件或目录时提醒用户确认(y表示删除,n表示不删除)
-r 删除目录时必须使用此选项,表示递归删除整个目录树(应谨慎使用)
  • 注∶ 不要直接删除系统中已有的目录或配置文件,以避免出现意外故障
  • rm -rf:删除目录下文件最好先切换到目录下再执行该命令
  • 使用rm相关命令一定要谨慎!

11.移动文件或目录-mv

  • mv命令用于将指定的文件或目录转移位置,如果目标位置与源位置相同,则效果相当于为文件或目录改名
  • 格式
    mv [选项] … 源文件或目录 … 目标文件或目录
  • 需要移动多个文件或目录时,目标位置必须是目录,而且目标目录必须已存在
  • 如果在同一个目录下移动文件或者目录,则相当于执行重命名操作
[root@localhost ~]# ls
anaconda-ks.cfg       公共  图片  音乐
initial-setup-ks.cfg  模板  文档  桌面
yum_bak               视频  下载
[root@localhost ~]# mv yum_bak/ yum-bak
[root@localhost ~]# ls
anaconda-ks.cfg       公共  图片  音乐
initial-setup-ks.cfg  模板  文档  桌面
yum-bak               视频  下载
  • mv命令只能对单个文件重命名,可使用rename命令批量修改文件名
    • 格式:rename 旧格式 新格式 .旧格式
      其中“
      .txt”表示以“.txt”结尾的所有文件
    • 例∶
      mark

12.查找命令文件存放目录-which

  • which命令用于查找Linux命令程序并显示所在的具体位置,其搜索范围主要由用户的环境变量PATH决定(可执行“echo $PATH"命令查看),这个范围也是Linux系统在执行命令或程序时的默认搜索路径
[root@localhost /]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  • which命令使用要查找的命令或者程序名作为参数,默认当找到第一个目标后即不再继续查找,若希望在所有搜索路径中查找,则可以添加“-a”选项
  • 使用which查找内部命令时,将找不到对应的程序

13.查找文件或目录-find

  • find命令时Linux系统中功能非常强大的查找命令,可根据目标的名称、类型、大小等不同属性进行精细查找
  • find命令在查找时采用递归的方式,其使用形式相当灵活,也可以相当复杂
  • 格式:
    find [查找范围] [查找条件表达式]
    • 查找范围对应在其中查找文件或者子目录的目录位置(可以有多个)
    • 查找条件决定了find命令根据哪些属性、特征来进行查找

13.1 常用的几种查找条件类型

查找类型 关键字 说明
按名称查找 -name 根据目标文件的名称进行查找,允许使用“*”及“?”通配符
按文件大小查找 -size 根据目标文件的大小进行查找,一般使用“+”、“-”号设置超过或小于指定的大小作为查找条件
按文件属主查找 -user 根据文件是否属于目标用户进行查找
按文件类型查找 -type 根据文件的类型进行查找:文件类型包括普通文件(f)、目录(d)、块设备文件(b;成块读取数据的设备,如硬盘、内存和光驱)、字符设备文件(c;按单个字符读取数据的设备,如键盘和鼠标)等
  • 设置find命令的查找条件时,若需要使用“*”“?”通配符,最好将文件名用双引号括起来,避免当前目录下符合条件的文件干扰查找结果
    例如,以下操作将在/etc目录中递归查找名称以“resol”开头、以“.conf”结尾的文件
[root@localhost ~]# find /etc/ -name "resol*.conf"
/etc/resolv.conf
  • Use the "-type" search criteria to filter out files of a specified type.
    For example, find all folders (corresponding to type d) in the following directory, and ignore other types of files
[root@localhost ~]# find /boot/ -type d
/boot/
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/grub2
/boot/grub2/i386-pc
/boot/grub2/locale
/boot/grub2/fonts
/boot/grub

  • The system administrator can also search based on the file owner (-user), that is, search based on whether the file belongs to the target user.
    For example, execute the following command to recursively search for files in the /var/ directory whose owner is the apache user
[root@localhost ~]# find /var/ -user apache 
/var/lib/dav
/var/cache/httpd
/var/cache/httpd/proxy

13.2 Use the find command to achieve multiple search conditions

  • Use logical operators between expressions
    • "-a" means and (and), that is, both conditions must be met
    • "-o" means or (or), that is, only one of the conditions needs to be met
  • Example
[root@localhost ~]# find /boot/ -size +1024k -a -name "vmlinuz"
[root@localhost ~]# find /boot/ -size +1024k -o -name "vmlinuz"
/boot/grub2/fonts/unicode.pf2
/boot/System.map-3.10.0-693.el7.x86_64
/boot/vmlinuz-3.10.0-693.el7.x86_64
/boot/initramfs-0-rescue-ebfb6260d8404c749ff81bfdbdf7a3be.img
/boot/vmlinuz-0-rescue-ebfb6260d8404c749ff81bfdbdf7a3be
/boot/initrd-plymouth.img
/boot/initramfs-3.10.0-693.el7.x86_64.img
/boot/initramfs-3.10.0-693.el7.x86_64kdump.img

13.3 exec usage of find

  • Find is a Linux command we commonly use, but we generally find it is not just a look, there will be further operations, at this time the role of exec is revealed
  • The -exec parameter is followed by the Linux command, which uses a semicolon ";" as the end symbol. Since the semicolon has different meanings in various systems, the backslash escape character "\" is added before the semicolon
    {} represents the file name found by the previous find
  • Example: The find command matches all ordinary files in the current directory, and uses the "ls -l" command in the "-exec" option to list them
[root@localhost ~]# find ./ -type f -exec ls -l {} \;

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/110318359