- tar 打包備份
- gzip 壓縮或解壓文件
- zip 打包和壓縮文件
- unzip 解壓zip文件
- scp 遠程文件復制
- rsync 文件同步工具
這期呢主要說一說Linux中文件備份與壓縮命令,一共6個命令。這6個命令都是平常工作中非常非常常用的。
tar 打包備份
1、簡介
tar 可以將多個文件壓縮打包、壓縮。是工作中經(jīng)常用到的命令
2、語法格式
tar [參數(shù)選項] [文件或目錄]
3、參數(shù)說明
參數(shù) | 參數(shù)說明 |
---|---|
z | 通過gzip壓縮或解壓 |
c | 創(chuàng)建新的tar包 |
v | 顯示詳細的tar命令執(zhí)行過程 |
f | 指定壓縮文件名字 |
t | 不解壓查看tar包的內(nèi)容 |
p | 保持文件的原有屬性 |
j | 通過bzip2命令壓縮或解壓 |
x | 解開tar包 |
C | 指定解壓的目錄路徑 |
--exclude=PATTERN | 打包時排除不需要處理的文件或目錄 |
-h | 打包軟連接文件指向的真實源文件 |
-hard-dereference | 打包硬鏈接文件 |
4、實踐操作
① 打包博客文件(將/home/hexoBlog 打包成hexoBlog.tar.gz)
[root@xiezhr home]# tar zcvf hexoBlog.tar.gz hexoBlog
hexoBlog/
hexoBlog/page/
hexoBlog/page/3/
hexoBlog/page/3/index.html
hexoBlog/page/2/
hexoBlog/page/2/index.html
hexoBlog/contact/
hexoBlog/contact/index.html
hexoBlog/baidusitemap.xml
hexoBlog/movies/
hexoBlog/movies/index.html
hexoBlog/images/
hexoBlog/images/0.jpg
hexoBlog/images/2020年年終總結(jié).jpg
hexoBlog/archives/
hexoBlog/archives/87a8e6ef.html
hexoBlog/archives/93a2b0e4.html
hexoBlog/archives/b55ad976.html
hexoBlog/archives/page/
hexoBlog/archives/page/3/
[root@xiezhr home]# ll
drwxr-xr-x 30 git git 4096 Jun 6 20:42 hexoBlog
-rw-r--r-- 1 root root 43478804 Jun 14 22:43 hexoBlog.tar.gz
② 不解壓查看壓縮包的內(nèi)容
[root@xiezhr home]# tar ztvf hexoBlog.tar.gz
drwxr-xr-x git/git 0 2022-06-06 20:42 hexoBlog/
drwxr-xr-x git/git 0 2022-02-26 16:43 hexoBlog/page/
drwxr-xr-x git/git 0 2022-06-03 14:57 hexoBlog/page/3/
-rw-r--r-- git/git 56427 2022-06-03 14:57 hexoBlog/page/3/index.html
drwxr-xr-x git/git 0 2022-06-03 14:57 hexoBlog/page/2/
-rw-r--r-- git/git 63574 2022-06-03 14:57 hexoBlog/page/2/index.html
drwxr-xr-x git/git 0 2022-06-03 14:57 hexoBlog/contact/
-rw-r--r-- git/git 43745 2022-06-03 14:57 hexoBlog/contact/index.html
-rw-r--r-- git/git 4169 2022-06-03 14:57 hexoBlog/baidusitemap.xml
drwxr-xr-x git/git 0 2022-06-03 14:57 hexoBlog/movies/
-rw-r--r-- git/git 43246 2022-06-03 14:57 hexoBlog/movies/index.html
drwxr-xr-x git/git 0 2021-02-13 19:32 hexoBlog/images/
-rw-r--r-- git/git 570852 2020-11-29 16:42 hexoBlog/images/0.jpg
-rw-r--r-- git/git 133945 2021-02-13 19:32 hexoBlog/images/2020年年終總結(jié).jpg
drwxr-xr-x git/git 0 2022-06-06 20:42 hexoBlog/archives/
-rw-r--r-- git/git 97587 2022-06-03 14:57 hexoBlog/archives/87a8e6ef.html
③ 解壓文件到指定目錄
[root@xiezhr home]# tar -zxvf hexoBlog.tar.gz -C /home/test/
hexoBlog/
hexoBlog/page/
hexoBlog/page/3/
hexoBlog/page/3/index.html
hexoBlog/page/2/
hexoBlog/page/2/index.html
hexoBlog/contact/
hexoBlog/contact/index.html
hexoBlog/baidusitemap.xml
hexoBlog/movies/
hexoBlog/movies/index.html
hexoBlog/images/
hexoBlog/images/0.jpg
hexoBlog/images/2020年年終總結(jié).jpg
hexoBlog/archives/
hexoBlog/archives/87a8e6ef.html
[root@xiezhr home]# cd /home/test/
[root@xiezhr test]# ll
total 1
drwxr-xr-x 30 git git 4096 Jun 6 20:42 hexoBlog
④ 排除指定文件后打包
# hexoBlog/books 目錄下的文件將被排除后打包
[root@xiezhr home]# tar zcvf hexoBlog.tar.gz hexoBlog --exclude=hexoBlog/books
hexoBlog/
hexoBlog/page/
hexoBlog/page/3/
hexoBlog/page/3/index.html
hexoBlog/page/2/
hexoBlog/page/2/index.html
⑤ 使用-h參數(shù)打包鏈接文件
[root@xiezhr etc]# tar zcfh local_h.tar.gz ./rc.local
[root@xiezhr etc]# tar tfv local_h.tar.gz
-rwxr-xr-x root/root 749 2022-03-22 09:10 ./rc.local
tar 使用小結(jié)
-
打包一個目錄時,一般需要進入到目錄的上級目錄,然后再執(zhí)行打包命令。解壓時需要非常注意,文件是否會覆蓋原文件
-
打包記憶:**tar zcvf /路徑/籃子.tar.gz 相對路徑/雞蛋 ** 【把雞蛋放到籃子里】
gzip 壓縮或解壓文件
1、簡介
gzip 命令用于將一個大的文件通過壓縮算法變成一個小文件。
注意: gzip 命令不能直接壓縮目錄,如果要壓縮目錄時,先要用tar 將目錄打包成一個文件,然后tar 再調(diào)用gzip壓縮
2、語法格式
gzip [參數(shù)選項] [文件]
3、參數(shù)說明
參數(shù) | 參數(shù)說明 |
---|---|
-d | 解壓文件 |
-v | 顯示命令執(zhí)行的過程 |
-l | 列出壓縮文件的內(nèi)容信息 |
-c | 將內(nèi)容輸出到標準輸出,不改變原始文件 |
-r | 對目錄下的所有文件遞歸進行壓縮 |
-數(shù)字<1-9> | 指定壓縮率,默認是6,值越大壓縮率越高 |
-t | 測試,檢查壓縮文件是否完整 |
4、實踐操作
① 把目錄下的每個文件都單獨壓縮成.gz文件
[root@xiezhr dir3]# ls
a.txt b.txt c.txt money.java test.java test.txt tt.txt user.java xiezhr.java xiezhr.txt
[root@xiezhr dir3]# gzip *.txt
[root@xiezhr dir3]# ls
a.txt.gz b.txt.gz c.txt.gz money.java test.java test.txt.gz tt.txt.gz user.java xiezhr.java xiezhr.txt.gz
② 不解壓顯示壓縮文件信息
[root@xiezhr dir3]# gzip -l xiezhr.txt.gz
compressed uncompressed ratio uncompressed_name
31 0 0.0% xiezhr.txt
③解壓文件,并顯示解壓過程
[root@xiezhr dir3]# ls
a.txt.gz b.txt.gz c.txt.gz money.java test.java test.txt.gz tt.txt.gz user.java xiezhr.java xiezhr.txt.gz
[root@xiezhr dir3]# gzip -dv *.gz
a.txt.gz: 0.0% -- replaced with a.txt
b.txt.gz: 0.0% -- replaced with b.txt
c.txt.gz: 0.0% -- replaced with c.txt
test.txt.gz: 15.9% -- replaced with test.txt
tt.txt.gz: 0.0% -- replaced with tt.txt
xiezhr.txt.gz: 0.0% -- replaced with xiezhr.txt
[root@xiezhr dir3]# ls
a.txt b.txt c.txt money.java test.java test.txt tt.txt user.java xiezhr.java xiezhr.txt
注: 以上實踐我們看到gzip命令在壓縮和解壓文件時,都會把源文件刪除。當然也有其他方法可以解決這個問題,但是使用起來有點麻煩。
zip 打包和壓縮文件
1、簡介
zip 壓縮格式是Windows和Linux通用的壓縮格式。和gzip命令相比,zip命令壓縮文件不會刪除源文件,還可以壓縮目錄。所以相比gzip命令,我們使用zip命令會更多一些
2、語法格式
zip [參數(shù)選項] [文件或目錄]
3、參數(shù)說明
參數(shù) | 參數(shù)說明 |
---|---|
-r | 指定目錄下的所有文件和子目錄一并壓縮 |
-x | 壓縮文件時排除某個文件 |
-q | 不顯示壓縮信息 |
4、實踐操作
① 壓縮文件
[root@xiezhr testdir]# ls
xiezhr.txt
[root@xiezhr testdir]# zip xiezhr.zip xiezhr.txt
adding: xiezhr.txt (deflated 17%)
[root@xiezhr testdir]# ls
xiezhr.txt xiezhr.zip
②壓縮目錄
# 這里只將testdir這個目錄壓縮了,目錄下的文件并沒有壓縮
[root@xiezhr test]# zip testdir.zip ./testdir/
adding: testdir/ (stored 0%)
# 使用-r參數(shù)可以將目錄及目錄下的文件也遞歸壓縮了
[root@xiezhr test]# zip -r testdir.zip ./testdir/
updating: testdir/ (stored 0%)
adding: testdir/xiezhr.txt (deflated 17%)
③ 排除壓縮
# 將abc.txt 文件排除到壓縮之外
[root@xiezhr testdir]# ll
total 8
-rw-r--r-- 1 root root 49 Jun 26 16:01 abc.txt
-rw-r--r-- 1 root root 527 Jun 26 15:50 xiezhr.txt
[root@xiezhr testdir]# cd ..
[root@xiezhr test]# zip -r testdir.zip ./testdir/ -x testdir/abc.txt
updating: testdir/ (stored 0%)
updating: testdir/xiezhr.txt (deflated 17%)xxxxxxxxxx -[root@xiezhr testdir]# lltotal 8-rw-r--r-- 1 root root 49 Jun 26 16:01 abc.txt-rw-r--r-- 1 root root 527 Jun 26 15:50 xiezhr.txt[root@xiezhr testdir]# cd ..[root@xiezhr test]# zip -r testdir.zip ./testdir/ -x testdir/abc.txt updating: testdir/ (stored 0%)updating: testdir/xiezhr.txt (deflated 17%)shell
unzip 解壓zip文件
1、簡介
unzip 命令可以解壓zip格式的文件,一般與zip配套使用
2、語法格式
unzip [參數(shù)選項] [壓縮文件]
3、參數(shù)選項
參數(shù) | 參數(shù)說明 |
---|---|
-l | 在不解壓的情況下顯示壓縮包的內(nèi)容 |
-o | 解壓時不提示是否覆蓋文件 |
-d | 指定解壓目錄 |
-v | 解壓時顯示詳細信息 |
4、實踐操作
①不解壓查看壓縮包里的文件
[root@xiezhr test]# unzip -l testdir.zip
Archive: testdir.zip
Length Date Time Name
--------- ---------- ----- ----
0 06-26-2022 16:01 testdir/
527 06-26-2022 15:50 testdir/xiezhr.txt
--------- -------
527 2 files
② 解壓文件
# 不帶參數(shù)解壓文件時,會提示是否覆蓋文件
# [y]es, [n]o, [A]ll, [N]one, [r]ename
# y--是 n--否 A--所有文件都替換 N--所有文件都不替換 r--重命名
[root@xiezhr test]# unzip testdir.zip
Archive: testdir.zip
replace testdir/xiezhr.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: testdir/xiezhr.txt
# -o 參數(shù),解壓時不提示覆蓋
[root@xiezhr test]# unzip -o testdir.zip
Archive: testdir.zip
inflating: testdir/xiezhr.txt
③ 指定解壓目錄解壓文件
# 將testdir.zip 解壓到指定目錄dir7下
[root@xiezhr test]# mkdir dir7
[root@xiezhr test]# unzip -d /home/test/dir7/ testdir.zip
Archive: testdir.zip
creating: /home/test/dir7/testdir/
inflating: /home/test/dir7/testdir/xiezhr.txt
[root@xiezhr test]# cd dir7/
[root@xiezhr dir7]# ls
testdir
scp 遠程文件復制
1、簡介
scp 命令用于不同主機之間文件復制。scp命令每次都是全量完整復制,因此效率不高,適合第一次復制時候使用,增量復制建議使用rsync命令
2、語法格式
scp [參數(shù)選擇] [用戶@主機1:文件1] [用戶@主機2:文件2]
3、參數(shù)選項
參數(shù) | 參數(shù)說明 |
---|---|
-C | 壓縮傳輸 |
-l | 指定傳輸占用的寬帶,單位Kbit/s |
-P
|
指定傳輸?shù)亩丝谔?/td> |
-p | 傳輸后保留文件原始屬性 |
-q | 不顯示傳輸進度 |
-r | 遞歸復制整個目錄 |
4、實踐操作
① 將本地文件或目錄復制到遠程服務器
[root@xiezhr ~]# scp /home/test/xie.txt 42.192.46.248:/tmp
root@42.192.46.248's password:
② 從遠程服務器將數(shù)據(jù)復制到本地
[root@xiezhr ~]# scp 42.192.21.248:/tmp .
rsync 文件同步工具
1、簡介
rsync 可以實現(xiàn)全量或增量的文件復制的開源工具。 可以在本地計算機與遠程計算機之間,或者兩個本地目錄之間同步文件 。 它也可以當作文件復制工具,替代
cp
和mv
命令
2、語法格式
# 1、本地模式
rsync [參數(shù)選項] [源文件] [目標文件]
# 2、遠程同步模式
# 拉取文件
rsync [參數(shù)選項] [用戶@主機:源文件] [目標文件]
# 推送文件
rsync [參數(shù)選項] [源文件] [用戶@主機:目標文件]
3、參數(shù)選項
參數(shù) | 參數(shù)說明 |
---|---|
-r | 遞歸方式同步目錄 |
-a | 可以替代-r參數(shù),除了可以遞歸同步,還可以同步文件屬性(修改時間、權(quán)限等) |
-n | 不確定 rsync 執(zhí)行后會產(chǎn)生什么結(jié)果,可以先用-n參數(shù)模擬執(zhí)行結(jié)果 |
--delete | 使目標目錄內(nèi)容和源保持目錄一致,刪除不同文件 |
--exclude | 同步時,排除不需要的文件或目錄 |
--include | 同步時,必須包含的文件 |
4、實踐操作
注: 有些Linux系統(tǒng)中可能沒有默認安裝rsync ,我們只需按照如下命令安裝即可
yum -y install rsync
① 將當前目錄下的hexoBlog 同步到 dir7目錄下
[root@xiezhr test]# rsync -r hexoBlog/ dir7/hexoBlog
[root@xiezhr test]# cd dir7/
[root@xiezhr dir7]# ll
total 4
drwxr-xr-x 30 root root 4096 Jun 29 20:54 hexoBlog
② 將當前目錄下的logo.png 和hexoBlog 同步到dir8/test 目錄下
[root@xiezhr test]# rsync -r logo.png hexoBlog/ dir8/test
[root@xiezhr test]# cd dir8/test/
[root@xiezhr test]# ll
total 1420
drwxr-xr-x 2 root root 4096 Jun 29 21:00 about
drwxr-xr-x 6 root root 4096 Jun 29 21:00 archives
drwxr-xr-x 2 root root 4096 Jun 29 21:00 artitalk
注: -r
表示遞歸,即包含子目錄。注意,-r
是必須的,否則 rsync 運行不會成功
③-a 參數(shù),遞歸同步,同步修改時間、權(quán)限等信息
# rsync -a hexoBlog hexoBlog_new
# 目標目錄hexoBlog_new 如果不存在,rsync 會自動創(chuàng)建。
# 執(zhí)行上面的命令后,源目錄hexoBlog被完整地復制到了目標目錄hexoBlog_new下面,hexoBlog_new/hexoBlog的目錄結(jié)構(gòu)。
[root@xiezhr test]# ll
total 16
drwxr-xr-x 2 xiezhr xiezhr 4096 Mar 26 21:46 dir
drwxr-xr-x 30 git git 4096 Jun 6 20:42 hexoBlog
-rw-r--r-- 1 root root 35 May 9 21:36 xie.txt
-rw-r--r-- 1 root root 527 Apr 9 10:55 xiezhr.txt
[root@xiezhr test]# rsync -a hexoBlog hexoBlog_new
[root@xiezhr test]# ll
total 20
drwxr-xr-x 2 xiezhr xiezhr 4096 Mar 26 21:46 dir
drwxr-xr-x 30 git git 4096 Jun 6 20:42 hexoBlog
drwxr-xr-x 3 root root 4096 Jul 2 09:15 hexoBlog_new
-rw-r--r-- 1 root root 35 May 9 21:36 xie.txt
-rw-r--r-- 1 root root 527 Apr 9 10:55 xiezhr.txt
#如果只想同步源目錄 hexoBlog 里面的內(nèi)容到目標目錄hexoBlog_new,則需要在源目錄后面加上斜杠。
[root@xiezhr test]# rsync -a hexoBlog/ hexoBlog_new
#上面命令執(zhí)行后,hexoBlog目錄里面的內(nèi)容,就都被復制到了hexoBlog_new目錄里面,并不會在hexoBlog_new下面創(chuàng)建一個hexoBlog子目錄。
④ -n
參數(shù)模擬執(zhí)行結(jié)果
注: 如果不確定 rsync 執(zhí)行后會產(chǎn)生什么結(jié)果,可以先用-n
參數(shù)模擬執(zhí)行結(jié)果
[root@xiezhr test]# rsync -anv xie.txt testdir
sending incremental file list
xie.txt
sent 49 bytes received 19 bytes 136.00 bytes/sec
total size is 35 speedup is 0.51 (DRY RUN)
# -n參數(shù)模擬命令執(zhí)行的結(jié)果,并不真的執(zhí)行命令。-v參數(shù)則是將結(jié)果輸出到終端,這樣就可以看到哪些內(nèi)容會被同步。
⑤ --delete
參數(shù)的使用,保持源目錄和目標目錄文件一致
[root@xiezhr test]# rsync -av --delete hexoBlog hexoBlog_0702
sending incremental file list
created directory hexoBlog_0702
hexoBlog/
hexoBlog/Staticfile.txt
hexoBlog/baidu_urls.txt
hexoBlog/baidusitemap.xml
hexoBlog/favicon.png
hexoBlog/tags/問卷調(diào)查/index.html
sent 63,638,655 bytes received 8,246 bytes 42,431,267.33 bytes/sec
total size is 63,597,216 speedup is 1.00
注: 默認情況下,rsync 只確保源目錄的所有內(nèi)容(明確排除的文件除外)都復制到目標目錄。它不會使兩個目錄保持相同,并且不會刪除文件。使用--delete
參數(shù),這將刪除只存在于目標目錄、不存在于源目錄的文件。
⑥ --exclude
參數(shù) 的使用,同步時排除某些文件
[root@xiezhr test]# rsync -av --exclude '*.txt' hexoBlog hexoBlog_test
sending incremental file list
created directory hexoBlog_test
hexoBlog/
hexoBlog/baidusitemap.xml
hexoBlog/favicon.png
hexoBlog/favicon1.png
hexoBlog/tags/問卷調(diào)查/index.html
sent 63,638,026 bytes received 8,208 bytes 127,292,468.00 bytes/sec
total size is 63,596,717 speedup is 1.00
注: 上面腳本將hexoBlog 目錄下除了.txt外的文件都同步到了hexoBlog_test 目錄下
⑦--include
參數(shù)的使用,同步時必須包含某些文件
[root@xiezhr test]# rsync -av --include "*.txt" --exclude "*" hexoBlog hexo_xie
sending incremental file list
sent 18 bytes received 12 bytes 60.00 bytes/sec
total size is 0 speedup is 0.00
注:上述命令將hexoBlog目錄下的所有文件排除,但包含txt文件同步到hexo_xie目錄下
⑧遠程同步資源
注: rsync 命令除了可以本地同步之外,還支持遠程同步資源
# 以下命令是將本地資源source 同步到遠程服務器destination
[root@xiezhr test]# rsync -av source/ username@remote_host:destination
# 以下命令是將遠程服務器資源source同步到本地 destination
[root@xiezhr test]# rsync -av username@remote_host:source/ destination
由于博主比較窮,手上只有一臺云服務器,這里就沒法做具體演示了??
⑨增量備份
[root@xiezhr test]# rsync -a --delete --link-dest /compare/path /source/path /target/path
上述命令解釋
-
--link-dest
參數(shù)指定基準目錄/compare/path
-
/source/path
代表源目錄 -
/target/path
代表目標目錄 -
命令的第一次備份時是全量備份,后面 源目錄
/source/path
跟基準目錄/compare/path
進行比較,找出變動的文件,將它們拷貝到目標目錄/target/path
實現(xiàn)增量更新
注: 上面命令可以寫成shell腳本,然后添加到Linux任務調(diào)度里,就實現(xiàn)了增量備份文件的功能。
關(guān)于rsync命令小結(jié)
- rsync 用法很多,所以上面說了好多常用例子,當然還有很多用法沒法一一例舉出來。
- rsync 不僅用于Linux,也同樣可以再Windows上使用,用于文章上關(guān)于Linux的,所以沒列舉Windows上使用方法。感興趣的小伙伴可自行查找資料,或者后面出一篇文章具體介紹
- 關(guān)于rsync命令的用法,阮一峰大佬也寫了一篇文章,講的也非常好。大家可以去看看,文章地址:https://www.ruanyifeng.com/blog/2020/08/rsync.html
這期主要是說了Linux系統(tǒng)文件備份與壓縮相關(guān)命令,大概內(nèi)容就這些了。下期根據(jù)思維導圖就到了 軟件安裝相關(guān) 的命令了。
涉及到的命令有: rpm、 yum兩個命令
這些命令呢比較基礎(chǔ),但也是比較常用比較重要的
敬請期待哦(●’?’●)
專欄目錄:快速上手Linux核心命令專欄目錄文章來源:http://www.zghlxwxcb.cn/news/detail-427806.html
上一篇:快速上手Linux核心命令(七):Linux系統(tǒng)信息相關(guān)命令
下一篇:快速上手Linux核心命令(九):文件備份與壓縮文章來源地址http://www.zghlxwxcb.cn/news/detail-427806.html
到了這里,關(guān)于快速上手Linux核心命令(九):文件備份與壓縮的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!