Linux 重定向的使用

38 阅读1分钟

简介

在日常的Linux使用过程中,很多时候都需要用到输入/输出重定向的功能,方便我们灵活的使用命令和操作文件等。

示例

输出重定向到文件

[xiaofeng@localhost tmp]$ echo "abc" > testfile
[xiaofeng@localhost tmp]$ cat testfile
abc

输出重定向到文件(追加)

[xiaofeng@localhost tmp]$ echo "123" >> testfile
[xiaofeng@localhost tmp]$ cat testfile
abc
123

输入重定向(从文件)

[xiaofeng@localhost tmp]$ cat < testfile
abc
123
[xiaofeng@localhost tmp]$ wc -l < testfile
2

输入重定向(多行)

[xiaofeng@localhost tmp]$ cat << EOF
> hello
> linux
> EOF
hello
linux
[xiaofeng@localhost tmp]$

标准错误重定向(stderr)

[xiaofeng@localhost tmp]$ ssb > testfile 2>&1
[xiaofeng@localhost tmp]$ cat testfile
-bash: ssb: command not found

/dev/null重定向(不显示)

[xiaofeng@localhost tmp]$ cat testfile
-bash: ssb: command not found
[xiaofeng@localhost tmp]$
[xiaofeng@localhost tmp]$ cat testfile > /dev/null
[xiaofeng@localhost tmp]$