Linux重定向

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

 

### Linux 重定向的使用方法与进阶技巧 #### 基本概念 在 Linux Shell 中,`>` 和 `>>` 是常用的重定向操作符。这些符号用于将命令的标准输出(stdout)重新导向到文件或其他目标位置[^1]。 - **覆盖写入 (`>`)** 当使用单箭头符号时,它会将指定的内容完全覆盖原文件内容并保存新数据。 ```bash echo "这是新的内容" > 文件名.txt ``` - **追加写入 (>>)** 双箭头符号则会在不删除已有内容的情况下,在文件末尾添加新内容。 ```bash echo "附加的新行" >> 文件名.txt ``` #### 高级应用实例 ##### 示例一:文件描述符的操作 通过执行特定指令来管理文件描述符(File Descriptor, FD),可以实现更复杂的输入/输出控制。例如: ```bash echo 1234567890 > File exec 3<> File # 打开FD 3读写模式连接至'File' read -n 4 <&3 # 从前四个字符中读取部分数据并通过FD 3传递给标准输出 echo -n '.' >&3 # 向文件中间插入额外字符串'.' exec 3>&- # 关闭自定义打开的FD 3 cat File # 查看最终修改后的整个文件内容 ``` 上述脚本展示了如何利用非默认文件描述符完成精确的数据片段处理过程[^3]。 ##### 示例二:批量替换文本中的固定值 对于大量源码或者其他纯文本形式文档内的数值更新需求场景,可借助sed命令快速达成目的: ```bash find . -name "*.c" | xargs sed -i 's/8080/88888/g' ``` 此命令能够遍历当前路径下的所有`.c`扩展名文件并将其中所有的数字序列`8080`替换成`88888`[^4]。 #### WSL环境下实践建议 鉴于越来越多开发人员倾向于采用Windows Subsystem for Linux(WSL),因为它提供了高效便捷的方式构建跨平台软件解决方案的同时保持较低资源消耗特点[^2]。因此推荐尝试以上提到的各种shell特性于最新版WSL发行版本之上测试运行效果最佳。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值