[英]In linux, how can I zip specific sub directories into their own zip files named parent directory name and output them all into a single directory?
所以我已經看到類似的問題回答了我的部分問題,但沒有達到我需要的具體水平。 這是我正在嘗試做的事情的總體要點。
我正在將一堆網站遷移到新服務器。 目前,它們都位於/srv/www/
每個站點在域之后的該目錄名稱中都有自己的文件夾,例如/srv/www/domain1.com
, /srv/www/domain2.com
。
並且在每個domain.com
目錄中都有名為html
的公共 web 目錄。
What I want to do is cd into /srv/www
then run a command or bash script that will recurrsivly go into each domain.com/html
directory and zip the contents of that directory, then output it as a zip name domain.com.zip
在/srv/web_backup/domain.com.zip
中。 同樣,我只想要內容。 當我解壓縮文件時,我不希望它 output 到html/{contents}
。
在這種情況下, domain.com
就是一個示例,但每個文件夾都有自己的名稱。
如果我手動執行此操作,一旦我 cd 進入/srv/www/domain.com/html
,我將運行命令zip -r /srv/web_backup/domain.com.zip. .htaccess
zip -r /srv/web_backup/domain.com.zip. .htaccess
這就是我想要運行以獲取所有文件、目錄和 .htaccess 文件的內容。
總而言之,我想單獨 zip 每個/srv/www/{domain.com}/html
的內容,而不是目錄,並將它們命名為{domain.com}.zip
( html
文件夾的父目錄)然后將這些 zip 文件保存到/srv/web_backup/domain.com.zip
我很感激任何幫助,因為我不太謙虛地承認這超出了我的 Linux 知識或能力。
我想你已經描述了這些步驟。 您只需要將它們放入腳本中。 就像是:
#!/bin/bash
cd /srv/www
for domain in *; do
(
cd $domain/html &&
zip -r /srv/web_backup/$domain.zip . .htaccess
)
done
稍微分解一下:
# change into the /srv/www directory
cd /srv/www
# for each domain in this directory (we're largely assuming here that
# this directory contains only directories representing domains you
# want to backup).
for domain in *; do
# a (...) expression runs the included commands in a subshell, so
# that when we exit we're back in /srv/www.
(
# change into the html directory and, if that was successful, update
# your zip file.
cd $domain/html &&
zip -r /srv/web_backup/$domain.zip .
)
done
這只是解決這個特殊問題的一種方法。 你也可以這樣寫:
#!/bin/sh
find /srv/www/* -maxdepth 0 -type d -print0 |
xargs -0 -iDOMAIN sh -c 'cd "DOMAIN/html" && zip -r "/srv/www/DOMAIN.zip" .
這大致相同,但巧妙地使用了find
和xargs
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.