Linux中的重定向

一、重定向符号:> >> 1> 1>> 2> 2>> < <<

1.标准输出重定向: >与1>

[root@NSW ~]# ls
anaconda-ks.cfg install.log.syslog
[root@NSW ~]# echo My name is NSW>nsw.txt
[root@NSW ~]# cat nsw.txt
My name is NSW
//新建nsw.txt,并将>左边的字符写入到文件中。目录下无文件,自动创建文件。

1>
[root@NSW ~]# echo My name is NSW 1>test.txt
[root@NSW ~]# cat test.txt
My name is NSW
//同理。

[root@NSW ~]# cat nsw.txt
My name is NSW
[root@NSW ~]# echo I love oldboy >nsw.txt
[root@NSW ~]# cat nsw.txt
I love oldboy
//可以看出>会将源文件的内容覆盖。(谨慎)
[root@NSW ~]# echo Welcome 1>nsw.txt
[root@NSW ~]# cat nsw.txt
Welcome
//1>同样也会先覆盖原文件的内容。

小结:重定向符>和1>都为输出重定向;
被输入的文件有则直接输入,无则自动创建该文件并输入;
将符号左边的字符串输入到右边文件中且覆盖原文件。

2.追加输出重定向:>> 1>>
[root@NSW ~]# ls
anaconda-ks.cfg install.log.syslog nsw.txt test.txt
[root@NSW ~]# cat nsw.txt
Welcome
[root@NSW ~]# echo My name is nsw>>nsw.txt
[root@NSW ~]# cat nsw.txt
Welcome
My name is nsw
//可以看出确实在云文件最后面新追加了My name is nsw没有覆盖源文件。

[root@NSW ~]# echo My name is nsw1>>nsw.txt
[root@NSW ~]# cat nsw.txt
Welcome
My name is nsw
My name is nsw1
//这需要注意的是符号左面需要空一格要不系统会认为1是输入的字符。

[root@NSW ~]# echo My name is nsw 1>>nsw.txt
[root@NSW ~]# cat nsw.txt
Welcome
My name is nsw
My name is nsw1
My name is nsw
//同理1>>和>>作用相同。

[root@NSW ~]# ls
anaconda-ks.cfg install.log.syslog nsw.txt test.txt
[root@NSW ~]# echo Hello >>hello.txt
[root@NSW ~]# cat hello.txt
Hello
[root@NSW ~]# cat HELLO.txt
HELLO

小结:追加输出重定向>>和1>>,将内容追加到指定文件中最后一行的下一行,不会覆盖源文件;
指定目录下有源文件直接追加,没有则自定创建文件并追加内容。

3.标准错误重定向2>
[root@NSW ~]# cat hello.txt
Hello
[root@NSW ~]# eho test
-bash: eho: command not found
[root@NSW ~]# eho test 2>hello.txt
[root@NSW ~]# cat hello.txt
-bash: eho: command not found
//将错误重定向到指定文件中并覆盖原文件内容。

4.错误追加重定向2>>
[root@NSW ~]# cat test.txt
My name is NSW
[root@NSW ~]# eho Hello 2>> test.txt
[root@NSW ~]# cat test.txt
My name is NSW
-bash: eho: command not found
//将错误追加到指定文件中。

[root@NSW ~]# ls
anaconda-ks.cfg  install.log.syslog
[root@NSW ~]# eho hello 2>test.txt
[root@NSW ~]# cat test.txt 
-bash: eho: command not found
[root@NSW ~]# 
[root@NSW ~]# 
[root@NSW ~]# eho hello 2>>test.txt
[root@NSW ~]# cat test.txt 

-bash: eho: command not found
-bash: eho: command not found
//无论2>还是2>>在目录下没有指定文件时都会自动创建文件。

小结:标准错误输出重定向和错误追加重定向都是将执行错误的提示内容放入指定文件中,故障排查,区别在于一个是覆盖一个是追加。

5.标准输入重定向:<
[root@NSW ~]# echo 1 2 3 4 5 6 >test.txt
[root@NSW ~]# cat test.txt
1 2 3 4 5 6
[root@NSW ~]# xargs -n2 test.txt
^C
[root@NSW ~]# xargs -n2 <test.txt
1 2
3 4
5 6
//用来将<右侧文件中的内容输出给左侧的命令,可以使用<符号的命令很少,xargs为其中之一。

6.追加输入重定向:<<
[root@NSW ~]# ls
anaconda-ks.cfg install.log.syslog test.txt
[root@NSW ~]# cat >>nsw.txt<<EOF

Hello!
My name is NSW
EOF
[root@NSW ~]# cat nsw.txt
Hello!
My name is NSW
//将多行内容追加输入到指定文件中,指定目录下没有文件则新建。

小结:<是用来将符号右侧文本的内容作为左侧命令的执行条件;
<<是方便输入多行内容到指定文件中。

二、查看进程
通过命令ps

[root@NSW ~]# ps
PID TTY TIME CMD
2297 pts/0 00:00:00 bash
3444 pts/0 00:00:00 ps
每次键入ps命令都会显示bash和ps两进程,应该是当前系统调用bash内核中的ps命令,所以当前有两进程在运行。

[root@NSW ~]# ps -e //显示系统当前所有进程
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 kthreadd
3 ? 00:00:00 migration/0
4 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 stopper/0
6 ? 00:00:00 watchdog/0
7 ? 00:00:10 events/0
8 ? 00:00:00 events/0
9 ? 00:00:00 events_long/0
10 ? 00:00:00 events_power_ef
...

三、查看端口
通过命令netstat

举例查看TCP传输协议的连接状况

[root@NSW ~]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.29.130:ssh 192.168.29.1:55448
//系统当前只有ssh服务使用了tcp传输协议连接。

如果文中有错误的地方欢迎大家指出,谢谢!

转载于:https://blog.51cto.com/13474721/2299465

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值