bashshellubuntu

How to extract multi-layer nested compressed files of multi format


I have some multi-layer organized compressed files, which are compressed in different methods. A sample structure looks like this (non compressed data files are omitted for simplicity).

A.zip
  |---B.zip
  |     |-----C.zip
  |---C.rar
  |---D.tar.gz

How can I extract all files into a new folder and keep this structure unchanged? I have searched and get something like find . -depth -name '*.zip' -exec /usr/bin/unzip -n {} \; -exec rm {} \;, but it only can uncompress *.zip.


Solution

  • You can use that pattern, once for each file type:

    find . -depth -name '*.zip' -exec unzip -n {} \; -delete
    find . -depth -name '*.tar.gz' -exec tar zxf {} \; -delete
    find . -depth -name '*.rar' -exec unrar x {} \; -delete
    

    Granted, this solution traverses the target path as many times as you have file types, but it has the benefit that it's simple.

    You could use a single find to process all file types, but readability will suffer a bit:

    find . -depth \( -name '*.zip' -exec unzip -n {} \; -delete \) \
               -o \( -name '*.tar.gz' -exec tar zxf {} \; -delete \) \
               -o \( -name '*.rar' -exec unrar x {} \; -delete \)