Linux Shell Basics (b)

1. This knowledge structure

2. query and retrieve files

2.1. Cd to change directories

  • Locate the file / directory location: cd
  • Switch to a working directory: cd -
  • Switch to home directory: cd or cd ~
  • Displays the current path: pwd
  • Change the current working directory path: $ cd path

2.2. Locate locate directories and files

  • find real-time lookup
  • locate database indexing for the file system, if there is a file update, you need to perform an update command to update the index database on a regular basis, find the file first before updating the database
# 搜寻文件或目录:
$find ./ -name "core*" | xargs file

# 查找目标文件夹中是否有obj文件:
$find ./ -name '*.o'

# 递归当前目录及子目录删除所有.o文件
$find ./ -name "*.o" -exec rm {} \;

# 寻找包含有string的路径
$updatedb
$locate string

2.3. Chown file and directory permissions to modify

  • Chown change file owner
  • Change file read, write, execute, and other attributes chmod
  • Recursive subdirectories Review: chown -R tuxapp source /
  • Added a script executable permissions: chmod a + x myscript

2.4 pipes and redirection

  • Batch command execution connection, use |
  • Series: a semicolon;
  • Earlier successful, the implementation of a back, otherwise, do not perform: &&
  • Front fails, after an execution: ||
  • Redirect:>

3. Text Processing

3.1. Find File Finder

find command to find the file in the specified directory. If you use this command does not set any parameters, the find command will find subdirectories and files in the current directory, and find all the subdirectories and files are displayed.

In general, its syntax is:

find path -option [ -print ] [ -exec -ok command ] {} \;

After find divided into two parts: path and expression, in the first - for the separator line. If the path is an empty string is using the current path, if the expression is an empty string is used as the default -print expression.

Commonly used expression options

Options meaning
-mount, -xdev Just check the file and specify a directory in the same file system, avoid listing files in other file systems
-amin n It has been read in the past n minutes
-anewer file Later than the document file has been read files
-atime n It has been read in the last n days Files
-cmin n It has been modified in the last n minutes
-cnewer file Later than the document file is updated files
-ctime n It has been modified in the last n days Files
-empty Empty file
-gid n , -group name n is gid name or file name is the group
-ipath p , path p P path name that matches the file, ipath ignore case
-name name , -iname name File name matches the name of the file. iname ignore case
-size n File size is n units. B represents a block group 512 yuan, c denotes the number of characters, k denotes kilo bytes, w is two bytes
-type c File type is c file. d represents a directory, c representative of the font file means, b means in the block representative file, p behalf of the named storage column, f representative of a general document, l representing the symbol link, s behalf socket
-pid n n is the process id of the file

In addition, you can also find find files using regular expressions.

Using regular expressions queries need to have two options: -regextype and -regex. ---- regextype specify the type of regular match, there are many options available, common are: 'awk', 'egrep', 'ed', 'emacs', 'grep', 'sed' and so on, different matching pattern matching the result is not the same. The default is emacs.

-regex pattern indicates that the file name and a regular expression pattern matching. This is a match for the whole path, not a search for files. That writing regular Dynasty to the front of the path expression.

3.2. Grep text search

grep command is used to find content that contains the specified template style file if the contents of a file found to comply with the specified style templates, preset grep command that will contain a column style template is displayed.

Its general syntax is:

grep [ options ] pattern [file]

Common options Option

Short order Long command meaning
-a --text Do not ignore binary data
-A --after-context = 行数 In addition to displaying the line in line with the style of the model outside, and displays the contents after the line
-b --byte-offset Prior to demonstrate compliance with the line style, mark the first character of the line number
-c --count Calculated in line with the number of columns styles
-C --context = number of lines In addition to displaying the line styles in line outside, and displays the contents of the line before
-d --directories = action When you specify when you want to find is a directory rather than a file, you must use this parameter, otherwise the grep command will return information and stop action
-e --regexp = Template Styles Find the file contents as specified string style
-E --extended-regexp The pattern is a regular expression used extending
-f --file = Rules Specify rules file, the contents of which contain one or more regular pattern, so grep to find content file rule conditions are met, the format for each line of a regular pattern
-F --fixed-regexp The style seen as a list of fixed strings
-G --basic-regexp It will be treated as ordinary style of notation to use
-h --no-filename Prior to demonstrate compliance with the line style, the line does not indicate the name of the file belongs
-H --with-filename Before displaying the line in line with the style, meaning the file name of the row belongs
-i --ignore-case Ignore character case differences
-l --file-with-match Lists the contents of the documents in line with the file name specified style
-L --file-without-match Lists the contents of the file file name does not comply with the specified style
-n --line-number Prior to demonstrate compliance with the line style, mark the number of columns in the row number
-The --only-matching Show only the matching pattern portion
-q --quiet , --silent Does not display any information
-r --recursive And the effect of this parameter specifies the same "-d recurse" parameter
-s --no-messages No error message
-v --revert-match Display does not contain matching text of all lines
-V --version Displays version information
-w --word-regexp Show only in line with the whole-word column
-x --line-regexp Column column display only full compliance with the
-Y The same effects and parameters specified for this parameter -i

3.3. Xargs command line parameter conversion

xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了 xargs 命令。xargs 一般是和管道一起使用。

其一般语法为:

somecommand | xargs -item command

常见的item选项

选项 含义
-a file 从文件中读入作为sdtin
-e flag 当xargs分析到含有flag这个标志的时候就停止,默认为'_'
-p 当每次执行一个argument的时候询问一次用户
-n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的
-t 先打印命令,然后再执行
-r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了
-s num 命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数
-L num 从标准输入一次读取 num 行送给 command 命令
-d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符
-x exit的意思,主要是配合-s使用
-P 修改最大的进程数,默认是1,为0时候为as many as it can

3.4. uniq 删除重复

uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

# 消除重复行
$ sort unsort.txt | uniq

# 统计各行在文件中出现的次数
$ sort unsort.txt | uniq -c

# 找出重复行
$ sort unsort.txt | uniq -d

3.5. tr 字符转换

tr 命令用于转换或删除文件中的字符。它从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。

其一般语法为:

tr [options]  SET1 [SET2]

常见options选项

短命令 长命令 含义
-c --complement 反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
-d --delete 删除指令字符
-s --squeeze-repeats 缩减连续重复的字符成指定的单个字符
-t --truncate-set1 削减 SET1 指定范围,使之与 SET2 设定长度相等

常见SET字符含义

字符 含义
\NNN 八进制值为NNN的字符
\\ 反斜杠
\a 终端鸣响
\b 退格
\f 换页
\n 换行
\r 回车
\t 水平制表符
\v 垂直制表符
char1-char2 从char1到char2的升序递增过程中经历的所有字符
[:alnum:] 所有的字母和数字
[:alpha:] 所有的字母
[:blank:] 所有呈水平排列的空白字符
[:cntrl:] 所有的控制字符
[:digit:] 所有的数字
[:graph:] 所有的可打印字符,不包括空格
[:lower:] 所有的小写字母
[:print:] 所有的可打印字符,包括空格
[:punct:] 所有的标点字符
[:space:] 所有呈水平或垂直排列的空白字符
[:upper:] 所有的大写字母
[:xdigit:] 所有的十六进制数
[=char=] 所有和指定char相等的字符

3.6. cut、paste 裁剪与拼接

cut命令用于显示每行从开头算起 num1 到 num2 的文字。

  • cut 取的范围

    N- 第N个字段到结尾

    -M 第1个字段为M

    N-M N到M个字段

  • cut 取的单位

    -b 以字节为单位

    -c 以字符为单位

    -f 以字段为单位(使用定界符)

paste 指令会把每个文件以列对列的方式,一列列地加以合并。

其一般语法为:

$ paste [-s] [-d <间隔字符>] [文件...]

参数:

  • -d <间隔字符> 或--delimiters= <间隔字符>  用指定的间隔字符取代跳格字符。
  • -s或--serial  串列进行而非平行处理。
  • --help  在线帮助。
  • --version  显示帮助信息。
  • [文件…] 指定操作的文件路径

3.7. sed 文本替换

sed 命令可依照脚本的指令来处理、编辑文本文件,它主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。sed 命令是一行一行的处理文件内容的,一般在动作前加数字以确定对应的行。

其一般语法为:

$ sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明

  • -e script 或--expression=script 以选项中指定的script来处理输入的文本文件。
  • -f script文件 或--file=script文件 以选项中指定的script文件来处理输入的文本文件。
  • -h或--help 显示帮助。
  • -n或--quiet或--silent 仅显示script处理后的结果。
  • -V或--version 显示版本信息。
  • -r或--regexp-extended 在脚本中使用扩展的正则表达式

动作说明

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行
  • d :删除,因为是删除啊,所以 d 后面通常不接任何东西
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行
  • s :取代,可以直接进行字符级的替换的工作
# sed 命令需要多练习,可参考菜鸟教程实例

3.8. awk 数据流处理

awk 是一种处理文本文件的语言,是一个强大的文本分析工具。

其一般语法为:

awk [选项参数] 'script' var=value file(s)
awk [选项参数] -f scriptfile var=value file(s)

选项参数说明:

  • -F fs or --field-separator fs
    指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:。
  • -v var=value or --asign var=value
    赋值一个用户定义变量。
  • -f scripfile or --file scriptfile
    从脚本文件中读取awk命令。
  • -mf nnn and -mr nnn
    对nnn值设置内在限制,-mf选项限制分配给nnn的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用。
  • -W compact or --compat, -W traditional or --traditional
    在兼容模式下运行awk。所以gawk的行为和标准的awk完全一样,所有的awk扩展都被忽略。
  • -W copyleft or --copyleft, -W copyright or --copyright
    打印简短的版权信息。
  • -W help or --help, -W usage or --usage
    打印全部awk选项和每个选项的简短说明。
  • -W lint or --lint
    打印不能向传统unix平台移植的结构的警告。
  • -W lint-old or --lint-old
    打印关于不能向传统unix平台移植的结构的警告。
  • -W posix
    打开兼容模式。但有以下限制,不识别:/x、函数关键字、func、换码序列以及当fs是一个空格时,将新行作为一个域分隔符;操作符=不能代替^和^=;fflush无效。
  • -W re-interval or --re-inerval
    允许间隔正则表达式的使用,参考(grep中的Posix字符类),如括号表达式[[:alpha:]]。
  • -W source program-text or --source program-text
    使用program-text作为源代码,可与-f命令混用。
  • -W version or --version
    打印bug报告信息的版本。

4.进程管理

4.1. lsof 查看进程状态

lsof 命令的全称是 list open files ,它是一个列出当前系统打开文件的工具。在 Linux 环境中,所有的事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。

其一般语法为:

$ lsof [options] filename

常用参数列表:

选项 含义
lsof filename 显示打开指定文件的所有进程
lsof -a 表示两个参数都必须满足时才显示结果
lsof -c string 显示COMMAND列中包含指定字符的进程所有打开的文件
lsof -u username 显示所属user进程打开的文件
lsof -g gid 显示归属gid的进程情况
lsof +d /DIR/ 显示目录下被进程打开的文件
lsof +D /DIR/ 显示目录下被进程打开的文件,它会搜索目录下的所有目录,时间相对较长
lsof -d FD 显示指定文件描述符的进程
lsof -n 不将IP转换为hostname,缺省是不加上-n参数
lsof -i 用以显示符合条件的进程情况
$ lsof -i[46] [protocol][@hostname|hostaddr][:service|port]

# 46 –> IPv4 or IPv6
# protocol –> TCP or UDP
# hostname –> Internet host name
# hostaddr –> IPv4地址
# service –> /etc/service中的 service name (可以不只一个)
# port –> 端口号 (可以不只一个)

4.2. pmap 分析线程栈

Pmap 提供了进程的内存映射,pmap命令用于显示一个或多个进程的内存状态。其报告进程的地址空间和内存状态信息

其一般语法为:

$ pmap [options] PID

常见的options选项为:

  • -x:显示扩展格式;
  • -d:显示设备格式;
  • -q:不显示头尾行;
  • -V:显示指定版本。

5.性能监控

5.1. sar 性能监控

sar 命令是系统维护的重要工具,主要帮助我们掌握系统资源的使用情况,特别是内存和CPU 的使用情况。

其一般语法为:

$ sar [options] [-A] [-o file] t [n]

其中:t为采样间隔,n为采样次数,默认值是1;-o file表示将命令结果以二进制格式存放在文件中,file 是文件名。

常见的options选项有:

选项 含义
-A 所有报告的总和
-u 输出CPU使用情况的统计信息
-v 输出inode、文件和其他内核表的统计信息
-d 输出每一个块设备的活动信息
-r 输出内存和交换空间的统计信息
-b 显示I/O和传送速率的统计信息
-a 文件读写情况
-c 输出进程统计信息,每秒创建的进程数
-R 输出内存页面的统计信息
-y 终端设备活动情况
-w 输出系统交换活动信息

参考材料:

Linux教程|菜鸟教程
Linux Tools Quick Tutorial
Linux 命令帮助文档

Guess you like

Origin www.cnblogs.com/fang-ren/p/11647675.html