Mailinglist Archive: opensuse (1599 mails)

< Previous Next >
RE: [opensuse] Bash Guru help needed, recursively extract rarred files
  • From: Philip Dowie <philip.dowie@xxxxxxxxxxxxxxxxxxxxxx>
  • Date: Tue, 13 Oct 2009 11:27:10 +1300
  • Message-id: <E50D0676A6F98941BC86ED082F8F2AA43634F89F39@xxxxxxxxxxxxxxxxxxxxxxxxxxx>

Hm, ok, something like this then

for clientdir in /backup/*; do
cd $clientdir
for daydir in $clientdir/*; do
rarfile=$daydir/*.rar
if [ ! -e $rarfile ]; then
rarfile=$daydir/*.r01
fi
unrar x $rarfile
[ $? -eq 0 ] && rm -rf $daydir
done
done

Anders

Thanks, that works.

if there is more than one rar file in $daydir, then you are going to run into
problems - rarfile will be a space separated list of all the files, and the if
will fail because of too many arguments.

Also, assuming there is no rar file, then $rarfile will be $daydir/*.rar
(literal), so the test for its existance will fail, and rarfile will become
either
a) the name of the only .r01 file if there is only one
b) space separated list of all the .r01 files (if there are more than one)
c) $daydir/*.r01 (literal) if there are none.

You probably want something more like this:

for clientdir in /backup/*; do
cd "${clientdir}"
for daydir in "${clientdir}"/*; do
for rarfile in "${daydir}"/*.{rar,r01}; do
unrar x "${rarfile}" && rm -rf "${daydir}"
done
done
done

This assumes that where you have .rar files and .r01 files, you want to unrar
them both. If you want something else, then you'll need something different
to check for both a .rar and a .r01 file.

Personally, I'd break out sed, but then the
unnecessary-invocations-of-external-command police would scold me for not using
the shell equivalent...


for clientdir in /backup/*; do
cd "${clientdir}"
for daydir in "${clientdir}"/*; do
for rarfile in "${daydir}"/*.{rar,r01}; do
rarname=`echo "${rarfile}" | sed -re 's/(.*)\.rar/\1/'`
[ ! -e "${rarname}".r01 ] && unrar x "${rarfile}" && rm -rf "${daydir}"
done
done
done

That will not unrar a rar file if a r01 file exists with the same first part of
the name. but it is guilty of that unnecessary invocation of sed.
something like

rarname="${rarfile/.rar/}".r01

might suit. BASH gurus will confirm or shoot me down..


Phil
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >