• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
linux重定向和管道 标准输入 输出 错误输出 tee xargs

 重定向

1,什么是重定向

 

 2,为什么要重定向

 

 

 

3,学习重定向的预备知识,标准输入与输出

 

 

 

 

 

 

[root@serv-test test123]# ll /dev/std*
lrwxrwxrwx 1 root root 15 6月   1 2021 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 6月   1 2021 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 6月   1 2021 /dev/stdout -> /proc/self/fd/1

文件描述符

 

 

 

案例1:标准输出重定向(每次都会覆盖文件)

 

 案例2:标准输出重定向(会往文件的尾部再添加内容)

 

 

 

 3,错误输出重定向

 

 

 

4,案例4:正确和错误都输入到相同的位置:

将标准输出和标准错误输出重定向到同一个文件,混合输出
find /etc -name "*.conf" &>ab
等同于
find /etc -name "*.conf" 1>ab 2>&1 

喝冰凉文件内容至一个文件
cat a b > c

 

 

 

 

 

 

 

 

 

 

 

案例5,在脚本中使用重定向

 

 

 

 echo $? 判断上条命令是否成功

[root@serv-test test123]# ping -c1 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.168 ms

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.168/0.168/0.168/0.000 ms
[root@serv-test test123]# ping -c1 192.168.1.1 &>/dev/null
[root@serv-test test123]# echo $?
0

 

 

 

2,进程管道技术

1,什么是管道

 

 2,管道流程示意图

 

 3,管道使用案例

 

 

 

 

 

 

 

 

 

3,管道中的tee技术

 

 

[root@serv-test test123]# tty
/dev/pts/8
[root@serv-test test123]# echo "$(mkpasswd)" | tee -a /dev/pts/8 | passwd --stdin test002
更改用户 test002 的密码 。
(aH3dJct8
passwd: 所有的身份验证令牌已经成功更新。

 

 

 

 

 重定向与tee 有什么区别

 

 

xargs参数传递,主要让一些不支持管道的命令可以使用管道技术

[root@serv-test test123]# which cat |xargs ls -l
-rwxr-xr-x 1 root root 48568 6月  19 2018 /bin/cat

案例:删除目录先除了65.txt的所有文件

touch {1...100}
ls |xargs -n1 |sed -r 's#(.*)#rm -f \1#g' | bash

 

 xargs -n 用法

[root@serv-test test]# ls |xargs -n1
1
10
100
11

[root@serv-test test]# ls |xargs -n3
1 10 100
11 12 13
14 15 16
17 18 19

[root@serv-test test]# ls |xargs
1 10 100 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 47 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 61 62 63 64 65 66 67 68 69 7 70 71 72 73 74 75 76 77 78 79 8 80 81 82 83 84 85 86 87 88 89 9 90 91 92 93 94 95 96 97 98 99

 

 

 

xargs 用作替换工具,读取输入数据重新格式化后输出。

定义一个测试文件,内有多行文本数据:

# cat test.txt

a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z

 

多行输入单行输出:

# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z

-d 选项可以自定义一个定界符:

# echo "nameXnameXnameXname" | xargs -dX

name name name name

 

xargs 的一个选项 -I,使用 -I 指定一个替换字符串 {},这个字符串在 xargs 扩展时会被替换掉,当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次:

# cat arg.txt | xargs -I {} ./sk.sh -p {} -l

-p aaa -l
-p bbb -l
-p ccc -l

复制所有图片文件到 /data/images 目录下:

ls *.jpg | xargs -n1 -I {} cp {} /data/images

xargs 结合 find 使用

用 rm 删除太多的文件时候,可能得到一个错误信息:/bin/rm Argument list too long. 用 xargs 去避免这个问题:

find . -type f -name "*.log" -print0 | xargs -0 rm -f

 

xargs -0 将 \0 作为定界符。

统计一个源代码目录中所有 php 文件的行数:

find . -type f -name "*.php" -print0 | xargs -0 wc -l

 

查找所有的 jpg 文件,并且压缩它们:

find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz

 

xargs 其他应用

假如你有一个文件包含了很多你希望下载的 URL,你能够使用 xargs下载所有链接:

# cat url-list.txt | xargs wget -c

 

本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/16514764.html

posted on 2022-07-24 16:31  孙龙-程序员  阅读(374)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3