Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip files in Linux using bash cause errors when path contains spaces

Tags:

linux

bash

unzip

File path like this: path/path/path/File name 2.3.pdf.zip

What am I doing wrong:

# unzip files back to normal
# and remove zip files
for f in `find "$1" -type f -iname "*.zip"`; do
        dir=`dirname "$f"`
        unzip -o "$f" -d "$dir"
        rm -f "$f"
done

Error message: unzip: cannot find or open file, file.zip, or file.ZIP

Using UnZip 5.52 Red Hat Enterprise Linux Server release 5.10 (Tikanga)

like image 221
Marvado Avatar asked Jan 26 '26 16:01

Marvado


1 Answers

I think your loop is splitting up the output of find based on the spaces. You may want to do something to read one line at a time, like this

find "$1" -type f -iname "*.zip" | while read f
do
    dir=`dirname "$f"`
    unzip -o "$f" -d "$dir"
    rm -f "$f"
done

Or, alternatively, you could set IFS:

IFS='\n'
for f in `find "$1" -type f -iname "*.zip"`; do
        dir=`dirname "$f"`
        unzip -o "$f" -d "$dir"
        rm -f "$f"
done
like image 51
Aaron Okano Avatar answered Jan 28 '26 08:01

Aaron Okano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!