linux---数据流重定向

本文深入探讨了Linux系统中标准输入、输出与错误输出的重定向技巧,包括使用'>'、'>>'和'<'进行文件内容的覆盖、追加及输入替换。通过实例演示了如何利用这些命令高效管理文件内容,并介绍了如何使用'<<'进行批处理输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 标准输入(stdin): 代码为0, 使用<或者<<
  • 标准输出(stdout): 代码为1, 使用>(覆盖)或者>>(累加)
  • 标准错误输出(stderr): 代码为2,使用2>或者2>>
#列出根目录下的各目录和文件
mali@mali:~$ ll /
total 108
drwxr-xr-x  24 root root  4096 7月  14 20:42 ./
drwxr-xr-x  24 root root  4096 7月  14 20:42 ../
drwxr-xr-x   2 root root  4096 7月  21 13:33 bin/
drwxr-xr-x   3 root root  4096 7月  21 13:48 boot/
drwxrwxr-x   2 root root  4096 7月  14 20:06 cdrom/
drwxr-xr-x  18 root root  3920 7月  29 06:47 dev/
drwxr-xr-x 131 root root 12288 7月  29 06:46 etc/
drwxr-xr-x   3 root root  4096 7月  14 20:37 home/
lrwxrwxrwx   1 root root    33 7月  14 20:42 initrd.img -> boot/initrd.img-4.10.0-28-generic
drwxr-xr-x  22 root root  4096 7月  14 20:43 lib/
drwxr-xr-x   2 root root  4096 7月  21 13:29 lib64/
drwx------   2 root root 16384 7月  14 20:03 lost+found/
drwxr-xr-x   3 root root  4096 7月  14 20:48 media/
drwxr-xr-x   2 root root  4096 8月   1  2017 mnt/
drwxr-xr-x   3 root root  4096 7月  14 20:48 opt/
dr-xr-xr-x 179 root root     0 7月  29 06:46 proc/
drwx------   3 root root  4096 7月  15 19:03 root/
drwxr-xr-x  26 root root   820 7月  29 06:51 run/
drwxr-xr-x   2 root root 12288 7月  21 13:34 sbin/
drwxr-xr-x   2 root root  4096 7月  21 13:45 snap/
drwxr-xr-x   2 root root  4096 8月   1  2017 srv/
dr-xr-xr-x  13 root root     0 7月  29 22:39 sys/
drwxrwxrwt  10 root root  4096 7月  29 22:40 tmp/
drwxr-xr-x  11 root root  4096 8月   1  2017 usr/
drwxr-xr-x  14 root root  4096 8月   1  2017 var/
lrwxrwxrwx   1 root root    30 7月  14 20:42 vmlinuz -> boot/vmlinuz-4.10.0-28-generic

#ll /的输出的数据被导向到~/testfile中
mali@mali:~$ ll / > ~/testfile
mali@mali:~$ ll ~/testfile 
-rw-rw-r-- 1 mali mali 1432 7月  29 22:44 /home/mali/testfile
mali@mali:~$ cat ~/testfile 
total 108
drwxr-xr-x  24 root root  4096 7月  14 20:42 ./
drwxr-xr-x  24 root root  4096 7月  14 20:42 ../
drwxr-xr-x   2 root root  4096 7月  21 13:33 bin/
drwxr-xr-x   3 root root  4096 7月  21 13:48 boot/
drwxrwxr-x   2 root root  4096 7月  14 20:06 cdrom/
drwxr-xr-x  18 root root  3920 7月  29 06:47 dev/
drwxr-xr-x 131 root root 12288 7月  29 06:46 etc/
drwxr-xr-x   3 root root  4096 7月  14 20:37 home/
lrwxrwxrwx   1 root root    33 7月  14 20:42 initrd.img -> boot/initrd.img-4.10.0-28-generic
drwxr-xr-x  22 root root  4096 7月  14 20:43 lib/
drwxr-xr-x   2 root root  4096 7月  21 13:29 lib64/
drwx------   2 root root 16384 7月  14 20:03 lost+found/
drwxr-xr-x   3 root root  4096 7月  14 20:48 media/
drwxr-xr-x   2 root root  4096 8月   1  2017 mnt/
drwxr-xr-x   3 root root  4096 7月  14 20:48 opt/
dr-xr-xr-x 179 root root     0 7月  29 06:46 proc/
drwx------   3 root root  4096 7月  15 19:03 root/
drwxr-xr-x  26 root root   820 7月  29 06:51 run/
drwxr-xr-x   2 root root 12288 7月  21 13:34 sbin/
drwxr-xr-x   2 root root  4096 7月  21 13:45 snap/
drwxr-xr-x   2 root root  4096 8月   1  2017 srv/
dr-xr-xr-x  13 root root     0 7月  29 22:39 sys/
drwxrwxrwt  10 root root  4096 7月  29 22:40 tmp/
drwxr-xr-x  11 root root  4096 8月   1  2017 usr/
drwxr-xr-x  14 root root  4096 8月   1  2017 var/
lrwxrwxrwx   1 root root    30 7月  14 20:42 vmlinuz -> boot/vmlinuz-4.10.0-28-generic

#使用>重定向,若文件已存在,则文件之前的内容被清空
mali@mali:~$ ll /home > ~/testfile
mali@mali:~$ cat ~/testfile 
total 12
drwxr-xr-x  3 root root 4096 7月  14 20:37 ./
drwxr-xr-x 24 root root 4096 7月  14 20:42 ../
drwxr-xr-x 18 mali mali 4096 7月  29 22:44 mali/

#使用>>,若文件已存在,新的数据会追加到原文件数据后面
mali@mali:~$ ll /usr >> ~/testfile
mali@mali:~$ cat ~/testfile 
total 12
drwxr-xr-x  3 root root 4096 7月  14 20:37 ./
drwxr-xr-x 24 root root 4096 7月  14 20:42 ../
drwxr-xr-x 18 mali mali 4096 7月  29 22:44 mali/
total 132
drwxr-xr-x  11 root root  4096 8月   1  2017 ./
drwxr-xr-x  24 root root  4096 7月  14 20:42 ../
drwxr-xr-x   2 root root 61440 7月  21 22:30 bin/
drwxr-xr-x   2 root root  4096 8月   1  2017 games/
drwxr-xr-x  35 root root 16384 7月  21 13:33 include/
drwxr-xr-x 138 root root  4096 7月  21 22:30 lib/
drwxr-xr-x  10 root root  4096 8月   1  2017 local/
drwxr-xr-x   3 root root  4096 8月   1  2017 locale/
drwxr-xr-x   2 root root 12288 7月  21 22:30 sbin/
drwxr-xr-x 288 root root 12288 7月  21 22:30 share/
drwxr-xr-x   4 root root  4096 7月  14 20:48 src/
#利用cat命令创建文件catfile1
mali@mali:~$ cat > catfile1
hello world #从键盘输入,ctrl+d结束
mali@mali:~$ cat catfile1
hello world
#用文件catfile1的内容替代键盘输入
mali@mali:~$ cat > catfile2 < catfile1
mali@mali:~$ cat catfile2
hello world #内容和catfile1相同

#<<右侧的控制字符,可以终止一次输入,这样就不必输入ctrl+d来结束
mali@mali:~$ cat > catfile << "eof"
> this is a test
> ok now stop
> eof
mali@mali:~$ cat catfile
this is a test
ok now stop
mali@mali:~$ 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值