Linux命令cat運(yùn)用講解
你們知道什么是Linux命令么這個(gè)非常高深的運(yùn)用技術(shù)將由我來(lái)非常講解,Linux命令NB在哪呢,下面我來(lái)進(jìn)入講述Linux命令的無(wú)限領(lǐng)域。
Linux命令cat - concatenate files and print on the standard output把文件連接后輸出到屏幕上,其實(shí)就是查看文件內(nèi)容。
SYNOPSIScat [OPTION] [FILEDESCRIPTIONConcatenate FILE(s), or standard input, to standard output.
-A,show-allequivalent to -vET --等同與參數(shù)-vET
-b,number-nonblanknumber nonblank output lines --對(duì)空白行不編號(hào),與-n有相似。
-e,equivalent to -vE --等同與參數(shù)-vE
-E,show-ends display $ at end of each line --每行以$符號(hào)為行結(jié)尾
-n,number number all output lines --由1開(kāi)始對(duì)所有輸出的行數(shù)編號(hào)
-s,squeeze-blank never more than one single blank line --當(dāng)遇到連續(xù)2行以上的空白行,只保留一行空白行
-t,equivalent to -vT
-T,show-tabsdisplay TAB characters as ^I
-u,(ignored)
-v,show-nonprintinguse ^ and M- notation, except for LFD and TAB
Linux命令練習(xí):
[root@CentOS4 ok_008]# cat mytestHello world!
Hello world 1
Hello world 2
hello world 3
[root@CentOS4 ok_008]# cat -n mytest
Hello world!
Hello world 1
Hello world 2
hello world 3
[root@CentOS4 ok_008]# cat -b mytest
1Hello world!
2Hello world 1
3Hello world 2
4hello world 3從以上Linux命令 我們可以驗(yàn)證 -n與 -b的差異。
[root@CentOS4 ok_008]# cat -b -E mytest
1Hello world!$
2Hello world 1$
3Hello world 2$
4hello world 3$
每行以$符號(hào)為行結(jié)尾
現(xiàn)在想合并mytest和oktest兩個(gè)文件的內(nèi)容成一個(gè)新的文件NewTest.txt[root@CentOS4 ok_008]# ls -ltotal 24-rw-r--r-- 1 root root 59 Sep 18 19:48 mytestdrwxr-xr-x 3 root root 4096 Sep 13 22:51 ok-rw-r--r-- 1 root root 21 Sep 17 20:45 oktest[root@CentOS4 ok_008]# cat mytest oktest >> newtest.txt[root@CentOS4 ok_008]# ls -ltotal 32-rw-r--r-- 1 root root 59 Sep 18 19:48 mytest-rw-r--r-- 1 root root 80 Sep 18 19:53 newtest.txtdrwxr-xr-x 3 root root 4096 Sep 13 22:51 ok-rw-r--r-- 1 root root 21 Sep 17 20:45 oktest查看新的文件newtest.txt的內(nèi)容:[root@CentOS4 ok_008]# cat -b newtest.txt
1 Hello world!
2 Hello world 1
3 Hello world 2
4 hello world 3
5 this is a test file.
合并兩個(gè)文件的內(nèi)容[root@CentOS4 ok_008]# cat mytest oktest > newtest1.txt[root@CentOS4 ok_008]# ls -ltotal 40
-rw-r--r-- 1 root root 59 Sep 18 19:48 mytest
-rw-r--r-- 1 root root 80 Sep 18 19:56 newtest1.txt
-rw-r--r-- 1 root root 80 Sep 18 19:53 newtest.txt
drwxr-xr-x 3 root root 4096 Sep 13 22:51 ok
-rw-r--r-- 1 root root 21 Sep 17 20:45 oktest
[root@CentOS4 ok_008]# cat -b newtest1.txt
1 Hello world!
2 Hello world 1
3 Hello world 2
4 hello world 3
5 this is a test file.
我們仔細(xì)會(huì)發(fā)現(xiàn)cat mytest oktest > newtest1.txt 和 cat mytest oktest >> newtest.txt 返回的結(jié)果在這里相同。其實(shí)根據(jù)《Linux常用命令 全集》的例子:cat -n textfile1 > textfile2 把 textfile1 的檔案內(nèi)容加上行號(hào)后輸入 textfile2 這個(gè)檔案里cat -b textfile1 textfile2 >> textfile3 把 textfile1 和 textfile2 的檔案內(nèi)容加上行號(hào)(空白行不加)之后將內(nèi)容附可以看出兩個(gè)Linux命令 的差異,這里就不多寫(xiě)例子。
Linux命令清空某個(gè)文件的內(nèi)容:
[root@CentOS4 ok_008]# cat /dev/null > mytest
[root@CentOS4 ok_008]# cat -b mytest
[root@CentOS4 ok_008]# ls -l
total 52
-rw-r--r-- 1 root root 0 Sep 18 20:05 mytest
-rw-r--r-- 1 root root 80 Sep 18 19:56 newtest1.txt
-rw-r--r-- 1 root root 93 Sep 18 20:01 newtest3.txt
-rw-r--r-- 1 root root 93 Sep 18 20:01 newtest4.txt
-rw-r--r-- 1 root root 80 Sep 18 19:53 newtest.txt
drwxr-xr-x 3 root root 4096 Sep 13 22:51 ok
-rw-r--r-- 1 root root 34 Sep 18 20:00 oktest
--明天找一下資料為什么要使用cat /dev/null > mytest能清空文件內(nèi)容。
Linux命令創(chuàng)建一個(gè)新的文件:
[root@CentOS4 ok_008]# cat >new.txt --按 CTRL + C 結(jié)束錄入
[root@CentOS4 ok_008]# ls -l
total 56
-rw-r--r-- 1 root root 0 Sep 18 20:05 mytest
-rw-r--r-- 1 root root 80 Sep 18 19:56 newtest1.txt
-rw-r--r-- 1 root root 93 Sep 18 20:01 newtest3.txt
-rw-r--r-- 1 root root 93 Sep 18 20:01 newtest4.txt
-rw-r--r-- 1 root root 80 Sep 18 19:53 newtest.txt
-rw-r--r-- 1 root root 0 Sep 18 20:07 new.txt
drwxr-xr-x 3 root root 4096 Sep 13 22:51 ok
-rw-r--r-- 1 root root 34 Sep 18 20:00 oktest
Linux命令溫習(xí)以前的rmLinux命令 把一些剛才臨時(shí)建立的文件(如newtest1.txt,newtest3.txt,newtest4.txt,newtest.txt,new.txt)刪除掉:
[root@CentOS4 ok_008]# rm newtest1.txt newttest3.txt newtest4.txt newtest.txt
rm: remove regular file `newtest1.txt'? y
rm: cannot lstat `newttest3.txt': No such file or directory
rm: remove regular file `newtest4.txt'? y
rm: remove regular file `newtest.txt'? y
[root@CentOS4 ok_008]# rm newtest3.txt
rm: remove regular file `newtest3.txt'? y
[root@CentOS4 ok_008]# ls -l
total 24
-rw-r--r-- 1 root root 0 Sep 18 20:05 mytest
-rw-r--r-- 1 root root 0 Sep 18 20:07 new.txt
drwxr-xr-x 3 root root 4096 Sep 13 22:51 ok
-rw-r--r-- 1 root root 34 Sep 18 20:00 oktest
[root@CentOS4 ok_008]# rm newt.txt
rm: cannot lstat `newt.txt': No such file or directory
[root@CentOS4 ok_008]# rm new.txt
rm: remove regular empty file `new.txt'? y
[root@CentOS4 ok_008]# ls -l
total 20
-rw-r--r-- 1 root root 0 Sep 18 20:05 mytest
drwxr-xr-x 3 root root 4096 Sep 13 22:51 ok
-rw-r--r-- 1 root root 34 Sep 18 20:00 oktest
今天就學(xué)到這里,休息一下,呵呵。
以上介紹Linux命令應(yīng)用。
【編輯推薦】