linux文件搜索及其它基礎(chǔ)命令介紹(3)
1、linux中包含大量的文件,對(duì)于文件查找,linux提供了find命令。
find是一個(gè)非常有效的工具,它可以遍歷目標(biāo)目錄甚至整個(gè)文件系統(tǒng)來查找某些文件或目錄:
find [path...] [expression]
其中expression包括三種:options、tests和actions。多個(gè)表達(dá)式之間被操作符分隔,當(dāng)操作符被省略時(shí),表示使用了默認(rèn)操作符-and。
當(dāng)表達(dá)式中不包含任何actions時(shí),默認(rèn)使用-print,也就是打印出搜索到的所有文件,用換行分隔。
其實(shí)可以將三種表達(dá)式均視為選項(xiàng),表示對(duì)搜索的某種限制(如-maxdepth表示搜索路徑的最大深度)、或?qū)φ业降哪繕?biāo)文件的某種測(cè)試(如-readable判斷是否可讀)、或?qū)Y(jié)果采取的某種動(dòng)作(如-print)。
選項(xiàng)-name pattern搜索文件名:
[root@centos7 temp]# find /root/* -name "file?" /root/file1 /root/temp/file1 /root/temp/file2 /root/temp/file3 /root/temp/file4 /root/temp/file5 /root/temp/file6 /root/temp/file7 /root/temp/file8 /root/temp/file9 [root@centos7 temp]#
此例中搜索目錄/root下所有文件,找出匹配file?的文件名,同時(shí)由于沒有指定action,所以使用默認(rèn)的-print將結(jié)果打印出來。find命令中,搜索路徑和某些文件名的表示可以使用shell通配符(見上一篇),但為了避免混淆,處于選項(xiàng)后的通配符需要被引號(hào)引起來。
選項(xiàng)-maxdepth n指定搜索路徑的最大深度:
[root@centos7 ~]# find /root -maxdepth 1 -name "file?" #注意表達(dá)式之間的隱含操作符 -and /root/file1 [root@centos7 ~]#
本例中指定最大深度為1,表示只搜索/root目錄,而不進(jìn)入任何它的子目錄去搜索。
和此選項(xiàng)相對(duì)應(yīng),-mindepth表示指定搜索路徑的最小深度。
選項(xiàng)-user name按照文件屬主來查找文件:
[root@centos7 ~]# find /root/temp -name "file?" -user learner /root/temp/file1 /root/temp/file2 [root@centos7 ~]#
或者類似選項(xiàng)-uid n表示按文件屬主的uid,-gid n表示按文件所屬組的gid,-group name表示按文件所屬組。
選項(xiàng)-mtime n 文件上次內(nèi)容被修改距離現(xiàn)在n*24小時(shí):
[root@centos7 temp]# ls -lt file1? -rw-r--r-- 1 root root 64 10月 27 15:06 file11 -rw-r--r-- 1 root root 132 10月 27 13:28 file10 -rw-r--r-- 1 root root 22 10月 26 21:31 file12 -rw-r--r-- 1 root root 137 10月 12 16:42 file13 [root@centos7 temp]# find . -name "file1?" -mtime +5 #五天前 ./file13 [root@centos7 temp]# [root@centos7 temp]# find . -name "file1?" -mtime -5 #五天內(nèi) ./file10 ./file11 [root@centos7 temp]# [root@centos7 temp]# find . -name "file1?" -mtime 5 #剛好五天 ./file12 [root@centos7 temp]#
本例中使用了命令ls的選項(xiàng)-t對(duì)文件的時(shí)間進(jìn)行排序,最近被修改的文件在前。選項(xiàng)-mtime n中n可以表示成:
+n 表示大于n
-n 表示小于n
n 表示等于n
還有其他時(shí)間(如atime,ctime)的比較,用法相同。
選項(xiàng)-newer file表示搜索到的文件比指定的file要‘新'(上次內(nèi)容被修改離現(xiàn)在時(shí)間更短):
[root@centos7 temp]# find . -name "file1?" -newer file12 ./file10 ./file11 [root@centos7 temp]#
選項(xiàng)-path pattern文件名匹配pattern(通配符):
[root@centos7 temp]# find . -name "file1?" -path "./file1[13]" ./file11 ./file13 [root@centos7 temp]#
注意pattern匹配時(shí)不會(huì)對(duì)/和.進(jìn)行特殊處理。
通常-path會(huì)配合選項(xiàng)-prune使用,表示對(duì)某目錄的排除:
[root@centos7 temp]# find . -name "file*" ./file10 ./file12 ./file11 ./tmp/file ./file13 [root@centos7 temp]# [root@centos7 temp]# find . -path "./tmp" -prune -o -name "file*" -print ./file10 ./file12 ./file11 ./file13 [root@centos7 temp]#
這里的-o表示或者,它和之前所說的-and都是操作符。表示表達(dá)式之間的邏輯關(guān)系。本例中可以理解為:如果目錄匹配./tmp則執(zhí)行-prune跳過該目錄,否則匹配-name指定的文件并執(zhí)行-print。
除這兩個(gè)操作符外,操作符!或-not表示邏輯非,操作符(...)和數(shù)學(xué)運(yùn)算中的括號(hào)類似,表示提高優(yōu)先級(jí):
[root@centos7 temp]# find . ! -path "./tmp*" -name "file*" ./file10 ./file12 ./file11 ./file13 [root@centos7 temp]# #排除多個(gè)目錄: [root@centos7 temp]# find . \( -path "./tmp" -o -path "./abcd" \) -prune -o -name "file*" -print ./file10 ./file12 ./file11 ./file13 [root@centos7 temp]#
注意這里的(...)操作符需要被轉(zhuǎn)義(為避免被shell解釋為其他含義),在符號(hào)前加上反斜線'\'。(關(guān)于shell中的轉(zhuǎn)義或引用我們會(huì)在講bash編程時(shí)詳述)
選項(xiàng)-type x表示搜索類型為x的文件,其中x的可能值包括b、c、d、p、f、l、s。它們和命令ls顯示的文件類型一致(見基礎(chǔ)命令介紹一),f代表普通文件。
[root@centos7 temp]# ln -s file13 file14 [root@centos7 temp]# ls -l file14 lrwxrwxrwx 1 root root 6 11月 1 12:29 file14 -> file13 [root@centos7 temp]# find . -type l ./file14 [root@centos7 temp]#
選項(xiàng)-perm mode表示搜索特定權(quán)限的文件:
[root@centos7 temp]# chmod 777 file14 [root@centos7 temp]# ls -l file1[3-4] -rwxrwxrwx 1 root root 137 10月 12 16:42 file13 lrwxrwxrwx 1 root root 6 11月 1 12:29 file14 -> file13 [root@centos7 temp]# [root@centos7 temp]# find . -perm 777 ./file13 ./file14 [root@centos7 temp]#
或表示成:
[root@centos7 temp]# find . -perm -g=rwx #表示文件所屬組的權(quán)限是可讀、可寫、可執(zhí)行。 ./file13 ./file14 [root@centos7 temp]#
選項(xiàng)-size n表示搜索文件大小
[root@centos7 temp]# find . -path "./*" -size +100c ./file10 ./file13 [root@centos7 temp]#
此例中+100c表示當(dāng)前目錄下大于100 bytes的文件,n和前面表示時(shí)間的方式類似(+n,-n,n),n后面的字符還包括:
b 單位為512 bytes的塊(n后面沒有后綴時(shí)的默認(rèn)單位)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes
選項(xiàng)-print0類似-print輸出文件名,但不用任何字符分隔它們。當(dāng)文件名中包含特殊字符時(shí)使用??梢耘浜蠋нx項(xiàng)-0的命令xargs一起使用(后述)。
選項(xiàng)-exec command ;表示要執(zhí)行的命令
-exec后可以跟任意shell命令來對(duì)搜索到的文件做進(jìn)一步的處理,在command和分號(hào)之間都被視為command的參數(shù),其中用{}代表被搜索到的文件。分號(hào)需要被轉(zhuǎn)義。
如對(duì)搜索到的文件執(zhí)行命令ls -l:
[root@centos7 temp]# find . -name "file*" -exec ls -l {} \; -rw-r--r-- 1 root root 132 10月 27 13:28 ./file10 -rw-r--r-- 1 root root 22 10月 26 21:31 ./file12 -rw-r--r-- 1 root root 64 10月 27 15:06 ./file11 -rw-r--r-- 1 root root 67 10月 31 17:50 ./tmp/file -rw-r--r-- 1 root root 0 11月 1 12:05 ./abcd/file15 -rwxrwxrwx 1 root root 137 10月 12 16:42 ./file13 lrwxrwxrwx 1 root root 6 11月 1 12:29 ./file14 -> file13
-exec選項(xiàng)后的命令是在啟動(dòng)find所在的目錄內(nèi)執(zhí)行的,并且對(duì)于每個(gè)搜索到的文件,該命令都執(zhí)行一次,而不是把所有文件列在命令后面只執(zhí)行一次。
舉例說明下其中的區(qū)別:
#命令echo只執(zhí)行一次 [root@centos7 temp]# echo ./file11 ./file12 ./file13 ./file11 ./file12 ./file13 #命令echo執(zhí)行了三次 [root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} \; ./file12 ./file11 ./file13 [root@centos7 temp]#
當(dāng)使用格式-exec command {} +時(shí)表示每個(gè)文件都被追加到命令后面,這樣,命令就只被執(zhí)行一次了:
[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} + ./file12 ./file11 ./file13 [root@centos7 temp]#
但有時(shí)會(huì)出現(xiàn)問題:
[root@centos7 temp]# find . -name "file1[1-3]" -exec mv {} abcd/ + find: 遺漏“-exec”的參數(shù) [root@centos7 temp]#
因?yàn)檫@里文件被追加于目錄abcd/的后面,導(dǎo)致報(bào)錯(cuò)。
同時(shí),使用格式-exec command {} +還可能會(huì)造成被追加的文件數(shù)過多,超出了操作系統(tǒng)對(duì)命令行長(zhǎng)度的限制。
使用-exec可能會(huì)有安全漏洞,通常使用管道和另一個(gè)命令xargs來代替-exec執(zhí)行命令。
2、xargs 從標(biāo)準(zhǔn)輸入中獲得命令的參數(shù)并執(zhí)行
xargs從標(biāo)準(zhǔn)輸入中獲得由空格分隔的項(xiàng)目,并執(zhí)行命令(默認(rèn)為/bin/echo)
選項(xiàng)-0將忽略項(xiàng)目的分隔符,配合find的選項(xiàng)-print0,處理帶特殊符號(hào)的文件。
[root@centos7 temp]# find . -name "file*" -print0 | xargs -0 ls -l -rw-r--r-- 1 root root 132 10月 27 13:28 ./file10 -rw-r--r-- 1 root root 64 10月 27 15:06 ./file11 -rw-r--r-- 1 root root 22 10月 26 21:31 ./file12 -rwxrwxrwx 1 root root 137 10月 12 16:42 ./file13 -rw-r--r-- 1 root root 0 11月 1 14:45 ./file 14 #注意此文件名中包含空格
當(dāng)不用時(shí):
[root@centos7 temp]# find . -name "file*" | xargs ls ls: 無法訪問./file: 沒有那個(gè)文件或目錄 ls: 無法訪問14: 沒有那個(gè)文件或目錄 ./file10 ./file11 ./file12 ./file13
選項(xiàng)-I string為輸入項(xiàng)目指定替代字符串:
[root@centos7 temp]# ls abcd/ [root@centos7 temp]# find . -name "file*" | xargs -I{} mv {} abcd/ [root@centos7 temp]# ls abcd/ file10 file11 file12 file13 [root@centos7 temp]#
這里的意思是說使用-I后面的字符串去代替輸入項(xiàng)目,這樣就可以把它們作為整體放到命令的任意位置來執(zhí)行了。也避免了-exec command {} +的錯(cuò)誤。
選項(xiàng)-d指定輸入項(xiàng)目的分隔符:
[root@centos7 temp]# head -n1 /etc/passwd root:x:0:0:root:/root:/bin/bash [root@centos7 temp]# head -n1 /etc/passwd|xargs -d ":" echo -n root x 0 0 root /root /bin/bash [root@centos7 temp]#
選項(xiàng)-P指定最大進(jìn)程數(shù),默認(rèn)進(jìn)程數(shù)為1,多個(gè)進(jìn)程并發(fā)執(zhí)行。
3、date 打印或設(shè)置系統(tǒng)時(shí)間
date [OPTION]... [+FORMAT]
當(dāng)沒有任何參數(shù)時(shí)表示顯示當(dāng)前時(shí)間:
[root@centos7 temp]# date 2016年 11月 01日 星期二 15:30:46 CST [root@centos7 temp]#
選項(xiàng)-d string按描述字符串顯示時(shí)間(例子中字符串表示距離1970-01-01零點(diǎn)的秒數(shù)):
[root@centos7 temp]# date --date='@2147483647' 2038年 01月 19日 星期二 11:14:07 CST
或者:
[root@centos7 temp]# date -d@2147483647 2038年 01月 19日 星期二 11:14:07 CST
-d后面的字符串還可以是:
[root@centos7 temp]# date -d "-1 day" 2016年 10月 31日 星期一 16:11:27 CST
表示昨天
又如明年表示為:
[root@centos7 temp]# date -d "1 year" 2017年 11月 01日 星期三 16:12:27 CST
選項(xiàng)-s設(shè)置系統(tǒng)時(shí)間:
[root@centos7 temp]# date -s "2016-11-01 15:49" 2016年 11月 01日 星期二 15:49:00 CST [root@centos7 temp]# date 2016年 11月 01日 星期二 15:49:03 CST
由于linux系統(tǒng)啟動(dòng)時(shí)將讀取CMOS來獲得時(shí)間,系統(tǒng)會(huì)每隔一段時(shí)間將系統(tǒng)時(shí)間寫入CMOS,為避免更改時(shí)間后系統(tǒng)的立即重啟造成時(shí)間沒有被寫入CMOS,通常設(shè)置完時(shí)間后會(huì)使用命令clock -w將系統(tǒng)時(shí)間寫入到CMOS中。
date命令中由FORMAT來控制輸出格式,加號(hào)+在格式之前表示格式開始:
[root@centos7 temp]# date "+%Y-%m-%d %H:%M:%S" 2016-11-01 16:00:45 [root@centos7 temp]#
本例中格式被雙引號(hào)引起來以避免被shell誤解,其中:
%Y 表示年
%m 表示月
%d 表示天
%H 表示小時(shí)
%M 表示分鐘
%S 表示秒
還可以指定很多其他格式如只輸出當(dāng)前時(shí)間:
[root@centos7 temp]# date "+%T" 16:03:50 [root@centos7 temp]#
如輸出距離1970-01-01零點(diǎn)到現(xiàn)在時(shí)間的秒數(shù):
[root@centos7 temp]# date +%s 1477987540 [root@centos7 temp]#
如輸出今天星期幾:
[root@centos7 temp]# date +%A 星期二 [root@centos7 temp]#
其他格式請(qǐng)自行man
4、gzip 壓縮或解壓文件
gzip [OPTION]... [FILE]...
當(dāng)命令后直接跟文件時(shí),表示壓縮該文件:
[root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 132 10月 27 13:28 file10 -rw-r--r-- 1 root root 64 10月 27 15:06 file11 -rw-r--r-- 1 root root 22 10月 26 21:31 file12 -rw-r--r-- 1 root root 137 10月 12 16:42 file13 [root@centos7 temp]# [root@centos7 temp]# gzip file10 file11 file12 file13 [root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 75 10月 27 13:28 file10.gz -rw-r--r-- 1 root root 49 10月 27 15:06 file11.gz -rw-r--r-- 1 root root 44 10月 26 21:31 file12.gz -rw-r--r-- 1 root root 109 10月 12 16:42 file13.gz
壓縮后的文件以.gz結(jié)尾,gzip是不保留源文件的
選項(xiàng)-d表示解壓縮
[root@centos7 temp]# gzip -d *.gz [root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 132 10月 27 13:28 file10 -rw-r--r-- 1 root root 64 10月 27 15:06 file11 -rw-r--r-- 1 root root 22 10月 26 21:31 file12 -rw-r--r-- 1 root root 137 10月 12 16:42 file13
選項(xiàng)-r可以遞歸地進(jìn)入目錄并壓縮里面的文件
選項(xiàng)-n指定壓縮級(jí)別,n為從1-9的數(shù)字。1為最快壓縮,但壓縮比最小;9的壓縮速度最慢,但壓縮比最大。默認(rèn)時(shí)n為6。
[root@centos7 temp]# gzip -r9 ./tmp
當(dāng)gzip后沒有文件或文件為-時(shí),將從標(biāo)準(zhǔn)輸入讀取并壓縮:
[root@centos7 temp]# echo "hello world" | gzip >hello.gz [root@centos7 temp]# ls -l *.gz -rw-r--r-- 1 root root 32 11月 1 16:40 hello.gz
注意例子中g(shù)zip的輸出被重定向到文件hello.gz中,如果對(duì)此文件進(jìn)行解壓,將會(huì)生成文件hello。如果被重定向的文件后綴不是.gz,文件名在被改成.gz后綴之前將不能被解壓。
5、zcat 將壓縮的文件內(nèi)容輸出到標(biāo)準(zhǔn)輸出
[root@centos7 temp]# zcat hello.gz hello world [root@centos7 temp]#
zcat讀取被gzip壓縮的文件,只需文件格式正確,不需要文件名具有.gz的后綴。
6、bzip2 壓縮解壓文件
bzip2 [OPTION]... [FILE]...
命令bzip2和gzip類似都是壓縮命令,只是使用的壓縮算法不一樣,通常bzip2的壓縮比較高。本命令默認(rèn)同樣不保留源文件,默認(rèn)文件名后綴為.bz2:
[root@centos7 temp]# bzip2 file11 [root@centos7 temp]# ls -l file11.bz2 -rw-r--r-- 1 root root 61 10月 27 15:06 file11.bz2
選項(xiàng)-k可使源文件保留:
[root@centos7 temp]# bzip2 -k file10 [root@centos7 temp]# ls -l file10* -rw-r--r-- 1 root root 132 10月 27 13:28 file10 -rw-r--r-- 1 root root 96 10月 27 13:28 file10.bz2
選項(xiàng)-d表示解壓(若存在源文件則報(bào)錯(cuò)):
[root@centos7 temp]# bzip2 -d file10.bz2 bzip2: Output file file10 already exists. [root@centos7 temp]# bzip2 -d file11.bz2 [root@centos7 temp]# ls -l file11 -rw-r--r-- 1 root root 64 10月 27 15:06 file11
選項(xiàng)-f表示強(qiáng)制覆蓋源文件:
[root@centos7 temp]# bzip2 -d -f file10.bz2 [root@centos7 temp]# ls -l file10* -rw-r--r-- 1 root root 132 10月 27 13:28 file10
選項(xiàng)-n和gzip用法一致,表示壓縮比。
7、tar 打包壓縮文件
tar [OPTION...] [FILE]...
命令gzip和bzip2均不支持壓縮目錄(雖然gzip可以用選項(xiàng)-r到目錄內(nèi)去壓縮,但仍無法壓縮目錄),用tar命令可以將目錄歸檔,然后利用壓縮命令進(jìn)行壓縮:
[root@centos7 temp]# tar -cf tmp.tar tmp/ [root@centos7 temp]# ls -l 總用量 18256 drwxr-xr-x 2 root root 6 11月 1 16:23 abcd -rwxr-xr-x 1 root root 12 10月 28 17:24 test.sh drwxr-xr-x 2 root root 425984 11月 1 17:08 tmp -rw-r--r-- 1 root root 18001920 11月 1 17:17 tmp.tar
例子中選項(xiàng)-c表示創(chuàng)建打包文件,-f tmp.tar表示指定打包文件名為tmp.tar,后面跟被打包目錄名tmp/。
選項(xiàng)-t列出歸檔內(nèi)容
選項(xiàng)-v詳細(xì)地列出處理的文件
[root@centos7 temp]# ls -l abcd.tar -rw-r--r-- 1 root root 10240 11月 2 08:58 abcd.tar [root@centos7 temp]# tar -tvf abcd.tar drwxr-xr-x root/root 0 2016-11-02 08:57 abcd/ -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file10 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file11 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file12 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file13
選項(xiàng)-u更新歸檔文件(update)。
[root@centos7 temp]# touch abcd/file15 [root@centos7 temp]# tar uvf abcd.tar abcd abcd/file15 [root@centos7 temp]# tar tvf abcd.tar drwxr-xr-x root/root 0 2016-11-02 08:57 abcd/ -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file10 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file11 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file12 -rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file13 -rw-r--r-- root/root 0 2016-11-02 09:07 abcd/file15
選項(xiàng)-x對(duì)歸檔文件進(jìn)行提取操作。(解包)
[root@centos7 temp]# rm -rf abcd/ [root@centos7 temp]# tar -xvf abcd.tar abcd/ abcd/file10 abcd/file11 abcd/file12 abcd/file13 abcd/file15 [root@centos7 temp]# ls abcd #這里是按兩次tab鍵的補(bǔ)全結(jié)果 abcd/ abcd.tar [root@centos7 temp]#
選項(xiàng)-O解壓文件至標(biāo)準(zhǔn)輸出
[root@centos7 temp]# tar -xf abcd.tar -O hello hello hello hello [root@centos7 temp]# #注意這里輸出了每個(gè)歸檔文件的內(nèi)容 [root@centos7 temp]# tar -xf abcd.tar -O | xargs echo hello hello hello hello [root@centos7 temp]#
選項(xiàng)-p保留文件權(quán)限(用于解包時(shí))。
選項(xiàng)-j、-J、-z 用于壓縮。
其中-j使用命令bzip2,-J使用命令xz,-z使用命令gzip分別將歸檔文件進(jìn)行壓縮解壓處理(命令tar后的選項(xiàng)可以省略-):
[root@centos7 temp]# tar zcf tmp.tar.gz tmp [root@centos7 temp]# tar jcf tmp.tar.bz2 tmp [root@centos7 temp]# tar Jcf tmp.tar.xz tmp [root@centos7 temp]# du -sh tmp* 70M tmp 28K tmp.tar.bz2 180K tmp.tar.gz 40K tmp.tar.xz [root@centos7 temp]#
本例中分別使用三種壓縮格式進(jìn)行壓縮,可以看到使用命令bzip2的壓縮比最高,命令gzip的壓縮比最低。在執(zhí)行壓縮文件時(shí),壓縮時(shí)間也是我們考量的一個(gè)重要因素。默認(rèn)時(shí),使用gzip最快,xz最慢。
對(duì)于這三種格式的壓縮文件進(jìn)行解壓,只需將選項(xiàng)中-c換成-x即可。
選項(xiàng)-X FILE 排除匹配文件FILE中所列模式的文件:
[root@centos7 abcd]# cat file file10 file13 [root@centos7 abcd]# tar -X file -cf file.tar file* [root@centos7 abcd]# tar -tvf file.tar -rw-r--r-- root/root 14 2016-11-02 10:10 file -rw-r--r-- root/root 6 2016-11-02 10:02 file11 -rw-r--r-- root/root 6 2016-11-02 10:02 file12 -rw-r--r-- root/root 0 2016-11-02 09:07 file15
注意文件FILE中支持通配符匹配:
[root@centos7 abcd]# cat file file1[2-3] [root@centos7 abcd]# tar -X file -cf file.tar file* [root@centos7 abcd]# tar -tvf file.tar -rw-r--r-- root/root 11 2016-11-02 10:20 file -rw-r--r-- root/root 6 2016-11-02 10:02 file10 -rw-r--r-- root/root 6 2016-11-02 10:02 file11 -rw-r--r-- root/root 0 2016-11-02 09:07 file15
選項(xiàng)-C DIR改變至目錄DIR(用于解包時(shí)):
[root@centos7 temp]# tar zxf tmp.tar.gz -C abcd [root@centos7 temp]# ls -l abcd/ 總用量 688 -rw-r--r-- 1 root root 11 11月 2 10:20 file -rw-r--r-- 1 root root 6 11月 2 10:02 file10 -rw-r--r-- 1 root root 6 11月 2 10:02 file11 -rw-r--r-- 1 root root 6 11月 2 10:02 file12 -rw-r--r-- 1 root root 6 11月 2 10:02 file13 -rw-r--r-- 1 root root 0 11月 2 09:07 file15 drwxr-xr-x 2 root root 425984 11月 1 17:08 tmp
只解壓指定文件:
[root@centos7 temp]# tar zxvf tmp.tar.gz -C abcd/ file1[23] file12 file13 [root@centos7 temp]# ls -l abcd 總用量 12 -rw-r--r-- 1 root root 6 11月 16 15:26 file12 -rw-r--r-- 1 root root 6 11月 16 15:26 file13
注意這里解壓時(shí),指定文件不能在選項(xiàng)-C之前
如不想解壓壓縮包,但想查看壓縮包中某個(gè)文件的內(nèi)容時(shí),可以使用如下技巧:
[root@centos7 temp]# tar zxf tmp.tar.gz file -O BLOG ADDRESS IS "http://www.dbjr.com.cn/article/101263.htm" [root@centos7 temp]#
本文講述了linux中關(guān)于文件搜索和歸檔壓縮等相關(guān)的命令及部分選項(xiàng)用法,都是在系統(tǒng)管理過程中經(jīng)常要使用的,需熟練使用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何短時(shí)間內(nèi)學(xué)好一門語(yǔ)言 shell腳本語(yǔ)言為例
這篇文章主要以shell腳本語(yǔ)言為例,為大家介紹了如何短時(shí)間內(nèi)學(xué)好一門語(yǔ)言,感興趣的小伙伴們可以參考一下2016-09-09shell編程中for循環(huán)語(yǔ)句的實(shí)現(xiàn)過程及案例
Bash?Shell中主要提供了三種循環(huán)方式:for、while和until,下面這篇文章主要給大家介紹了關(guān)于shell編程中for循環(huán)語(yǔ)句的實(shí)現(xiàn)過程及案例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Linux Shell 常見的命令行格式簡(jiǎn)明總結(jié)
這篇文章主要介紹了Linux Shell 常見的命令行格式簡(jiǎn)明總結(jié),非常實(shí)用,需要的朋友可以參考下2014-04-04解決linux?shell中傳遞包含空格的參數(shù)問題
這篇文章主要介紹了如何解決linux?shell中傳遞包含空格的參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09linux中文件的三種time(atime,mtime,ctime)的用法
linux下文件有3個(gè)時(shí)間的,分別是atime,mtime,ctime,有些小伙伴對(duì)這3個(gè)時(shí)間還是比較迷茫和困惑的,所以小編為大家整理了下,希望對(duì)大家有所幫助2023-08-08詳解systemctl?和?service?區(qū)別及命令
systemctl和service都是管理Linux系統(tǒng)服務(wù)的工具,但systemctl更加先進(jìn),可以方便地管理systemd服務(wù),而service適用于管理傳統(tǒng)的SysV服務(wù),這篇文章主要介紹了systemctl和service區(qū)別及命令,需要的朋友可以參考下2023-07-07