Search code examples
bashshellzshunzip

How can I extract an archive with the archive's name as a directory to a path?


I found this command. for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done

I want something similar to this, but I want to extract my zip files from my ~/Downloads folder to ~/Documents.

So.

- Downloads
    - zip1.zip
    - zip2.zip
    - zip3.zip

- Documents
    - zip1/data.txt
    - zip2/data.txt
    - zip3/data.txt

Please, help!


Solution

  • Just prefix the path your provide in the -d flag with the Documents directory:

    for f in *.zip; do unzip -d ~/Documents/"${f%*.zip}" "$f"; done
    

    Note that the script still needs to be executed inside your downloads directory.