Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

Linux - How to zip files per subdirectory separately

I have directory structure like this.

enter image description here

From this I want to create different zip files such as

data-A-A_1-A_11.zip

data-A-A_1-A_12.zip

data-B-B_1-B_11.zip

data-C-C_1-C_11.zip

question from:https://stackoverflow.com/questions/65887193/linux-how-to-zip-files-per-subdirectory-separately

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
while read line;
do 
   echo "zip -r ${line////-}.zip $line";
   # zip -r "${line////-}.zip" "$line"
done <<< "$(find data -maxdepth 3 -mindepth 2 -type d)"

Redirect the result of a find command into a while loop. The find command searches the directory data for directories only, searching 3 directories deep only. In the while loop with use bash expansion to convert all forward slashes to "-" and add ".zip" in such a way that we can build a zip command on each directory. Once you are happy that the zip command looks fine when echoed for each directory, comment in the actual zip command


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...