从子文件夹、ZIP 和 RAR 复制所有文件,而不覆盖目标文件夹中同名的文件
Copy all files from subfolders, ZIP's and RAR's, without overwriting files with same name in destination folders
我想知道是否可以在不覆盖不同文件夹中同名文件的情况下提取子文件夹、zip 和 rars 中的文件。
示例文件夹:
TargetFolder
|
|
-- FolderOne
|
--- PDF.PDF
--- ZIP.ZIP <- FileInsideZIP.docx
|
-- FolderTwo
|
--- PDF.PDF
--- NestedFolder
|
--- SomeFile.txt
--- OtherFile.xls
期望输出:
FinalFolder
|--- PDF.PDF
|--- FileInsideZip.docx
|--- PDF (2).PDF
|--- SomeFile.txt
|--- OtherFile.xls
目前我正在使用以下命令行:
find TargetFolder -type f -exec cp --backup=numbered \{\} FinalFolder \;
这很好,但不幸的是,此命令无法获取 zip 中的文件,有时我在 zip 中有 zip,等等。所以,我需要一个更好的方法,因为目前我正在浪费时间.
谢谢。
这里有一个方法(警告:未经测试):
backup_decompress_recursive () {
# is set to TargetFolder by usage backup_decompress_recursive "TargetFolder"
# find and backup up all non-zip, non-rar files (based on @joao's code)
find "" -type f -not -iname "*.zip" -and -not -iname "*.rar" -exec cp --backup=numbered "{}" FinalFolder \;
# make and go into a temporary folder (for storing decompressed contents of zip and rar files)
tmp=`mktemp -d`
cd $tmp
# now find all zip or rar and pipe names to a while loop
find "" -type f -iname "*.zip" -or -iname "*.rar" |
while read z; do
# $z is one zip or rar
# make a temp dir for its contents and go in
mkdir "$z".d
cd "$z".d
# unzip or unrar depending on whether $z ends in ".zip" or ".rar"
if [[ "$z" =~ \.zip$ ]]; then
unzip "$z"
elif
unrar x "$z"
fi
cd ..
done
# do this whole process again, recursively, on $tmp
# ($tmp contains all the inner tempdirs as well)
# this handles file inside of rar inside of zip inside of zip inside of....
backup_decompress_recursive $tmp
# cleanup
rm -rf $tmp
}
# ignore case to handle ".ZIP", ".ziP", etc...
shopt -s nocasematch
backup_decompress_recursive "TargetFolder"
# stop ignoring case
shopt -u nocasematch
我想知道是否可以在不覆盖不同文件夹中同名文件的情况下提取子文件夹、zip 和 rars 中的文件。
示例文件夹:
TargetFolder
|
|
-- FolderOne
|
--- PDF.PDF
--- ZIP.ZIP <- FileInsideZIP.docx
|
-- FolderTwo
|
--- PDF.PDF
--- NestedFolder
|
--- SomeFile.txt
--- OtherFile.xls
期望输出:
FinalFolder
|--- PDF.PDF
|--- FileInsideZip.docx
|--- PDF (2).PDF
|--- SomeFile.txt
|--- OtherFile.xls
目前我正在使用以下命令行:
find TargetFolder -type f -exec cp --backup=numbered \{\} FinalFolder \;
这很好,但不幸的是,此命令无法获取 zip 中的文件,有时我在 zip 中有 zip,等等。所以,我需要一个更好的方法,因为目前我正在浪费时间.
谢谢。
这里有一个方法(警告:未经测试):
backup_decompress_recursive () {
# is set to TargetFolder by usage backup_decompress_recursive "TargetFolder"
# find and backup up all non-zip, non-rar files (based on @joao's code)
find "" -type f -not -iname "*.zip" -and -not -iname "*.rar" -exec cp --backup=numbered "{}" FinalFolder \;
# make and go into a temporary folder (for storing decompressed contents of zip and rar files)
tmp=`mktemp -d`
cd $tmp
# now find all zip or rar and pipe names to a while loop
find "" -type f -iname "*.zip" -or -iname "*.rar" |
while read z; do
# $z is one zip or rar
# make a temp dir for its contents and go in
mkdir "$z".d
cd "$z".d
# unzip or unrar depending on whether $z ends in ".zip" or ".rar"
if [[ "$z" =~ \.zip$ ]]; then
unzip "$z"
elif
unrar x "$z"
fi
cd ..
done
# do this whole process again, recursively, on $tmp
# ($tmp contains all the inner tempdirs as well)
# this handles file inside of rar inside of zip inside of zip inside of....
backup_decompress_recursive $tmp
# cleanup
rm -rf $tmp
}
# ignore case to handle ".ZIP", ".ziP", etc...
shopt -s nocasematch
backup_decompress_recursive "TargetFolder"
# stop ignoring case
shopt -u nocasematch