高级文件操作

  • 2019 年 10 月 3 日
  • 筆記

输入输出的介绍

标准输入(stdin) : 从该设配接受用户输入的数据

标准输出(stdout) : 通过该设备向用户输入数据

标准错误: 通过该设备报告执行出错信息


类型 设备文件 文件描述符 默认设备
标准输入 /dev/stdin 0 键盘
标准输出 /dev/stdout 1 显示器
标准错误输出 /dev/stderr 2 显示器

输入输出重定向

通过命令,改变了标准输入输出的方向就是重定向

类型 操作符 用途
重定向标准输出 > 将命令执行的结果,重定向输入的指定的文件中,而不再是显示屏
重定向标准输出 >> 将命令执行的结果追加到指定的文件中
重定向标准输入 < 将命令中接受到的路径,由默认的键盘,更改为指定的文件

例: 将abc.log的内容当成输入,重定向作为cat的输出

cat < abc.log

标准错误: 通过该设备报告执行出错信息

类型 操作符
重定向标准错误 2>
重定向标准错误 2>>
重定向标准输出 和 标准错误 2>&1 或者 >& 或 &>
重定向标准输出 和 标准错误 到不同的文件 >文件1 2>文件2

其中的2是标识符号

例: 将标准输出和标准错误重定向到不同的文件

ls a.txt aaaa > /dev/null 2>err.txt  如果目标文件是不存在的,会被自动创建出来

例: 将标准输出和标准错误重定向相同的文件

ls a.txt aaaa > /dev/null 2>&1  如果目标文件是不存在的,会被自动创建出来  1是可以省略的

两个特殊的文件

  • /dev/null 黑洞文件,传递进去的任何文件都会被当成垃圾丢弃
  • /dev/zero 用来创建指定长度的文件,不指定就是空文件

其他命令

echo

在屏幕上显示一段指定的文件或者内容

echo [-n] 字符串

-n: 不会进行换行

[root@ecs-t6-large-2-linux-20190824103606 ~]# echo 123  123  [root@ecs-t6-large-2-linux-20190824103606 ~]# echo -n 123  123[root@ecs-t6-large-2-linux-20190824103606 ~]#  

管道及相关配套命令

操作符

一条竖线 |

作用

连接左右两个命令,将左侧命令的标准输出,作为右侧命令的标准输入

有约束: 左侧命令要支持标准输出, 右侧命令会支持标准输入

格式:

cmd1 | cmd2 | cmd3  出         出入       入  cmd1的标准输出  被cmd2当成标准输入使用,  cmd2的标准输出被cmd3当成标准输入使用

例: 过滤出 /etc/下包含 pass的行

[root@ecs-t6-large-2-linux-20190824103606 ~]# ls -l /etc | grep pass  -rw-r--r--   1 root root   1092 Sep  8 20:32 passwd  -rw-r--r--.  1 root root   1135 Sep  8 20:29 passwd-  

xargs参数

用途: 让一些不支持管道的操作的命令行,可以使用管道

例1:

查看useradd命令所在的位置  [root@ecs-t6-large-2-linux-20190824103606 ~]# which useradd  /usr/sbin/useradd    管道的右边不支持标准输入, 所以它是在对当前目录使用 ls -lh  [root@ecs-t6-large-2-linux-20190824103606 ~]# which useradd | ls -lh  total 0    使用xargs 将左边的标准输入作为参数,添加的右侧的命令中  [root@ecs-t6-large-2-linux-20190824103606 ~]# which useradd |xargs ls -lh  -rwxr-x--- 1 root root 116K Mar 14 18:35 /usr/sbin/useradd

例2 可以实现分批删除:

find 200个文件 | args rm -f 

seq

作用: 打印出一串有序的数字

格式: seq [选项] [范围]

  • -s:指定分隔符
  • -w:指定同等宽度

例:

[root@ecs-t6-large-2-linux-20190824103606 ~]# seq 3  1  2  3    [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 2 3  2  3    [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 5 2 10  5  7  9    [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 1 -1 10  [root@ecs-t6-large-2-linux-20190824103606 ~]#    死循环  [root@ecs-t6-large-2-linux-20190824103606 ~]# seq 1 0 10  [root@ecs-t6-large-2-linux-20190824103606 ~]#    [root@ecs-t6-large-2-linux-20190824103606 ~]# seq -s XXX 1 10  1XXX2XXX3XXX4XXX5XXX6XXX7XXX8XXX9XXX10    [root@ecs-t6-large-2-linux-20190824103606 ~]# seq -w 90 100  090  091  092  093  094  095  096  097  098  099  100  

tr 转换/删除/压缩

  • 字符转换工具
    不能直接对文件进行操作
    命令: tr set1 set2

作用: 用set2中的字符替换掉set1中相同的字符

echo 123456 | tr 345 abc  将左边标准输出的3 转换为 a , 4->b ,  5->c   只要相同, 一一转换      将 /etc/hosts的内容转大写  tr ‘[a-z]’ ‘[A-Z]’ < /etc/hosts  将A-Z 转换成a-z
  • 使用tr 删除字符

格式 tr -d set
删除和set相同的字符

 [root@ecs-t6-large-2-linux-20190824103606 ~]# echo 123456 | tr -d 123  456
  • 压缩

将连续相同的字符压缩成一个字符

echo 112233444555666 | tr -s 345  碰到连续的3, 就压缩成1个3  碰到连续的4, 就压缩成1个4    tr  -s SET1 SET2  先替换为SET2再压缩  echo 112233444555666 | tr -s 345 abc  echo 112233444555666 | tr 345 abc | tr -s abc

排序sort

默认会按照每一行的第一个字符进行排序

  • -n: numeric sort 按整数排序
  • -r: reverse 递减排序
  • -k: key 指定某一列为排序键
  • -t: field-separator 指定字段分隔符

例:

[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat sort_text  dsd  asd  sad  asa  das  da1  da3  d23  123  316  164  a45  d34  456  234  444  d64  as6  da4  d6a  asd      [root@ecs-t6-large-2-linux-20190824103606 tmp]# cat sort_text | sort -n  a45  as6  asa  asd  asd  d23  d34  d64  d6a  da1  da3  da4  das  dsd  sad  123  164  234  316  444  456    [root@ecs-t6-large-2-linux-20190824103606 tmp]# cat sort_text | sort -n -k2  123  164  234  316  444  456  a45  as6  asa  asd  asd  d23  d34  d64  d6a  da1  da3  da4  das  dsd  sad  

例2: 按照表格的方式展示 /etc/passwd 的内容

sort  /etc/passwd  | column -t -s ":"  [root@ecs-t6-large-2-linux-20190824103606 tmp]# sort  /etc/passwd  | column -t -s ":"  adm                x  3    4    adm                                                              /var/adm            /sbin/nologin  bin                x  1    1    bin                                                              /bin                /sbin/nologin  daemon             x  2    2    daemon                                                           /sbin               /sbin/nologin  dbus               x  81   81   System message bus                                               /                   /sbin/nologin  ....

uniq命令

删除重复的记录,通常和sort连用

它只会去除连续出现的相同的记录,针对如下记录的结果操作如下  123  123123  123123  123  123        [root@ecs-t6-large-2-linux-20190824103606 tmp]# cat text | uniq  123  123123  123    先排序,再去重的效果如下  [root@ecs-t6-large-2-linux-20190824103606 tmp]# sort -r text | uniq  123123  123    
  • -c: count 显示文件中连续出现的次数
[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat text | uniq -c        1 123      122 123123        1 123
  • -u:unique 只显示不重复的行
    cat tt | uniq –u
  • -d:repead 只显示重复的行
    cat tt | uniq -d

wc 命令

wc(字数统计)命令
格式:wc [选项]… 目标文件…

  • -l:lines 统计行数
  • -w:words 统计字数 (前后都是空白的一组字符)
  • -c:bytes 统计字符数(可见和不可见的字符)

例1:

[root@ecs-t6-large-2-linux-20190824103606 ~]# wc /etc/passwd    23   44 1092 /etc/passwd  23行  44字 1092字节  

例2:

[root@ecs-t6-large-2-linux-20190824103606 ~]# echo 123 | wc -l  1

cut命令

从指定的文本或者文本流中提取指定的列

格式: cut [可选项] 范围 文本/文本流

可选项

  • -c: 从指定位置提取
  • -f: fields 仅仅打印指定的列
  • -d: delimiter 指定分隔符, 默认是 tab

提取范围

  • n: 第n列
  • n-: 从n到列尾
  • -m: 从开头到m
  • n,m:第n和第m项
  • n-m: 从n到m项

例1:

[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll  total 40  drwxr-xr-x 2 root root  4096 Sep  4 19:35 hsperfdata_root  drwxr-xr-x 2 root root  4096 Sep  4 19:35 jetty-0.0.0.0-9998-browser-_browser-any-6157528924019481141.dir  -rw-r--r-- 1 root root    84 Sep  9 21:46 sort_text  drwx------ 3 root root  4096 Sep  4 15:28 systemd-private-c3ac023caef0453f85963d47758cc2cf-ntpd.service-LXVmNS  -rw-r--r-- 1 root root   862 Sep  9 21:54 text  drwx------ 2 root root  4096 Sep  4 20:22 tmp.2RTupK8p7R  prw-r--r-- 1 root root     0 Sep 10 08:53 wrapper-4156-1-in  prw-r--r-- 1 root root     0 Sep 10 08:53 wrapper-4156-1-out  -rw------- 1 root root 13478 Sep  4 20:16 yum_save_tx.2019-09-04.20-16.5iMOEY.yumtx    提取第十列  [root@ecs-t6-large-2-linux-20190824103606 tmp]# ll | cut -c 10    x  x  -  -  -  -  -  -  -    提取20-40 列  [root@ecs-t6-large-2-linux-20190824103606 tmp]# who | cut -c 20-40     2019-09-10 08:46 (  

数据的提取和过滤

grep 数据提取程序

用途: 在文件中查找查找并显示包含指定字符串的行

格式: grep [选项] 模式 目标文件

  • -i: ignore case 查找时忽略大小写
  • -v: invert match 反转查找,输出和模式不相符的行
  • -w: word regexp 按整字查找, 数字,字母,下划线 连在一起就是整字
  • -n: line number 显示符合模式要求的行号
  • -r: 递归查找所有文件
  • -o: 仅仅输出匹配到的字符

模式

a   :包含a的行  ^...: 以...开头  ...$: 以...结尾

例: 统计文件中某个字的数量

grep -o "abc" abc.txt | wc -l

文件的差异对比

比较两个文件之间的差异

输出结果为两个文件的不同之处

diff [文件1] [文件2]

没有任何输出说明文件不一样