This is how Linux should be learned--commonly used linux commands and bash basics

This is how Linux should be learned--commonly used linux commands and bash basics

Common commands

Common system work orders

  • echo [string | $variable]
  • date [options] [+\specified format]
  • reboot
  • poweroff
  • wget [parameter] download address
    • -b background download mode
    • -P download to the specified directory
    • -t maximum number of attempts
    • -c resume the upload from a breakpoint
    • -p Download all resources on the page, including pictures, videos, etc.
    • -r download recursively
  • ps
    • -a show all processes (including those of other users)
    • -u user and other details
    • -x show processes without a controlling terminal
    • In a Linux system, there are 5 common process states:
      • R (running): The process is running or waiting in the run queue.
      • S (interrupt): The process is dormant. When a certain condition is formed or a signal is received, it will leave this state.
      • D (non-interruptible): The process does not respond to system asynchronous signals, and cannot be interrupted even with the kill command.
      • Z (Zombie): The process has terminated, but the process descriptor still exists, until the parent process calls the wait4() system function to release the process.
      • T (stop): The process stops running when it receives a stop signal.
  • top
    • The first line of top: system time, running time, number of login terminals, and system load (the three values ​​are the average within 1 minute, 5 minutes, and 15 minutes, the smaller the value, the lower the load).
    • The second line of top: the total number of processes, the number of running processes, the number of sleeping processes, the number of stopped processes, and the number of dead processes.
    • The third line of top: the percentage of resources occupied by users, the percentage of resources occupied by the system kernel, the percentage of process resources whose priorities have been changed, the percentage of idle resources, etc.
    • The fourth line of top: total physical memory, memory usage, free memory, and memory used as kernel cache.
    • The fifth line of top: the total amount of virtual memory, the amount of virtual memory used, the amount of free virtual memory, and the amount of memory that has been loaded in advance.
  • pidof [parameters]\[service name]
  • kill [parameters] [process PID]
  • killall [parameters] [process name]

System Status Check Command

  • ifconfig [network device]\ [parameters]; get information such as network card configuration and network status
  • uname [-a]; system kernel and system version and other information
  • uptime; view system load information
  • free [-h]; memory usage information
  • who [parameter]; user terminal information currently logged in to the host
  • last [parameter]; view the login records of all systems
  • history [-c]; show the commands executed in history
  • sosreport; collect system configuration and architecture information and output diagnostic documentation

Working directory switch command

  • pwd [options]; display the current working directory of the user
  • cd [directory name]
  • ls [options] [files]

Text file editing commands

  • cat [options] [file]
  • more [options] [files]
  • head [options] [file]; view the file header
  • tail [options] [file]; view the end of the file
  • tr [original character] [target character]; replace character in text file
  • wc [parameter] text; count the number of lines, words, and bytes of the specified text
    • -l; only display the number of lines
    • -w; only display word count
    • -c; show only the number of bytes
  • stat file name; view the specific storage information and time of the file and other information
  • cut [parameter] text; used to extract text characters by "column"
  • diff [parameter] file; compares the differences of multiple text files

file directory management commands

  • touch [options] [file]; create a blank file or set the time of the file
    • -a; modify only "read time" (atime)
    • -m; modify only "modified time" (mtime)
    • -d; modify atime and mtime at the same time
  • mkdir [options] directory; used to create an empty directory
  • cp [options] source file target file; copy file or directory
    • -p preserve the attributes of the original file
    • -d If the object is a "linked file", keep the attributes of the "linked file"
    • -r recursively keep copying (for directories)
    • -i Ask whether to overwrite the target file if it exists
    • -a is equivalent to -pdr (p, d, r are the above parameters)
  • mv [options] sourcefile[destination path|destination filename]; for cutting or renaming files
  • rm [options] file; used to delete a file or directory
  • dd [parameter]; copy files or convert files according to the specified size and number of data blocks
    • if input file name
    • of 输出的文件名称
    • bs 设置每个“块”的大小
    • count 设置要复制“块”的个数
    • dd if=/dev/zero of=560_file count=1 bs=560M ;从/dev/zero设备文件中取出一个大小为560MB的数据块,然后保存成名为560_file的文件
    • dd if=/dev/cdrom of=RHEL-server-7.0-x86_64-LinuxProbe.Com.iso;用dd命令来压制出光盘镜像文件
  • file 文件名 ;查看文件的类型

打包压缩与搜索命令

  • tar [选项] [文件] ;对文件进行打包压缩或解压
    • -c 创建压缩文件
    • -x 解开压缩文件
    • -t 查看压缩包内有哪些文件
    • -z 用Gzip压缩或解压
    • -j 用bzip2压缩或解压
    • -v 显示压缩或解压的过程
    • -f 目标文件名
    • -p 保留原始的权限与属性
    • -P 使用绝对路径来压缩
    • -C 指定解压到的目录
    • tar -czvf 压缩包名称.tar.gz 要打包的目录;
    • tar -xzvf 压缩包名称.tar.gz
  • grep [选项] [文件] ;用于在文本中执行关键词搜索
    • -b 将可执行文件(binary)当作文本文件(text)来搜索
    • -c 仅显示找到的行数
    • -i 忽略大小写
    • -n 显示行号
    • -v 反向选择——仅列出没有“关键词”的行。
  • find [查找路径] 寻找条件 操作;用于按照指定条件来查找文件
    • -name 匹配名称
    • -perm 匹配权限(mode为完全匹配,-mode为包含即可)
    • -user 匹配所有者
    • -group 匹配所有组
    • -mtime -n +n 匹配修改内容的时间(-n指n天以内,+n指n天以前)
    • -atime -n +n 匹配访问文件的时间(-n指n天以内,+n指n天以前)
    • -ctime -n+n 匹配修改文件权限的时间(-n指n天以内,+n指n天以前)
    • -nouser 匹配无所有者的文件
    • -nogroup 匹配无所有组的文件
    • -newer f1 !f2 匹配比文件f1新但比f2旧的文件
    • -type b/d/c/p/l/f 匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
    • -size 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
    • -prune 忽略某个目录
    • -exec ...{}\;后面可跟用于进一步处理搜索结果的命令。用于把find命令搜索到的结果交由紧随其后的命令作进一步处理
      • find /etc -name "host*" -print

输入、输出、错误输出和重定向

输入重定向中用到的符号及其作用

  • 命令 < 文件;将文件作为命令的标准输入
  • 命令 << 分界符;从标准输入中读入,直到遇见分界符才停止
  • 命令 < 文件1 > 文件2;将文件1作为命令的标准输入并将标准输出到文件2
    输出重定向中用到的符号及其作用
  • 命令 > 文件;将标准输出重定向到一个文件中(清空原有文件的数据)
  • 命令 2> 文件;将错误输出重定向到一个文件中(清空原有文件的数据)
  • 命令 >> 文件;将标准输出重定向到一个文件中(追加到原有内容的后面)
  • 命令 2>> 文件;将错误输出重定向到一个文件中(追加到原有内容的后面)
  • 命令 >> 文件 2>&1 或 命令 &>> 文件;将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)

管道命令符

命令A | 命令B;把前一个命令原本要输出到屏幕的标准正常数据当作是后一个命令的标准输入.。

在修改用户密码时,通常都需要输入两次密码以进行确认,通过把管道符和passwd命令的--stdin参数相结合,我们可以用一条命令来完成密码重置操作:

[root@linuxprobe ~]# echo "linuxprobe" | passwd --stdin root

在发送电子邮件时,默认采用交互式的方式来进行,我们完全可以利用一条结合了管道符的命令语句,把编辑好的内容与标题一起“打包”,最终用这一条命令实现邮件的发送:

[root@linuxprobe ~]# echo "Content" | mail -s "Subject" linuxprobe

让用户一直输入内容,直到用户输入了其自定义的分界符时,才结束输入:

[root@linuxprobe ~]# mail -s "Readme" [email protected] << over
> I think linux is very practical
> I hope to learn more
> can you teach me ?
> over
[root@linuxprobe ~]#

命令行的通配符

星号(*)代表匹配零个或多个字符。
问号(?)代表匹配单个字符。
中括号内加上数字[0-9]代表匹配0~9之间的单个数字的字符。
中括号内加上字母[abc]则是代表匹配a、b、c三个字符中的任意一个字符。

常用的转义字符

4个最常用的转义字符如下所示
反斜杠(\):使反斜杠后面的一个变量变为单纯的字符串。

单引号(''):转义其中所有的变量为单纯的字符串。

双引号(""):保留其中的变量属性,不进行转义处理。

反引号(``):把其中的命令执行后返回结果。

重要的环境变量

命令在Linux中的执行分为4个步骤。
第1步:判断用户是否以绝对路径或相对路径的方式输入命令(如/bin/ls),如果是的话则直接执行。
第2步:Linux系统检查用户输入的命令是否为“别名命令”,即用一个自定义的命令名称来替换原本的命令名称。

  • 可以用alias命令来创建一个属于自己的命令别名,格式为“alias 别名=命令”。
  • 若要取消一个命令别名,则是用unalias命令,格式为“unalias 别名”。
    第3步:Bash解释器判断用户输入的是内部命令还是外部命令。
  • 可以使用“type命令名称”来判断用户输入的命令是内部命令还是外部命令。
    第4步:系统在多个路径中查找用户输入的命令文件,而定义这些路径的变量叫作PATH,可以简单地把它理解成是“解释器的小助手”,作用是告诉Bash解释器待执行的命令可能存放的位置,然后Bash解释器就会乖乖地在这些位置中逐个查找。

Linux系统中最重要的10个环境变量

  • HOME 用户的主目录(即家目录)
  • SHELL 用户在使用的Shell解释器名称
  • HISTSIZE 输出的历史命令记录条数
  • HISTFILESIZE 保存的历史命令记录条数
  • MAIL 邮件保存路径
  • LANG 系统语言、语系名称
  • RANDOM 生成一个随机数字
  • PS1 Bash解释器的提示符
  • PATH 定义解释器搜索用户执行命令的路径
  • EDITOR 用户默认的文本编辑器

使用export命令将自定义变量提升为全局变量,这样其他用户也就可以使用它了:

#root下建立自定义变量
[root@linuxprobe ~]# WORKDIR=/home/workdir
[root@linuxprobe ~]# cd $WORKDIR 
#切换用户,变量不起作用
[root@linuxprobe workdir]# su linuxprobe
[linuxprobe@linuxprobe ~]$ echo $WORKDIR
[linuxprobe@linuxprobe ~]$ exit
#返回root用户,提升全局变量
[root@linuxprobe ~]# export WORKDIR
#切换普通用户
[root@linuxprobe ~]# su linuxprobe
[linuxprobe@linuxprobe ~]$ cd $WORKDIR
[linuxprobe@linuxprobe workdir]$ pwd
/home/workdir

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325521032&siteId=291194637