ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第十四章 重定向和追加

2022-09-09 15:31:10  阅读:163  来源: 互联网

标签:00 重定向 pts qls 追加 第十四章 sbin txt root


重定向的概述


将原本要输出到屏幕上面的内容,重定向到一个指定的文件中 

将原本从键盘上面输入的内容,改为从命令或者文件当中读取 


为什么要用重定向 

1. 数据非常重要,需要保存  

2. 后台程序的输出重定向到一个文件中 

3. 定时任务的执行结果 

4. 把一些错误的输出定向到空  

5. 把正确和错误的信息都需要保存 



stdin		#标准输入		0		#从键盘上面读取输入的内容,或者从命令及文件中读取输入的内容 

stdout		#标准输出		1		#默认将正确的信息标准输出到屏幕上方  

stderr		#错误输出		2		#默认将错误的信息标准输出到屏幕上方 


[root@qls ~]# ll /dev/std*
lrwxrwxrwx 1 root root 15 Jul 20 19:44 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Jul 20 19:44 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Jul 20 19:44 /dev/stdout -> /proc/self/fd/1



[root@qls ~]# cat 
hello		#标准输入
hello		#标准输出



[root@qls ~]# tail  -f passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
^Z
[1]+  Stopped                 tail -f passwd
[root@qls ~]# ps
   PID TTY          TIME CMD
 10861 pts/0    00:00:00 bash
 10879 pts/0    00:00:00 bash
 10958 pts/0    00:00:00 su
 10980 pts/0    00:00:00 su
 10981 pts/0    00:00:00 bash
 10998 pts/0    00:00:00 su
 11020 pts/0    00:00:00 su
 11021 pts/0    00:00:00 bash
 11039 pts/0    00:00:00 su
 11040 pts/0    00:00:00 bash
 14928 pts/0    00:00:00 tail
 14930 pts/0    00:00:00 ps
[root@qls ~]# ll /proc/14928/fd
total 0
lrwx------ 1 root root 64 Jul 24 10:51 0 -> /dev/pts/0
lrwx------ 1 root root 64 Jul 24 10:51 1 -> /dev/pts/0
lrwx------ 1 root root 64 Jul 24 10:51 2 -> /dev/pts/0
lr-x------ 1 root root 64 Jul 24 10:51 3 -> /root/passwd
lr-x------ 1 root root 64 Jul 24 10:51 4 -> anon_inode:inotify


[root@qls ~]# jobs
[1]+  Stopped                 tail -f passwd
[root@qls ~]# fg %1
tail -f passwd
^C
[root@qls ~]# jobs
[root@qls ~]# ll /proc/14928/fd
ls: cannot access /proc/14928/fd: No such file or directory
[root@qls ~]# ps
   PID TTY          TIME CMD
 10861 pts/0    00:00:00 bash
 10879 pts/0    00:00:00 bash
 10958 pts/0    00:00:00 su
 10980 pts/0    00:00:00 su
 10981 pts/0    00:00:00 bash
 10998 pts/0    00:00:00 su
 11020 pts/0    00:00:00 su
 11021 pts/0    00:00:00 bash
 11039 pts/0    00:00:00 su
 11040 pts/0    00:00:00 bash
 14933 pts/0    00:00:00 ps

输出重定向


将原本要输出到屏幕上面的内容,重定向到一个指定的文件中 

符号


>	#标准覆盖正确输出重定向 		#将正确的内容覆盖源文件的   当文件不存在时,会自动创建 

>>	#标准追加正确输出重定向		#将正确的内容追加到指定文件的底部  当文件不存在时,会自动创建

2>	#标准覆盖错误输出重定向		#将错误的信息覆盖到指定的文件中 

2>>	#标准追加错误输出重定向		#将错误的信息追加到文件的底部  


案例:

[root@qls ~]# echo  "hello"  > 123.txt


[root@qls ~]# ip a s eth0 > ip.txt
[root@qls ~]# cat ip.txt
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

#合并文件
[root@qls ~]# cat /etc/hosts  /etc/resolv.conf   > new.txt
[root@qls ~]# cat new.txt
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
# Generated by NetworkManager
nameserver 223.5.5.5

[root@qls ~]# cat 123.txt 
hello
[root@qls ~]# echo "test"  >>  123.txt
[root@qls ~]# cat 123.txt
hello
test



[root@qls ~]# ls  /roott
ls: cannot access /roott: No such file or directory
[root@qls ~]# ls  /roott  2>err.txt
[root@qls ~]# cat err.txt
ls: cannot access /roott: No such file or directory



[root@qls ~]# ls  /roott  2>>err.txt
[root@qls ~]# ls  /roott  2>>err.txt
[root@qls ~]# ls  /roott  2>>err.txt
[root@qls ~]# cat err.txt
ls: cannot access /roott: No such file or directory
ls: cannot access /roott: No such file or directory
ls: cannot access /roott: No such file or directory
ls: cannot access /roott: No such file or directory



#把正确的和错误的信息都保存到一个文件中


[root@qls ~]# ls  /roott   > file.txt  2>&1 		#不推荐
[root@qls ~]# cat file.txt 
ls: cannot access /roott: No such file or directory
[root@qls ~]# mkdir  /roott
[root@qls ~]# ls  /roott   > file.txt  2>&1 
[root@qls ~]# cat file.txt 


[root@qls ~]# ls  /roott   &>file.txt
[root@qls ~]# ls  /roottt   &>file.txt
[root@qls ~]# cat file.txt
ls: cannot access /roottt: No such file or directory

#将正确的信息和错误的信息重定向到空

[root@qls ~]# ls  /roottt   &> /dev/null


#把正确的和错误的放在不同的文件中

[root@qls ~]# ls  /rooot  >> file1.txt  2>> file2.txt
[root@qls ~]# ll
total 4
-rw-r--r-- 1 root root  0 Jul 24 11:14 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
[root@qls ~]# cat file1.txt 
[root@qls ~]# cat file2.txt 
ls: cannot access /rooot: No such file or directory
[root@qls ~]# ls  /root  >> file1.txt  2>> file2.txt
[root@qls ~]# ll
total 8
-rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
[root@qls ~]# cat file1.txt 
file1.txt
file2.txt
[root@qls ~]# cat file2.txt 
ls: cannot access /rooot: No such file or directory

输入重定向


将原本从键盘上面输入的内容,改为从命令或者文件当中读取 

符号

<	#标准输入重定向 		#将原本从默认的键盘中读取数据改为由命令或者文件中读取 


<<	#标识符限定输入重定向 	#从键盘中读取内容,直到遇到标识符的分解符为止



案例:

[root@qls ~]# grep  'root'   /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# grep  'root'  <  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# grep  root
oldboy
root
root
rottttroot
rottttroot



[root@qls ~]# dd  if=/dev/zero  of=/root/test.log  bs=1M  count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.0362247 s, 1.4 GB/s
[root@qls ~]# ll
total 51208
-rw-r--r-- 1 root root       20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root       52 Jul 24 11:14 file2.txt
-rw-r--r-- 1 root root 52428800 Jul 24 11:45 test.log
[root@qls ~]# ll -h
total 51M
-rw-r--r-- 1 root root  20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root  52 Jul 24 11:14 file2.txt
-rw-r--r-- 1 root root 50M Jul 24 11:45 test.log


[root@qls ~]# dd  </dev/zero   >/root/oldboy.log  bs=10M  count=100
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB) copied, 21.3836 s, 49.0 MB/s
[root@qls ~]# ll -h
total 1.1G
-rw-r--r-- 1 root root    20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root    52 Jul 24 11:14 file2.txt
-rw-r--r-- 1 root root 1000M Jul 24 11:48 oldboy.log
-rw-r--r-- 1 root root   50M Jul 24 11:45 test.log

[root@qls ~]# cat file1.txt 
file1.txt
file2.txt
[root@qls ~]# tr  't' 'T'  < file1.txt
file1.TxT
file2.TxT




数据库导入表  

[root@qls ~]# mysql  -uroot  -p123  < all.sql


[root@qls ~]# cat >>file3.txt
123
345
678
456
EOF
123
^C
[root@qls ~]# cat file3.txt
123
345
678
456
EOF
123
[root@qls ~]# cat >>file4.txt<<EOF
> 123
> dfer
> rghrt
> gergrtg
> EOF
[root@qls ~]# cat file4.txt 
123
dfer
rghrt
gergrtg



[root@qls ~]# cat 123.sh
cat<<EOF
1. rge
2. fgo
3. jfrio
4. hfoih
EOF
[root@qls ~]# sh 123.sh
1. rge
2. fgo
3. jfrio
4. hfoih

管道技术


|	管道  	

连接左右两个命令的使用     把前面的命令作为标准输出通过管道交给后面的命令 作为标准输入 


只能把正确的信息交给后面的命令,错误的信息的不会传递 


[root@qls ~]# head passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# head passwd  | cat -n 
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin


[root@qls ~]# ifconfig  eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::3310:9d15:9ee4:43e8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:eb:ea:8d  txqueuelen 1000  (Ethernet)
        RX packets 99054  bytes 61039138 (58.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 55989  bytes 5452694 (5.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@qls ~]# ifconfig  eth0 | awk 'NR==2'
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
[root@qls ~]# ifconfig  eth0 | awk 'NR==2' | awk '{print $2}'
10.0.0.100


[root@qls ~]# awk  -F:  '{print $3}'  passwd  | sort -rn | head
1046
1045
1044
1043
1042
1041
1040
1039
1038
1037


#管道技术中的tee技术 

需要保存一些标准输出的内容 

[root@qls ~]# echo $RANDOM | md5sum | cut -c 1-8 | tee  pass.txt    |passwd  --stdin  qls01
Changing password for user qls01.
passwd: all authentication tokens updated successfully.
[root@qls ~]# cat pass.txt 
100ada15


-a		#追加 


tee 和 重定向的区别  



重定向		#把要输出到屏幕上面的内容重定向到指定的文件中


tee			#把要输出到屏幕上面的内容重定向到指定的文件中 ,并且可以继续输出一份到屏幕上面   



#管道中xargs技术 


让一些不支持管道的命令支持管道    把前面命令的执行结果以文件的参数传递方式传递给后面的命令 


[root@qls ~]# ls  | xargs  sed -i  's#root#oldboy#g'


[root@qls ~]# find  /var/log/  -type f -name "*.log"  |xargs  cp  -t  /opt


[root@qls ~]# find  /var/log/  -type f -name "*.log"  |xargs  -I  {}   cp  {}  /mnt/


标签:00,重定向,pts,qls,追加,第十四章,sbin,txt,root
来源: https://www.cnblogs.com/GAO321/p/16673011.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有