I am trying to make a bash script which will tar all folders individually recursively.
However, I have a problem because some folder names have spaces, etc. So it does not work correctly.
What I have:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in $(ls ./2011)
do
tar -zcvf "$i".tar.gz "$i"
done
IFS=$SAVEIFS
However problems arise for example:
tar -zcvf St Patricks Day Bar Night.tar.gz St Patricks Day Bar Night
The spaces cause problems, what's a good way around this?
Use double quotes around the file name and bash filename expansion instead of ls
.
#!/bin/bash
for i in ./*
do
echo tar -zcvf "$i.tar.gz" "$i"
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With