简体   繁体   中英

Copy all files from subfolders, ZIP's and RAR's, without overwriting files with same name in destination folders

I would like to know if is possible to extract files in subfolders, zips and rars without overwrite files with same name in distinct folders.

Example folder:

TargetFolder
|
|
-- FolderOne
   |
   --- PDF.PDF
   --- ZIP.ZIP <- FileInsideZIP.docx
|
-- FolderTwo
   |
   --- PDF.PDF
   --- NestedFolder
       |
       --- SomeFile.txt
       --- OtherFile.xls

Desire output:

FinalFolder
|--- PDF.PDF
|--- FileInsideZip.docx
|--- PDF (2).PDF
|--- SomeFile.txt
|--- OtherFile.xls

Currently i'm using the following command line:

find TargetFolder -type f -exec cp --backup=numbered \{\} FinalFolder \;

Which is good, but unfortunately this command won't get the files inside the zip, and sometimes i have zip inside zips, and so on.. so, i need a better approach because currently i'm losing time.

Thanks.

Here's a way (warning: untested):

backup_decompress_recursive () {
  # $1 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 "$1" -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 "$1" -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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM