设为首页 收藏本站
查看: 959|回复: 0

[经验分享] Linux命令总结1

[复制链接]

尚未签到

发表于 2018-5-20 11:39:18 | 显示全部楼层 |阅读模式
  1,mv (move (rename) files)移动文件或一并改名
  参数:
  -b,   like --backup but does not accept an argument  (如果已经有相同的文件,会先备份一个副本)
  -f, --force
  do not prompt before overwriting       (强制执行,覆盖时不提示)
  -i, --interactive                             (覆盖前提示)
  prompt before overwrite
  -n, --no-clobber
  do not overwrite an existing file
  If you specify more than one of -i, -f, -n, only the final one takes effect.( 如果你一下指定了多个参数-i,-f,-n只有最后一个生效)
  所以:mv不能用混合参数,只能用单一参数
  --strip-trailing-slashes
  remove any trailing slashes from each SOURCE argument
  

  -S, --suffix=SUFFIX
  override the usual backup suffix
  

  -t, --target-directory=DIRECTORY
  move all SOURCE arguments into DIRECTORY
  

  -T, --no-target-directory
  treat DEST as a normal file
  

  -u, --update
  move only when the SOURCE file is newer than the destination file or when the destination file
  is missing
  

  -v, --verbose
  explain what is being done
  

  --help display this help and exit
  

  --version
  output version information and exit
  

  使用实例:
  1)移动文件。
  [root@learnlinux test]# ll
  total 20
  drwxr-xr-x. 2 root root 4096 Apr 28 16:24 11
  -rw-r--r--. 1 root root    9 Apr 28 16:54 2
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 a
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 b
  drwxr-xr-x. 3 root root 4096 Apr 28 14:41 c
  [root@learnlinux test]# cat 2
  safhafdj
  [root@learnlinux test]# mv 2 3           (移动文件2并改名为3)
  [root@learnlinux test]# ll
  total 20
  drwxr-xr-x. 2 root root 4096 Apr 28 16:24 11
  -rw-r--r--. 1 root root    9 Apr 28 16:54 3
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 a
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 b
  drwxr-xr-x. 3 root root 4096 Apr 28 14:41 c
  [root@learnlinux test]# cat 3
  safhafdj
  

  2)移动目录。
  [root@learnlinux test]# ll
  total 20
  -rw-r--r--. 1 root root    9 Apr 28 16:54 3
  drwxr-xr-x. 2 root root 4096 Apr 28 14:41 a
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d      ( a,b,c,d为目录)
  [root@learnlinux test]# mv a b                   (移动a到b,当b目录存在时,a会移动到b目录下面。当b目录不存在时,a目录改名为b)
  [root@learnlinux test]# ll
  total 16
  -rw-r--r--. 1 root root    9 Apr 28 16:54 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cd b
  [root@learnlinux b]# ll
  total 4
  drwxr-xr-x. 2 root root 4096 Apr 28 14:41 a
  

  3)当文件同名时,用-b会复制个文件副本。
  [root@learnlinux test]# ll
  total 20
  -rw-r--r--. 1 root root    4 Apr 29 14:34 1
  -rw-r--r--. 1 root root    9 Apr 28 16:54 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 1
  234
  [root@learnlinux test]# cat 3
  safhafdj
  [root@learnlinux test]# mv -b 1 3
  mv: overwrite `3'? y                            (提示:是否要覆盖y,n取消本次移动)
  [root@learnlinux test]# ll
  total 20
  -rw-r--r--. 1 root root    4 Apr 29 14:34 3
  -rw-r--r--. 1 root root    9 Apr 28 16:54 3~    (这个是文件3的副本)
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 3
  234
  [root@learnlinux test]# cat 3~
  safhafdj
  

  4)-f,强行覆盖,不提示。
  [root@learnlinux test]# ll
  total 20
  -rw-r--r--. 1 root root    4 Apr 29 14:34 1
  -rw-r--r--. 1 root root    9 Apr 28 16:54 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 1
  234
  [root@learnlinux test]# cat 3
  safhafdj
  [root@learnlinux test]# mv -f 1 3        (没有提示)
  [root@learnlinux test]# ll
  total 16
  -rw-r--r--. 1 root root    4 Apr 29 14:34 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 3
  234                                        (3的内容已经被覆盖成1的内容)
  

  5)-i,覆盖时提示是否要覆盖。
  -rw-r--r--. 1 root root    4 Apr 29 14:58 1
  -rw-r--r--. 1 root root    4 Apr 29 14:34 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 3
  234
  [root@learnlinux test]# echo jhdf >>1
  [root@learnlinux test]# cat 1
  123
  jhdf
  [root@learnlinux test]# cat 3
  234
  [root@learnlinux test]# mv -i 1 3
  mv: overwrite `3'? y
  [root@learnlinux test]# ll
  total 16
  -rw-r--r--. 1 root root    9 Apr 29 14:59 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 3
  123
  jhdf
  

  

  系统默认已经将命令设置了别名mv = mv -i 所以前面移动时不加-i也会提示
  [root@learnlinux test]# alias
  alias cp='cp -i'
  alias l.='ls -d .* --color=auto'
  alias ll='ls -l --color=auto'
  alias ls='ls --color=auto'
  alias mv='mv -i'              (已经设置了,所以输入mv等于输mv -i了)
  alias rm='rm -i'
  alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  

  

  2,touch (- change file timestamps)改变文件时间戳  (还有创建文档的意思)
  参数:
  

  -a     change only the access time  (仅仅改变访问时间)
  

  -c, --no-create                     (不创建任何文件)
  do not create any files
  

  -d, --date=STRING
  parse STRING and use it instead of current time   (解析字符串并用它代替当前时间)
  

  -f     (ignored)
  

  -h, --no-dereference
  affect each symbolic link instead of any referenced file (useful
  only on systems that can change the timestamps of a symlink)
  

  -m     change only the modification time
  

  -r, --reference=FILE
  use this fileas times instead of current time
  

  -t STAMP
  use [[CC]YY]MMDDhhmm[.ss] instead of current time
  

  --time=WORD
  change the specified time: WORD is access, atime, or use: equiv-
  alent to -a WORD is modify or mtime: equivalent to -m
  

  --help display this help and exit
  

  --version
  output version information and exit
  

  使用实例:
  1)当文件存在时,仅仅改变文档时间。
  [root@learnlinux test]# ll
  total 16
  -rw-r--r--. 1 root root    9 Apr 29 14:59 3      (时间为14:59)
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# touch 3
  [root@learnlinux test]# ll
  total 16
  -rw-r--r--. 1 root root    9 Apr 29 15:11 3       (时间变成15:11)
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# cat 3
  123
  jhdf
  

  2)当文件不存在时,就创建文件。
  [root@learnlinux test]# touch test.txt
  [root@learnlinux test]# ll
  total 16
  -rw-r--r--. 1 root root    0 Apr 29 15:12 1
  -rw-r--r--. 1 root root    9 Apr 29 15:11 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  -rw-r--r--. 1 root root    0 Apr 29 15:28 test.txt
  

  NAME
  cat - concatenate files and print on the standard output   连接到文件并打印到标准输出。(查看文件内容)
  

  SYNOPSIS
  cat [OPTION]... [FILE]...
  

  DESCRIPTION
  Concatenate FILE(s), or standard input, to standard output.
  

  -A, --show-all
  equivalent to -vET    (等同于-vET)
  

  -b, --number-nonblank
  number nonempty output lines   (为查看文本添加行号,空行不加行号)
  

  -e     equivalent to -vE
  

  -E, --show-ends
  display $ at end of each line
  

  -n, --number
  number all output lines        (给查看的内容添加行号,空行也有行号)
  

  -s, --squeeze-blank
  suppress repeated empty output lines
  

  -t     equivalent to -vT
  

  -T, --show-tabs
  display TAB characters as ^I
  

  -u     (ignored)
  

  -v, --show-nonprinting
  use ^ and M- notation, except for LFD and TAB
  

  --help display this help and exit
  

  --version
  output version information and exit
  

  使用实例:
  1)查看文件内容
  [root@learnlinux test]# ll
  total 20
  -rw-r--r--. 1 root root   52 Apr 29 15:51 1
  -rw-r--r--. 1 root root    9 Apr 29 15:11 3
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  -rw-r--r--. 1 root root    0 Apr 29 15:28 test.txt
  [root@learnlinux test]# cat 1
  yrwe
  rtur4
  36
  264327
  

  327
  6
  347
  32467
  

  7426
  2365
  

  

  2)查看内容,并为内容添加行号,空行不加行号
  [root@learnlinux test]# cat -b 1
  1  yrwe
  2  rtur4
  3  36
  4  264327
  

  5  327
  6  6
  7  347
  8  32467
  

  9  7426
  10  2365
  

  

  3)查看内容,并为内容添加行号,空行也添加行号
  [root@learnlinux test]# cat -n 1
  1  yrwe
  2  rtur4
  3  36
  4  264327
  5
  6  327
  7  6
  8  347
  9  32467
  10
  11  7426
  12  2365
  13
  14
  15
  

  4)打入新内容到文档,并覆盖原来内容
  [root@learnlinux test]# cat 1.txt
  yrwe
  rtur4
  36
  264327
  

  327
  6
  347
  32467
  

  7426
  2365
  

  [root@learnlinux test]# cat > 1.txt <<eof   (>为重定向,>>为追加重定向,eof为开始和结束符,任意字符都可以,但开始和结尾必须相同)
  > aabc
  > shag
  > jsahgj                       (为输入的内容)
  > sahfj
  > eof
  [root@learnlinux test]# cat 1.txt
  aabc
  shag
  jsahgj
  sahfj
  

  5)在文档末端增加文本
  [root@learnlinux test]# cat >>1.txt <<eof
  > 1234
  > 35436
  > 165464
  > 7547                          (为输入内容)
  > 754
  > eof
  [root@learnlinux test]# cat 1.txt
  aabc
  shag
  jsahgj
  sahfj
  1234
  35436
  165464
  7547
  754
  

  3,rm - remove files or directories(删除文件或目录)
  参数:
  -f, --force
  ignore nonexistent files, never prompt  (忽略不存在的文件,不提示)
  

  -i     prompt before every removal           (每次删除前提示)
  

  -I     prompt once  before  removing  more  than  three
  files,   or  when  removing  recursively.   Less
  intrusive than -i, while still giving protection
  against most mistakes
  -r, -R, --recursive
  remove  directories  and  their  contents recur-sively (递归地删除目录及其内容)
  

  -v, --verbose
  explain what is being done      (解释正在做的事情)
  1)删除文件。
  [root@learnlinux test]# rm -v test.txt   (-v输出删除信息)
  removed `test.txt'
  [root@learnlinux test]# ll
  

  [root@learnlinux test]# ll
  total 12
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  -rw-r--r--. 1 root root    0 Apr 29 16:57 test.txt
  [root@learnlinux test]# rm -i test.txt     (-i提示并问要删除文件吗?y确定,n取消)
  rm: remove regular empty file `test.txt'? y
  [root@learnlinux test]# ll
  total 12
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29
  

  

  [root@learnlinux test]# ll
  total 12
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  -rw-r--r--. 1 root root    0 Apr 29 16:58 test.txt
  [root@learnlinux test]# rm -f test.txt      (-f强制删除)
  [root@learnlinux test]# ll
  total 12
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  

  2)删除目录,要加-r参数
  [root@learnlinux b]# rm b  (没有这个b目录时,删除目录b,会提示,如下面)
  rm: cannot remove `b': No such file or directory  (rm:无法删除` b”:没有这样的文件或目录 。因为当前在目录b下,而b目录下并没有b目录)
  

  [root@learnlinux test]# ll
  total 12
  drwxr-xr-x. 3 root root 4096 Apr 29 06:27 b
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29 d
  [root@learnlinux test]# rm b
  [root@learnlinux test]# rm b          (不加参数-r时,会提示)
  rm: cannot remove `b': Is a directory  (rm:不能删除`b':是一个目录)
  [root@learnlinux test]# rm -r b        (加-r后,会立即删除)
  [root@learnlinux test]# ll
  total 8
  drwxr-xr-x. 2 root root 4096 Apr 29 06:26 c
  drwxr-xr-x. 2 root root 4096 Apr 28 14:29
  

  注意:rm命令要慎用,一旦删除,数据就会丢失。所以用该命令时,要先备份数据。切记,切记!!
  

  

  4,rmdir -remove empty directories(删除空目录)
  

  [root@learnlinux aa]# ll
  total 8
  drwxr-xr-x. 2 root root 4096 Apr 30 14:26 test1
  drwxr-xr-x. 2 root root 4096 Apr 30 14:25 test2
  [root@learnlinux aa]# cd test1
  [root@learnlinux test1]# ll
  total 0
  -rw-r--r--. 1 root root 0 Apr 30 14:26 1
  -rw-r--r--. 1 root root 0 Apr 30 14:26 2
  -rw-r--r--. 1 root root 0 Apr 30 14:26 3
  [root@learnlinux test1]# cd ../test2
  [root@learnlinux test2]# ll
  total 0
  [root@learnlinux test2]# cd ../
  [root@learnlinux aa]# rmdir test1
  rmdir: failed to remove `test1': Directory not empty
  [root@learnlinux aa]# rmdir test2
  [root@learnlinux aa]# ll
  total 4
  drwxr-xr-x. 2 root root 4096 Apr 30 14:26 test1
  

  

  5, head - output the first part of files(输出文件前面一部分)-----默认显示文件前十行
  

  [root@learnlinux ~]# cat test.txt
  ekwlksdagl
  sajgkjsagjs
  249u49ajtu49
  833t3  jsjsdfg
  agkjkjs02393-e
  faskjgjht934u
  ashrkelyk23
  92390-5035-=23943959
  jastihioerooeeeeeejds
  asjdfffffffks
  2388888888883
  rasjtgjijgtijgag
  

  [root@learnlinux ~]# head test.txt
  ekwlksdagl
  sajgkjsagjs
  249u49ajtu49
  833t3  jsjsdfg
  agkjkjs02393-e
  faskjgjht934u
  ashrkelyk23
  92390-5035-=23943959
  jastihioerooeeeeeejds
  asjdfffffffks
  [root@learnlinux ~]# head -5 test.txt
  ekwlksdagl
  sajgkjsagjs
  249u49ajtu49
  833t3  jsjsdfg
  agkjkjs02393-e
  [root@learnlinux ~]# head -12 test.txt
  ekwlksdagl
  sajgkjsagjs
  249u49ajtu49
  833t3  jsjsdfg
  agkjkjs02393-e
  faskjgjht934u
  ashrkelyk23
  92390-5035-=23943959
  jastihioerooeeeeeejds
  asjdfffffffks
  2388888888883
  rasjtgjijgtijgag
  

  6,tail - output the last part of files(输出文件结尾一部分)---默认结尾十行
  

  [root@learnlinux ~]# cat test.txt
  ekwlksdagl
  sajgkjsagjs
  249u49ajtu49
  833t3  jsjsdfg
  agkjkjs02393-e
  faskjgjht934u
  ashrkelyk23
  92390-5035-=23943959
  jastihioerooeeeeeejds
  asjdfffffffks
  2388888888883
  rasjtgjijgtijgag
  [root@learnlinux ~]# tail test.txt
  833t3  jsjsdfg
  agkjkjs02393-e
  faskjgjht934u
  ashrkelyk23
  92390-5035-=23943959
  jastihioerooeeeeeejds
  asjdfffffffks
  2388888888883
  rasjtgjijgtijgag
  [root@learnlinux ~]# tail -5 test.txt
  jastihioerooeeeeeejds
  asjdfffffffks
  2388888888883
  rasjtgjijgtijgag
  (空行也显示)
  [root@learnlinux ~]#
  

  7,grep
  

  Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是
  Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。
  grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被
  看作文件名。搜索的结果被送到标准输出,不影响原文件内容
  grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回
  1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。
  

  1.命令格式:
  grep [option] pattern file
  2.命令功能:
  用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活
  3.命令参数:
  -a   --text   #不要忽略二进制的数据。
  -A<显示行数>   --after-context=<显示行数>   #除了显示符合范本样式的那一列之外,并显示该行之后的内容。
  -b   --byte-offset   #在显示符合样式的那一行之前,标示出该行第一个字符的编号。
  -B<显示行数>   --before-context=<显示行数>   #除了显示符合样式的那一行之外,并显示该行之前的内容。
  -c    --count   #计算符合样式的列数。
  -C<显示行数>    --context=<显示行数>或-<显示行数>   #除了显示符合样式的那一行之外,并显示该行之前后的内容。
  -d <动作>      --directories=<动作>   #当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
  -e<范本样式>  --regexp=<范本样式>   #指定字符串做为查找文件内容的样式
  -E      --extended-regexp   #将样式为延伸的普通表示法来使用。
  -f<规则文件>  --file=<规则文件>   #指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件
  内容,格式为每行一个规则样式
  -F   --fixed-regexp   #将样式视为固定字符串的列表。
  -G   --basic-regexp   #将样式视为普通的表示法来使用
  -h   --no-filename   #在显示符合样式的那一行之前,不标示该行所属的文件名称。
  -H   --with-filename   #在显示符合样式的那一行之前,表示该行所属的文件名称。
  -i    --ignore-case   #忽略字符大小写的差别。
  -l    --file-with-matches   #列出文件内容符合指定的样式的文件名称。
  -L   --files-without-match   #列出文件内容不符合指定的样式的文件名称。
  -n   --line-number   #在显示符合样式的那一行之前,标示出该行的列数编号。
  -q   --quiet或--silent   #不显示任何信息。
  -r   --recursive   #此参数的效果和指定“-d recurse”参数相同
  -s   --no-messages   #不显示错误信息。
  -v   --revert-match   #显示不包含匹配文本的所有行
  -V   --version   #显示版本信息。
  -w   --word-regexp   #只显示全字符合的列。
  -x    --line-regexp   #只显示全列符合的列。
  -y   #此参数的效果和指定“-i”参数相同。
  4.规则表达式:
  ^  #锚定行的开始 如:'^grep'匹配所有以grep开头的行。
  $  #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。
  .  #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
  *  #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。
  .*   #一起用代表任意字符。
  []   #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。
  [^]  #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。
  \(..\)  #标记匹配字符,如'\(love\)',love被标记为1。
  \<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。
  \>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。
  x\{m\}  #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。
  x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。
  x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。
  \w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。
  \W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。
  \b    #单词锁定符,如: '\bgrep\b'只匹配grep。
  
  5. 使用实例:
  实例1:查找指定进程
  命令:
  ps -ef|grep svn
  

  输出:
  [root@learn ~]# ps -ef|grep svn
  root      2748  2680  3 15:56 pts/0    00:00:00 grep svn
  

  实例2:查找指定进程个数
  命令:
  ps -ef|grep -c svn
  ps -ef|grep svn -c
  输出:
  [root@learn ~]# ps -ef|grep -c svn
  1
  [root@learn ~]# ps -ef|grep svn -c
  1
  

  实例3:从文件中读取关键词进行搜索
  命令:
  cat test.txt|grep -f test2.txt
  输出:
  [root@learn ~]# cat test.txt
  learn linux
  show
  beatuful
  abc
  redhat
  linux
  [root@learn ~]# cat test2.txt
  redhat
  linux
  abcdef
  [root@learn ~]# cat test.txt|grep -f test2.txt
  learn linux
  redhat
  linux
  说明:
  输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行
  

  实例3:从文件中读取关键词进行搜索 且显示行号
  命令:
  cat test.txt|grep -nf test2.txt
  输出:
  [root@learn ~]# cat test.txt
  learn linux
  show
  beatuful
  abc
  redhat
  linux
  [root@learn ~]# cat test2.txt
  redhat
  linux
  abcdef
  [root@learn ~]# cat test.txt|grep -nf test2.txt
  1:learn linux
  5:redhat
  6:linux
  说明:
  输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,并显示每一行的行号
  

  实例4:从文件中查找关键词
  

  命令:
  grep “Linux” test.txt
  

  输出:
  [root@learn ~]# grep  'linux' test.txt
  learn linux
  linux
  [root@learn ~]# grep  linux test.txt
  learn linux
  linux
  [root@learn ~]# grep  "linux" test.txt
  learn linux
  linux
  

  实例5:从多个文件中查找关键词。
  

  命令:
  

  grep linux test.txt test2.txt
  

  输出:
  [root@learn ~]# grep linux test.txt test2.txt
  test.txt:learn linux
  test.txt:linux
  test2.txt:linux
  说明:
  多文件时,输出查询到的信息内容行时,会把文件名放在查找出来内容行最前面并且加上":"作为标示符
  

  实例7:grep不显示本身进程
  

  命令:
  ps aux|grep \sh
  ps aux | grep ssh | grep -v "grep"
  

  输出:
  [root@learn ~]# ps aux|grep ssh
  root       810  0.0  0.1   8944  1028 ?        Ss   14:03   0:00 /usr/sbin/sshd
  root      2650  0.0  0.4  11860  3288 ?        Ss   15:28   0:00 sshd: test [priv]
  test      2652  0.0  0.2  11860  1548 ?        S    15:28   0:00 sshd: test@pts/0
  root      2820  2.0  0.0   4356   712 pts/0    S+   16:44   0:00 grep ssh
  [root@learn ~]# ps aux|grep \sh
  root       810  0.0  0.1   8944  1028 ?        Ss   14:03   0:00 /usr/sbin/sshd
  root      2650  0.0  0.4  11860  3288 ?        Ss   15:28   0:00 sshd: test [priv]
  test      2652  0.0  0.2  11860  1548 ?        S    15:28   0:00 sshd: test@pts/0
  

  实例8:找出以字母l开头的内容
  

  命令:
  grep ^l test.txt
  

  输出:
  [root@learn ~]# grep ^l test.txt
  learn linux
  linux
  

  实例9:找出非以字母l开头的内容
  

  命令:
  grep ^[^l] test.txt
  

  输出:
  [root@learn ~]# cat test.txt
  learn linux
  show
  beatuful
  abc
  redhat
  linux
  [root@learn ~]# grep ^[^l] test.txt
  show
  beatuful
  abc
  redhat
  

  实例10:找出以linux结尾的行内容
  

  命令:
  grep linux$ test.txt
  

  输出:
  [root@learn ~]# cat test.txt
  learn linux
  show
  beatuful
  abc
  redhat
  linux
  [root@learn ~]# grep linux$ test.txt
  learn linux
  linux
  [root@learn ~]#  cat test.txt|grep linux$
  learn linux
  linux
  

  实例11:显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
  

  命令:
  grep '[a-z]\{7\}' *.txt
  

  输出:
  [root@learn ~]# grep '[a-z]\{7\}' *.txt
  test2.txt:abcdefghf
  test.txt:beatuful
  [root@learn ~]#
  

  实例12:显示包含ed或者at字符的内容行
  

  命令:
  grep -E "ed|at" test.txt
  

  输出:
  [root@learn ~]# grep -E "ed|at" test.txt
  beatuful
  redhat
  [root@learn ~]# grep -nE "ed|at" test.txt
  3:beatuful
  5:redhat
  说明:
  n给查出的行加上行号。
  

  实例13:不区分大小写查找内容
  命令:
  grep -i B test.txt
  

  输出
  [root@learn ~]# cat test.txt
  learn linux
  ABCDE
  aVIlin
  show
  beatuful
  abc
  redhat
  linux
  [root@learn ~]# grep -i B test.txt
  ABCDE
  beatuful
  abc
  [root@learn ~]# grep B test.txt
  ABCDE
  

  8,sed----stream editor for filtering and transforming text用于筛选和转换文本的流编辑器
  

  命令格式:
  sed [OPTION]... {script-only-if-no-other-script} [input-file]...
  

  参数:
  -n, --quiet, --silent   (取消默认输出)
  suppress automatic printing of pattern space
  -e script, --expression=script  (允许多点编辑)
  add the script to the commands to be executed
  -f script-file, --file=script-file
  add the contents of script-file to the commands to be executed将脚本文件的内容添加到要执行的命令
  --follow-symlinks
  follow  symlinks when processing in place; hard links will still be
  broken.
  -i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes  backup  if  extension  supplied).   The
  default  operation  mode is to break symbolic and hard links.  This
  can be changed with --follow-symlinks and --copy.
  -c, --copy
  use copy instead of rename when shuffling files in -i mode.   While
  this  will  avoid  breaking links (symbolic or hard), the resulting
  editing operation is not atomic.  This is rarely the desired  mode;
  --follow-symlinks is usually enough, and it is both faster and more
  secure.
  -l N, --line-length=N
  specify the desired line-wrap length for the ala command
  --posix
  disable all GNU extensions.
  -r, --regexp-extended
  use extended regular expressions in the script.
  -s, --separate
  consider files as separate rather than as a single continuous  long
  stream.
  -u, --unbuffered
  

  使用实例:
  

  实例1:取消默认输出。
  

  命令:
  sed -n /ab/p test.txt
  

  输出:
  [root@learn ~]# sed /ab/p test.txt
  learn linux
  ABCDE
  aVIlin
  show
  beatuful
  abc
  abc
  redhat
  linux
  [root@learn ~]# sed  -n /ab/p test.txt
  abc
  

  实例2:允许多点编辑 同时删除含ab,A行的内容。
  

  命令:
  sed -e /ab/d  -e /A/d test.txt
  

  输出:
  [root@learn ~]# cat test.txt
  learn linux
  ABCDE
  aVIlin
  show
  beatuful
  abc
  redhat
  linux
  [root@learn ~]# sed -e /ab/d  -e /A/d test.txt
  learn linux
  aVIlin
  show
  beatuful
  redhat
  linux
  

  实例3:替换内容 将abc替换为123456789
  

  命令:
  sed -i s#abc#123456789#g test.txt
  

  输出:
  [root@learn ~]# sed s#abc#123456789#g test.txt  (没有加-i参数时,只是临时更改打印出来)
  learn linux
  ABCDE
  aVIlin
  show
  beatuful
  123456789
  redhat
  linux
  [root@learn ~]# cat test.txt
  learn linux
  ABCDE
  aVIlin
  show
  beatuful
  abc           (abc还在,所以并没有替换)
  redhat
  linux
  [root@learn ~]# sed -i s#abc#123456789#g test.txt  (加-i后,直接更改文档内容)
  [root@learn ~]# cat test.txt
  learn linux
  ABCDE
  aVIlin
  show
  beatuful
  123456789
  redhat
  linux
  

  实例4:打印奇数行或偶数行
  

  命令:
  sed -n 'p;n' test.txt 或 sed -n '1~2p' test.txt 奇数
  sed -n 'n;p' test.txt 或 sed -n '2~2p' test.txt 偶数
  

  输出:
  [root@learn ~]# cat -n test.txt
  1  learn linux
  2  ABCDE
  3  aVIlin
  4  show
  5  beatuful
  6  abc
  7  abc
  8  redhat
  9  linux
  [root@learn ~]# sed -n 'p;n' test.txt
  learn linux
  aVIlin
  beatuful
  abc
  linux
  [root@learn ~]# sed -n 'n;p' test.txt
  ABCDE
  show
  abc
  redhat
  [root@learn ~]# sed -n '1~2p' test.txt
  learn linux
  aVIlin
  beatuful
  abc
  linux
  [root@learn ~]# sed -n '2~2p' test.txt
  ABCDE
  show
  abc
  redhat
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-463925-1-1.html 上篇帖子: linux _for循环语句 下篇帖子: Linux navite过期
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表