一、默认输出到屏幕
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