Issue
I have an old script where I extract the contents of archives into a new directory with the name of the archives. I need to simplify this as I find it inefficient since the commands run regardless. I'm not an expert with bash, and this worked for a while, but has become unbearable of late.
I get constant errors because the directory is already created or is not a unzip/unrar archive. I don't know how to check if the file is unrar or zip format prior to starting the script so I don't know how to put together a proper if/else for loop. I'm no expert with bash and this is just a small portion of a much larger script all written in bash years ago.
for x in $(find -name '*.cbr'); do dir=${x%%.cbr}; mkdir "$dir"; unzip -d "$dir" $x; done
for x in $(find -name '*.cbr'); do dir=${x%%.cbr}; mkdir "$dir"; unrar e $x "$dir"; done
for x in $(find -name '*.cbz'); do dir=${x%%.cbz}; mkdir "$dir"; unzip -d "$dir" $x; done
for x in $(find -name '*.cbz'); do dir=${x%%.cbz}; mkdir "$dir"; unrar e $x "$dir"; done
Solution
Thanks for the feedback, I ended up doing something else.
for x in $(find -name '*.cbr' -o -name '*.cbz');
do dir=${x%.*};
mkdir "$dir";
unrar e $x "$dir" || unzip -d "$dir" $x;
done
Which is what I was looking to do, only attempt to create the directory once, and make a choice between unrar or unzip. Also, far more clean than what i previously had.
Answered By - superuser-Miguel Answer Checked By - Pedro (WPSolving Volunteer)