atorex
asked on
Shell script to extract rar files in severa directories
I'm working on a bash script to unrar 10 to 20 directories containing rare files, I however want all extracted files to remain in the directory where the rar files reside.
I have the below working ok if the directory name is continuos, however if the folder name is for example /rar files directorie1 where there are spaces in the dir name it fails.
Any help would be appreciated!
#!/bin/bash
while
mesg="\n================== ========== ========== ========\n
1.. Unrar all files.\n
2.. Delete rar and sfv files.\n
3.. Exit
\n======================== ========== ========== ==\n
Select: \c"
do
echo -e $mesg
read selection
case $selection in
1)
for f in `find $1 -wholename *.rar`
do
echo "Unpacking in directory: "`dirname $f`
unrar e -inul $f `dirname $f`
done ;;
2)
for g in `find $1 -wholename *.rar`
do
cd `dirname $g`
echo "Deleting in directory: "`dirname $g`
rm *.r?? *.url *.sfv *.nfo imdb.nfo
rm -r Sample/
done ;;
3)
exit;;
esac
done
I have the below working ok if the directory name is continuos, however if the folder name is for example /rar files directorie1 where there are spaces in the dir name it fails.
Any help would be appreciated!
#!/bin/bash
while
mesg="\n==================
1.. Unrar all files.\n
2.. Delete rar and sfv files.\n
3.. Exit
\n========================
Select: \c"
do
echo -e $mesg
read selection
case $selection in
1)
for f in `find $1 -wholename *.rar`
do
echo "Unpacking in directory: "`dirname $f`
unrar e -inul $f `dirname $f`
done ;;
2)
for g in `find $1 -wholename *.rar`
do
cd `dirname $g`
echo "Deleting in directory: "`dirname $g`
rm *.r?? *.url *.sfv *.nfo imdb.nfo
rm -r Sample/
done ;;
3)
exit;;
esac
done
Try the following,
#!/bin/bash
while
mesg="\n==============================================\n
1.. Unrar all files.\n
2.. Delete rar and sfv files.\n
3.. Exit
\n==============================================\n
Select: \c"
do
echo -e $mesg
read selection
case $selection in
1)
for f in `find $1 -wholename *.rar`
do
echo "Unpacking in directory: "`dirname $f`
unrar e -inul $f "`dirname $f`"
done ;;
2)
for g in `find $1 -wholename *.rar`
do
cd "`dirname $g`"
echo "Deleting in directory: "`dirname $g`
rm *.r?? *.url *.sfv *.nfo imdb.nfo
rm -r Sample/
done ;;
3)
exit;;
esac
done
ASKER
I was having issues making it work with your proposal, my scripting skills are limited, however I came up with a hack way of doing it, I'm renaming all directories replacing the spaces with an underscore at the start of my script so now all directories are accessed with no issues.
Thanks for the input.
Regards,
Atorex
Thanks for the input.
Regards,
Atorex
One other thing might fix is the "remove" section where you cd into each directory to remove the old rar files etc.
Your original code will only work if you have the full path (starting with a "/") for each directory. If not, when you cd into the first directory, the second one won't be starting from the right place so will fail.
Fix with either this, to put the "cd" command and "rm" into a subshell (so that the "cd" is forgotten when you exit the subshell):
Your original code will only work if you have the full path (starting with a "/") for each directory. If not, when you cd into the first directory, the second one won't be starting from the right place so will fail.
Fix with either this, to put the "cd" command and "rm" into a subshell (so that the "cd" is forgotten when you exit the subshell):
for g in `find $1 -wholename *.rar`
do
(cd `dirname $g`
echo "Deleting in directory: "`dirname $g`
rm *.r?? *.url *.sfv *.nfo imdb.nfo
rm -r Sample/ )
done ;;
or remember where you started and go back to it after each cd:OLDPWD=`pwd`
for g in `find $1 -wholename *.rar`
do
cd "`dirname $g`"
echo "Deleting in directory: `dirname $g`"
rm *.r?? *.url *.sfv *.nfo imdb.nfo
rm -r Sample/
cd "${OLDPWD}"
done ;;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help
Alternatively, surround your `dirname ` with double-quotes or try "$(dirname $g)" as a replacement in your script.