這篇文章主要介紹“怎么在Linux中歸檔文件和目錄”,在日常操作中,相信很多人在怎么在Linux中歸檔文件和目錄問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么在Linux中歸檔文件和目錄”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司主營(yíng)紅山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā),紅山h5微信小程序開(kāi)發(fā)搭建,紅山網(wǎng)站營(yíng)銷(xiāo)推廣歡迎紅山等地區(qū)企業(yè)咨詢(xún)
歸檔文件和目錄最常見(jiàn)的程序是:
tar
zip
這是一個(gè)很大的話題,所以,我將分兩部分發(fā)表這篇文章。在***部分中,我們將看到如何使用 tar 命令來(lái)歸檔文件和目錄。
Tar是一個(gè) Unix 命令,代表 Tape Archive(磁帶歸檔)。它用于將多個(gè)文件(相同或不同大?。┙M合或存儲(chǔ)到一個(gè)文件中。在 tar 實(shí)用程序中有 4 種主要的操作模式。
c
– 從文件或目錄中建立歸檔
x
– 提取歸檔
r
– 將文件追加到歸檔
t
– 列出歸檔的內(nèi)容
有關(guān)完整的模式列表,參閱 man 手冊(cè)頁(yè)。
為了本指南,我將使用名為 ostechnix
的文件夾,其中包含三種不同類(lèi)型的文件。
$ ls ostechnix/file.odt image.png song.mp3
現(xiàn)在,讓我們?yōu)?ostechnix
目錄創(chuàng)建一個(gè)新的 tar 歸檔。
$ tar cf ostechnix.tar ostechnix/
這里,c
標(biāo)志指的是創(chuàng)建新的歸檔,f
是指定歸檔文件。
同樣,對(duì)當(dāng)前工作目錄中的一組文件創(chuàng)建歸檔文件,使用以下命令:
$ tar cf archive.tar file1 file2 file 3
要在當(dāng)前目錄中提取歸檔文件,只需執(zhí)行以下操作:
$ tar xf ostechnix.tar
我們還可以使用 C
標(biāo)志(大寫(xiě)字母 C)將歸檔提取到不同的目錄中。例如,以下命令將歸檔文件提取到 Downloads
目錄中。
$ tar xf ostechnix.tar -C Downloads/
或者,轉(zhuǎn)到 Downloads
文件夾并像下面一樣提取其中的歸檔。
$ cd Downloads/$ tar xf ../ostechnix.tar
有時(shí),你可能想要提取特定類(lèi)型的文件。例如,以下命令提取 “.png” 類(lèi)型的文件。
$ tar xf ostechnix.tar --wildcards "*.png"
默認(rèn)情況下,tar 創(chuàng)建歸檔文件以 .tar
結(jié)尾。另外,tar
命令可以與壓縮實(shí)用程序 gzip
和 bzip
結(jié)合使用。文件結(jié)尾以 .tar
為擴(kuò)展名使用普通 tar 來(lái)歸檔文件,文件以 tar.gz
或 .tgz
結(jié)尾使用 gzip
歸檔并壓縮文件,文件以 tar.bz2
或 .tbz
結(jié)尾使用 bzip
歸檔并壓縮。
首先,讓我們來(lái)創(chuàng)建一個(gè) gzip 歸檔:
$ tar czf ostechnix.tar.gz ostechnix/
或者:
$ tar czf ostechnix.tgz ostechnix/
這里,我們使用 z
標(biāo)志來(lái)使用 gzip 壓縮方法壓縮歸檔文件。
你可以使用 v
標(biāo)志在創(chuàng)建歸檔時(shí)查看進(jìn)度。
$ tar czvf ostechnix.tar.gz ostechnix/ostechnix/ostechnix/file.odtostechnix/image.pngostechnix/song.mp3
這里,v
指顯示進(jìn)度。
從一個(gè)文件列表創(chuàng)建 gzip 歸檔文件:
$ tar czf archive.tgz file1 file2 file3
要提取當(dāng)前目錄中的 gzip 歸檔文件,使用:
$ tar xzf ostechnix.tgz
要提取到其他文件夾,使用 -C
標(biāo)志:
$ tar xzf ostechnix.tgz -C Downloads/
現(xiàn)在,讓我們創(chuàng)建 bzip 歸檔。為此,請(qǐng)使用下面的 j
標(biāo)志。
創(chuàng)建一個(gè)目錄的歸檔:
$ tar cjf ostechnix.tar.bz2 ostechnix/
或
$ tar cjf ostechnix.tbz ostechnix/
從一個(gè)列表文件中創(chuàng)建歸檔:
$ tar cjf archive.tar.bz2 file1 file2 file3
或
$ tar cjf archive.tbz file1 file2 file3
為了顯示進(jìn)度,使用 v
標(biāo)志。
現(xiàn)在,在當(dāng)前目錄下,讓我們提取一個(gè) bzip 歸檔。這樣做:
$ tar xjf ostechnix.tar.bz2
或者,提取歸檔文件到其他目錄:
$ tar xjf ostechnix.tar.bz2 -C Downloads
這是 tar
命令的另一個(gè)最酷的功能。要一次創(chuàng)建多個(gè)目錄或文件的 gzip 歸檔文件,使用以下文件:
$ tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt
上述命令創(chuàng)建 Downloads
、 Documents
目錄和 ostechnix
目錄下的 file.odt
文件的歸檔,并將歸檔保存在當(dāng)前工作目錄中。
這在備份數(shù)據(jù)時(shí)非常有用。你可以在備份中排除不重要的文件或目錄,這是 –exclude
選項(xiàng)所能幫助的。例如你想要?jiǎng)?chuàng)建 /home
目錄的歸檔,但不希望包括 Downloads
、 Documents
、 Pictures
、 Music
這些目錄。
這是我們的做法:
$ tar czvf ostechnix.tgz /home/sk --exclude=/home/sk/Downloads --exclude=/home/sk/Documents --exclude=/home/sk/Pictures --exclude=/home/sk/Music
上述命令將對(duì)我的 $HOME
目錄創(chuàng)建一個(gè) gzip 歸檔,其中不包括 Downloads
、Documents
、Pictures
和 Music
目錄。要?jiǎng)?chuàng)建 bzip 歸檔,將 z
替換為 j
,并在上例中使用擴(kuò)展名 .bz2
。
要列出歸檔文件的內(nèi)容,我們使用 t
標(biāo)志。
$ tar tf ostechnix.tarostechnix/ostechnix/file.odtostechnix/image.pngostechnix/song.mp3
要查看詳細(xì)輸出,使用 v
標(biāo)志。
$ tar tvf ostechnix.tardrwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3
文件或目錄可以使用 r
標(biāo)志添加/更新到現(xiàn)有的歸檔??纯聪旅娴拿睿?/p>
$ tar rf ostechnix.tar ostechnix/ sk/ example.txt
上面的命令會(huì)將名為 sk
的目錄和名為 exmple.txt
添加到 ostechnix.tar
歸檔文件中。
你可以使用以下命令驗(yàn)證文件是否已添加:
$ tar tvf ostechnix.tardrwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3drwxr-xr-x sk/users 0 2018-03-26 19:52 sk/-rw-r--r-- sk/users 0 2018-03-26 19:39 sk/linux.txt-rw-r--r-- sk/users 0 2018-03-26 19:56 example.txt
創(chuàng)建 tar 歸檔:
普通 tar 歸檔:tar -cf archive.tar file1 file2 file3
Gzip tar 歸檔:tar -czf archive.tgz file1 file2 file3
Bzip tar 歸檔:tar -cjf archive.tbz file1 file2 file3
提取 tar 歸檔:
普通 tar 歸檔:tar -xf archive.tar
Gzip tar 歸檔:tar -xzf archive.tgz
Bzip tar 歸檔:tar -xjf archive.tbz
我們只介紹了 tar
命令的基本用法,這些對(duì)于開(kāi)始使用 tar
命令足夠了。但是,如果你想了解更多詳細(xì)信息,參閱 man 手冊(cè)頁(yè)。
$ man tar
到此,關(guān)于“怎么在Linux中歸檔文件和目錄”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
名稱(chēng)欄目:怎么在Linux中歸檔文件和目錄
文章位置:http://www.rwnh.cn/article46/ipjgeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、外貿(mào)建站、網(wǎng)站營(yíng)銷(xiāo)、云服務(wù)器、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)