Linux系统之cat命令
来源:
- http://blog.csdn.net/jackalfly/article/details/7556848
- http://man.linuxde.net/cat
- http://blog.csdn.net/pi9nc/article/details/18257593
cat命令
1、滚屏显示文件,大文件会滚的很快,Ctrl+S暂停滚屏,Ctrl+Q继续滚屏,Ctrl+C中断滚屏。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat 1.sh 2 #!/bin/sh 3 #author: 4 #date: 5 #describe: 6 #modify: 7 #versions: 8 # 9 echo 'Hello,shell!' 10 [super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat 1.sh 2.sh 2 #!/bin/sh 3 #author: 4 #date: 5 #describe: 6 #modify: 7 #versions: 8 # 9 echo 'Hello,shell!' 10 #!/bin/sh 11 set -x 12 echo -n "The system is Linux?" 13 echo answer Y or N. 14 read answer 15 # 16 if [ -f $answer ] 17 then 18 echo "No input anything." 19 exit 20 fi 21 if [ $answer = Y ] 22 then 23 echo "Yes,This is Linux System." 24 else 25 echo "No,This is Windows System." 26 fi 27 [super@SuPer ~]$
2、创建一个新的文件,需要注意会进入文件编辑状态,此时的输入会分行保存,每按一次Enter键保存一次,Ctrl+C结束当前任务。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat > 1.txt 2 AAAA 3 SSSS 4 DDDD^C 5 [super@SuPer ~]$ cat 1.txt 6 AAAA 7 SSSS 8 [super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat << 1 > 2.txt 2 > BBBB 3 > DDDD 4 > 1 5 [super@SuPer ~]$ cat 2.txt 6 BBBB 7 DDDD 8 [super@SuPer ~]$
- EOF:end of file,表示文本结束符,可以用任何字符代替,编辑时在一行中只有该字符并回车保存则结束文本编辑。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat > 1.txt << EOF 2 > AAAA 3 > BBB 4 > EOF 5 [super@SuPer ~]$ cat 1.txt 6 AAAA 7 BBB 8 [super@SuPer ~]$ cat > 1.txt << EOF 9 > CCCC 10 > DDD 11 > EOF 12 [super@SuPer ~]$ cat 1.txt 13 CCCC 14 DDD 15 [super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat > 1.txt << EOF 2 > sss 3 > sss 4 > EOF 5 [super@SuPer ~]$ cat 1.txt 6 sss 7 sss 8 [super@SuPer ~]$ cat >> 1.txt << EOF 9 > aaa 10 > vvv 11 > EOF 12 [super@SuPer ~]$ cat 1.txt 13 sss 14 sss 15 aaa 16 vvv 17 [super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat 1.txt 2 sss 3 sss 4 aaa 5 vvv 6 [super@SuPer ~]$ cat >> 1.txt 7 s^Hdsds 8 dsd[super@SuPer ~]$ cat 1.txt 9 sss 10 sss 11 aaa 12 vvv 13 dsds 14 dsd[super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat 1.txt 2 sss 3 sss 4 aaa 5 vvv 6 dsds 7 dsdDDD 8 [super@SuPer ~]$ cat >> 1.txt 9 EEE 10 [super@SuPer ~]$ cat 1.txt 11 sss 12 sss 13 aaa 14 vvv 15 dsds 16 dsdDDD 17 EEE 18 [super@SuPer ~]$
3、文件合并
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat > 1.txt 2 AAA 3 [super@SuPer ~]$ cat > 2.txt 4 BBB 5 [super@SuPer ~]$ cat 1.txt 6 AAA 7 [super@SuPer ~]$ cat 2.txt 8 BBB 9 [super@SuPer ~]$ cat -n 1.txt >> 2.txt 10 [super@SuPer ~]$ cat 1.txt 11 AAA 12 [super@SuPer ~]$ cat 2.txt 13 BBB 14 1 AAA 15 [super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat 1.txt 2 AAA 3 [super@SuPer ~]$ cat 2.txt 4 BBB 5 1 AAA 6 [super@SuPer ~]$ cat 1.txt 2.txt > 3.txt 7 [super@SuPer ~]$ cat 3.txt 8 AAA 9 BBB 10 1 AAA 11 [super@SuPer ~]$
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer ~]$ cat 3.txt 2 AAA 3 BBB 4 1 AAA 5 [super@SuPer ~]$ cat /dev/null > 3.txt 6 [super@SuPer ~]$ cat 3.txt 7 [super@SuPer ~]$
- /dev/null:在类UNIX系统中,称空设备,是一个特殊的设备文件,俗称位桶(bit bucket)或黑洞(black hole)。它丢弃一切写入其中的数据(但报告写入操作成功),读取它则会立即得到一个EOF。常用作丢弃不需要的输出流,或作为用于输入流的空文件,通常由重定向完成。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [super@SuPer txt]$ ll 2 总用量 4 3 -rw-rw-r-- 1 super super 0 10月 8 09:58 1.txt 4 -rw-rw-r-- 1 super super 15 10月 8 09:40 2.txt 5 -rw-rw-r-- 1 super super 0 10月 8 09:43 3.txt 6 [super@SuPer txt]$ cat 4.txt 1>/dev/null 7 cat: 4.txt: 没有那个文件或目录 8 [super@SuPer txt]$ cat 4.txt 2>/dev/null 9 [super@SuPer txt]$
- 默认重定向的1就是标准输出,2是标准错误输出。使用2可以做到只判断该命令是否运行正常。
【注】目前没有发现cat编辑下有修改文本的功能,还需要使用vi再编辑。