Linux重定向

一、默认输出到屏幕

linux下的所有操作都是与文件相关的(stdout/stderr/stdin都是特殊的文件)

[root@localhost ~]# ll /dev/std*
lrwxrwxrwx  1 root root 15 Jul 25  2010 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx  1 root root 15 Jul 25  2010 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx  1 root root 15 Jul 25  2010 /dev/stdout -> /proc/self/fd/1

 0(stdin),1(stdout),2(stderr)。

1、stdout:默认是输出到屏幕上

[root@localhost ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

 

2、stderr:默认是输出到屏幕上

[root@localhost ~]# cat .bash_profileXXXXXX
cat: .bash_profileXXXXXX: No such file or directory

 

3、stdout、stderr同时输出

[root@localhost ~]# su - zhmg
-bash-3.00$ find /etc/ -name passwd
/etc/passwd
find: /etc/cups/certs: Permission denied
/etc/pam.d/passwd
find: /etc/Pegasus: Permission denied
find: /etc/racoon/certs: Permission denied

 

二、重定向到文件

>:导出且覆盖文件中已有的内容

>>:导出并追加到文件的尾部

<:导入

<<导入,直至遇到 delimiter 分界符

1、//将Standard output ,Standard error分别导入到不同的文件
[root@localhost ~]# su - zhmg
-bash-3.00$ find /etc/ -name passwd 2>error  1>output
-bash-3.00$
-bash-3.00$ cat error
find: /etc/cups/certs: Permission denied
find: /etc/Pegasus: Permission denied
find: /etc/racoon/certs: Permission denied
-bash-3.00$
-bash-3.00$ cat output
/etc/passwd
/etc/pam.d/passwd
-bash-3.00$

2、//将Standard output ,Standard error分别导入到一个文件
-bash-3.00$ find /etc/ -name passwd >allput.txt 2>&1
-bash-3.00$
-bash-3.00$ cat allput.txt
/etc/passwd
find: /etc/cups/certs: Permission denied
/etc/pam.d/passwd
find: /etc/Pegasus: Permission denied
find: /etc/racoon/certs: Permission denied
-bash-3.00$
-bash-3.00$ cat allput.txt
/etc/passwd
find: /etc/cups/certs: Permission denied
/etc/pam.d/passwd
find: /etc/Pegasus: Permission denied
find: /etc/racoon/certs: Permission denied
-bash-3.00$

3、//将Standard output ,Standard error分别导入到一个文件(&代表0、1、2,所以注意是不是需要0)
-bash-3.00$ find /etc/ -name passwd &>all.txt
-bash-3.00$
-bash-3.00$ cat all.txt
/etc/passwd
find: /etc/cups/certs: Permission denied
/etc/pam.d/passwd
find: /etc/Pegasus: Permission denied
find: /etc/racoon/certs: Permission denied
-bash-3.00$
-bash-3.00$ su root
Password:
[root@localhost zhmg]# cd
[root@localhost ~]# cat .bash_profile > test.txt
[root@localhost ~]# cat test.txt
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME
[root@localhost ~]#

4、//输入:将.bash_profile的"副本"的内容中的大写字母转化成小写
[root@localhost ~]# tr 'A-Z' 'a-z' < test.txt
# .bash_profile

# get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# user specific environment and startup programs

path=$path:$home/bin

export path
unset username

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值