Linux基礎知識--文件內容操作命令
文件內容操作命令
cat命令:
用途:用途顯示文件的全部內容,也可以同時顯示多個文件的內容。
語法:cat 文件名
more命令和less命令
用途:全屏方式分頁顯示內容
head、tail命令
用途與格式:
head是查看文件開頭的一部分(默認10行)。格式為head -n 文件名
tail是查看文件末尾的一部分(默認10行),格式為:tail -n 文件名 tail -f 文件名
wc命令:
用途:統計文件中單詞的數量(Word count)等信息
格式:wc [選項]... 目標文件...
-l 統計行數
-w 統計單詞個數
-c 統計字節數
[root@localhost home]# cat file i am bigbigtong [root@localhost home]# wc file 1 3 16 file [root@localhost home]# wc -l file 1 file [root@localhost home]# wc -w file 3 file [root@localhost home]# wc -c file 16 file
可以看到file文件中有1行,3個單詞,占16個字節
grep命令:
用途:在文件中查找並顯示包含某字符串的行
格式:grep [選項]... 查找條件 目標文件
-i 查找時忽略大小寫
-v 反轉查找,輸出與查找條件不符的行
查找條件設置:
要查找的字符串用雙引號括起來
“^……”表示以……開頭,“……$”表示以……結尾
“^$”表示空行
[root@localhost home]# cp /etc/audisp/audispd.conf /home [root@localhost home]# ls audispd.conf file file1 file2 test1 test2 [root@localhost home]#cat audispd.conf # # This file controls the configuration of the audit event # dispatcher daemon, audispd. # q_depth = 64 overflow_action = SYSLOG name_format = HOSTNAME #name = mydomain [root@localhost home]# grep "name" audispd.conf name_format = HOSTNAME #name = mydomain
audispd.cof文件中查找包含"name"的行,結果就是這兩行
[root@localhost home]# grep -v "name" audispd.conf # # This file controls the configuration of the audit event # dispatcher daemon, audispd. # q_depth = 64 overflow_action = SYSLOG
查找不包含“name”的行
[root@localhost home]# grep -i "this" audispd.conf # This file controls the configuration of the audit event
忽略大小寫
[root@localhost home]# grep "^#" audispd.conf # # This file controls the configuration of the audit event # dispatcher daemon, audispd. # #name = mydomain [root@localhost home]# grep "main$" audispd.conf #name = mydomain [root@localhost home]# grep "^$" audispd.conf
以#開頭,以main結尾以及空行的信息
[root@localhost home]# grep -v "^$" audispd.conf # # This file controls the configuration of the audit event # dispatcher daemon, audispd. # q_depth = 64 overflow_action = SYSLOG name_format = HOSTNAME #name = mydomain
去除空行
tar命令:
用途:制作歸檔文件、釋放歸檔文件
格式:
制作歸檔文件:tar [選項]... 歸檔文件名 源文件或目錄
釋放歸檔文件:tar [選項]... 歸檔文件名 [-C 目標目錄]
-c 創建.tar格式的包文件
-x 解開.tar格式的包文件
-v 輸出詳細信息
-f 表示使用歸檔文件
-p 打包時保留源文件或目錄的權限
-t 列表查看包內文件
-C 解包時指定釋放的目標目錄
-z 調用gzip程序進行壓縮或解壓
-j 調用bzip2程序進行壓縮或解壓
創建歸檔文件時一般使用.tar文件名作為結尾
[root@localhost home]# ls audispd.conf file file1 file2 test1 test2 [root@localhost home]# tar -cvf file1.tar file1 file1 [root@localhost home]# ls audispd.conf file file1 file1.tar file2 test1 test2 [root@localhost home]# ls -l file1 -rw-r--r-- 1 root root 98 Jan 27 04:11 file1 [root@localhost home]# ls -l file1.tar -rw-r--r-- 1 root root 10240 Jan 27 06:08 file1.tar
可以看到打包之後的文件比源文件還大,這是因為打包並沒有進行壓縮,而且打包還帶有一些打包信息
如果你是使用gzip程序進行壓縮,後綴名就是.gz
[root@localhost home]# tar -zcvf file1.tar.gz file1 file1
[root@localhost home]# ls -l file1.tar.gz -rw-r--r-- 1 root root 148 Jan 27 06:13 file1.tar.gz
會發現一個有趣的現象,壓縮之後的文件比源文件還大
[root@localhost home]# du -h file1 4.0K file1 [root@localhost home]# du -h file1.tar.gz 4.0K file1.tar.gz
使用du命令查看兩個文件的磁盤空間,均為4k,這是因為Linux給文件劃分磁盤空間的時候,最小單位為4k。
在壓縮的時候,會寫入一些打包信息,如果你源文件太小,那麽壓縮之後的文件就會比源文件更大。
新建一個文件,往裏面添加多一點的內容
[root@localhost home]# tar -zcvf file3.tar.gz file3 file3 [root@localhost home]# ls -l total 52 -rw-r----- 1 root root 173 Jan 27 04:26 audispd.conf -rw-r--r-- 1 root root 16 Jan 27 04:18 file -rw-r--r-- 1 root root 98 Jan 27 04:11 file1 -rw-r--r-- 1 root root 10240 Jan 27 06:08 file1.tar -rw-r--r-- 1 root root 148 Jan 27 06:13 file1.tar.gz -rw-r--r-- 1 root root 117 Jan 27 04:11 file2 -rw-r--r-- 1 root root 160 Jan 27 06:17 file2.tar.gz -rw-r--r-- 1 root root 1415 Jan 27 06:31 file3 -rw-r--r-- 1 root root 213 Jan 27 06:32 file3.tar.gz drwxr-xr-x 3 root root 4096 Jan 27 00:27 test1 drwxr-xr-x 2 root root 4096 Jan 27 00:23 test2
可以看到這次壓縮file3,壓縮後的文件比源文件小很多了
把源文件都刪除,然後進行解壓縮
[root@localhost home]# tar -zxvf file3.tar.gz file3 [root@localhost home]# ls audispd.conf file1.tar.gz file3 test1 file1.tar file2.tar.gz file3.tar.gz test2
可以看到file3被解壓出來了,不寫目標目錄,默認解壓到當前目錄
還可以解壓到指定的目錄,-C 指定的目錄名
[root@localhost home]# mkdir test [root@localhost home]# tar -zxvf file3.tar.gz -C test file3 [root@localhost home]# ls test file3
可以看到壓縮文件被解壓到test目錄下了
如果你用-j調用bzip2程序壓縮文件,那麽後綴名就是bz2
[root@localhost home]# ls -l test total 4 -rw-r--r-- 1 root root 1415 Jan 27 06:31 file3 [root@localhost home]# tar -jcvf test.tar.bz2 test test/ test/file3 [root@localhost home]# ls audispd.conf file1.tar.gz file3 test test2 file1.tar file2.tar.gz file3.tar.gz test1 test.tar.bz2 [root@localhost home]# ls -l test.tar.bz2 -rw-r--r-- 1 root root 278 Jan 27 06:42 test.tar.bz2 [root@localhost home]# tar -jxvf test.tar.bz2 -C test1 test/ test/file3 [root@localhost home]# ls -l test1 total 4 lrwxrwxrwx 1 root root 11 Jan 27 00:16 file1 -> /home/file1 drwxr-xr-x 2 root root 4096 Jan 27 06:38 test
可以看到壓縮文件解壓到了指定的目錄下
vim命令:
用途:編輯文件
格式: vim 文件名
i 進入編輯模式
wq 保存並退出,如果vim 文件名(文件不存在),wq保存之後,該文件也就生成並且編輯保存了
q! 強制退出,不保存,如果vim 文件名(文件不存在),q!之後,該文件也不會生成
小結:
tar 命令制作或釋放歸檔文件,-cvf 是打包但並沒有壓縮,-zcvf 以gzip程序壓縮文件,-jcvf以bzip2程序壓縮文件,-x是解壓縮,-C 跟指定目錄是解壓到指定目錄
vim創建新文件時,只有保存並退出,文件才會被創建編輯
cat、wc、head、tail、grep
Linux基礎知識--文件內容操作命令