Linux命令cat運用講解

來源:互聯網
上載者:User

你們知道什麼是Linux命令麼這個非常高深的運用技術將由我來非常講解,Linux命令NB在哪呢,下面我來進入講述Linux命令的無限領域。

Linux命令cat - concatenate files and print on the standard output把檔案串連後輸出到螢幕上,其實就是查看檔案內容。
SYNOPSIScat [OPTION] [FILEDESCRIPTIONConcatenate FILE(s), or standard input, to standard output.
-A,show-allequivalent to -vET --等同與參數-vET
-b,number-nonblanknumber nonblank output lines --對空白行不編號,與-n有相似。
-e,equivalent to -vE --等同與參數-vE
-E,show-ends display $ at end of each line --每行以$符號為行結尾
-n,number number all output lines --由1開始對所有輸出的行數編號
-s,squeeze-blank never more than one single blank line --當遇到連續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命令練習:
[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命令 我們可以驗證 -n與 -b的差異。
[root@CentOS4 ok_008]# cat -b -E mytest
1Hello world!$
2Hello world 1$
3Hello world 2$
4hello world 3$
每行以$符號為行結尾

現在想合并mytest和oktest兩個檔案的內容成一個新的檔案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的內容:[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.

合并兩個檔案的內容[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.

我們仔細會發現cat mytest oktest > newtest1.txt 和 cat mytest oktest >> newtest.txt 返回的結果在這裡相同。其實根據《Linux常用命令 全集》的例子:cat -n textfile1 > textfile2 把 textfile1 的檔案內容加上行號後輸入 textfile2 這個檔案裡cat -b textfile1 textfile2 >> textfile3 把 textfile1 和 textfile2 的檔案內容加上行號空白行不加)之後將內容附可以看出兩個Linux命令 的差異,這裡就不多寫例子。

Linux命令清空某個檔案的內容:
[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能清空檔案內容。

Linux命令建立一個新的檔案:
[root@CentOS4 ok_008]# cat >new.txt --按 CTRL + C 結束錄入
[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命令溫習以前的rmLinux命令 把一些剛才臨時建立的檔案(如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
今天就學到這裡,休息一下,呵呵。
以上介紹Linux命令應用。

  1. 一些高效的Linux命令列操作
  2. 菜鳥筆記:Linux命令大全
  3. 命令簡介:檔案和目錄管理的基本Linux命令
  4. 掌握cat學好Linux命令
  5. 簡單解說Linux命令輸出與命令替換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.