Wednesday, 2 February 2011

Howto: Unpack/Extract Files from Multiple Folders in One Go

Just recently I had a situation where I needed to extract multiple archives, roughly 100 in fact, with each being in a sub folder.

Not particularly wanting to sit clicking into each folder, right click the archive and extract each one, then move the file (avi in this case) up into the parent folder I got busy on the terminal. Luckily this is a perfect job for Bash and instead of the job taking around 2hrs of tedious clicking and waiting to extract each I was able to leave the command processing the files and get on with some more interesting work while the job was done.

So here is the command line that go it all sorted.

  cur="`pwd`";for i in `"ls"`; do echo "$i";cd "$i";unrar e "*.rar";mv "*.avi" "$cur"/ ;cd "$cur";done

So its not very elegant, I was going to use "find" but as I wanted to move the avi at the same time, thought this way would be better.

I would be happy for anyone to leave a comment and show me a better way, always eager to learn :]


Dirk said...

This is exactly what I want to do. If I want to delete the .rar file instead of moving the extracted file, how will it look?
I appreciate the help!

Subbass said...

Hi Dirk, I am glad this helped you.

If you don't want to move the file as I did; and instead just delete the rar file then I would change:

mv "*.avi" "$cur"/

to be :

rm "*.rar"

If it is multipart rars then perhaps :

rm "*.r*"

Disclaimer: This is untested, use at own risk.

batis said...

Hi, what if the extension is unknown (it can be anything = infinity) and the we have two extracted files: a big one and a small one... and we want to rename the big one using the folder's name then move it away (somewhere)...
It's not an imaginary situation, it is real xD
Thanks in advance :)

Joseph wills said...

The simplest and easy method I have seen.I used to extract folder by folder.Thanks for sharing..
san diego data recovery