簡體   English   中英

在linux中,我怎樣才能把zip具體的子目錄改成自己的zip文件名,把父目錄名和Z78E6221F6393D1356DZF2Z文件全部改成a393D1356DZF21的單目錄?

[英]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}.ziphtml文件夾的父目錄)然后將這些 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" .

這大致相同,但巧妙地使用了findxargs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM