LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums HCL Reviews Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2008, 05:28 AM   #1
quackyo
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Rep: Reputation: 0
shellscript to extract multiple archives


I have a drive with multiple directories on, all of them containing rar files.

I want to batch-decompress them all into the directory the archives are in now. So I want something that finds a rar file, say /home/xx/movie1/file.rar

then do a "unrar x /home/xx/movie1/file.rar /home/xx/movie1/"


Unfortunately I'm not a programmer so I have only thought this through. Can somebody finish this for me?

find -name *.rar (should this be something that allows all cases (i.e. *.Rar *.RAR)) | unrar x "input from find" "input directory"
 
Old 01-13-2008, 06:18 AM   #2
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 542

Rep: Reputation: 33
#!/bin/sh
for i in `ls -1 /path/to/yourdir | grep -i .rar`; do
rar -x $i
done

or

#!/bin/sh
for i in `ls -1 $1 | grep -i .rar`; do
rar -x $i
done

or

#!/bin/sh
find "$1" -name `ls -1 $1 | grep -i .rar` -exec rar -x '{}' \;



for the latters you must specificy the path when you launch the script ie
my untar_script.sh /path/to/yourdir



They should work if your filenames has no white spaces..otherwise things are a little more complicate
Test them first....

ciao

Last edited by urka58; 01-13-2008 at 06:21 AM.
 
Old 01-13-2008, 06:46 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by urka58 View Post
#!/bin/sh
find "$1" -name `ls -1 $1 | grep -i .rar` -exec rar -x '{}' \;
Hmmm... sorry urka58 but I think it does not work in the way the OP was looking for, e.g. it does not extract files in the directory where the archives reside. Keep it simple and do something like this
Code:
for archive in $(find . -iname "*.rar")
do
  path_to_extract=$(dirname $archive)
  unrar e $archive $path_to_extract > /dev/null
done
the test -iname (of the command find) matches entries in a case-insensitive way. The command dirname extract the path from the filename (actually it strips non-directory suffix from the filename). Finally unrar the archive specifying path_to_extract as last argument.

As urka58 correctly suggested, you may encounter problem if file names contain blank spaces. In this case a little workaround is needed.
 
Old 01-13-2008, 07:09 AM   #4
quackyo
LQ Newbie
 
Registered: Aug 2005
Posts: 16

Original Poster
Rep: Reputation: 0
Thanx, the last one does work fine, but as you said there is problems with blank spaces.. How do I fix that ?
 
Old 01-13-2008, 07:41 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
for archive in *.rar
do
  unrar e "$archive" "${archive%/*}" > /dev/null
done
if you want to use find in the for loop, put double quotes when you call the "archive" variable

Last edited by ghostdog74; 01-13-2008 at 07:57 AM.
 
Old 01-13-2008, 07:58 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
ghostdog74, you sure? On my system "for archive in *.rar" does not recurses subdirectories. I'd rather do something a little more tricky like this
Code:
#!/bin/bash
archives=$(find . -iname "*.rar" -printf "%p@")
IFS="@"
for archive in $archives
do
  path_to_extract=$(dirname $archive)
  unrar e $archive $path_to_extract > /dev/null
done
@quackyo: here the trick is to use an Input Field Separator different from the default one (any of blank space, tab or newline). I choosed "@" assuming you don't have any file or directory names containing "@". In this way, names containing one or more blank spaces are not splitted inside the for loop.
 
Old 01-13-2008, 08:24 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
ghostdog74, you sure? On my system "for archive in *.rar" does not recurses subdirectories. I'd rather do something a little more tricky like this
Code:
#!/bin/bash
archives=$(find . -iname "*.rar" -printf "%p@")
IFS="@"
for archive in $archives
do
  path_to_extract=$(dirname $archive)
  unrar e $archive $path_to_extract > /dev/null
done
no, it does not recurse directories, my bad for misinterpreting the requirement. however that's trivial to do.
Quote:
@quackyo: here the trick is to use an Input Field Separator different from the default one (any of blank space, tab or newline). I choosed "@" assuming you don't have any file or directory names containing "@". In this way, names containing one or more blank spaces are not splitted inside the for loop.
one way to go around that is include double quotes, at least it works for me eg
Code:
for files in "$(find . -iname  '*.rar')"; do echo "$files"; done
 
Old 01-13-2008, 09:26 AM   #8
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
With zsh:

Code:
for f (**/*.rar) rar x  $f
Otherwise:

Code:
find . -name '*.rar' -exec rar x {} \;
 
Old 01-13-2008, 09:30 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
for files in "$(find . -iname '*.rar')"; do echo "$files"; done
Yes, it works for me too. But the loop will run once and the unrar command will fail, since all the arguments will be on the same (unique) command line, even if filenames are separated by a newline. E.g. the following lines will stay all together as one block
Code:
/path/to/file_one.rar
/path/to/subdir/file with spaces.rar
/path/to/subdir/file_two.rar
This is actually one of the reasons why all the *nix guides advise against using blank spaces in filenames! Ciao
 
Old 01-13-2008, 10:09 AM   #10
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 542

Rep: Reputation: 33
@colucix

yes, you're right, I did not consider the output dir, but on the solution you proposed the imput dir is missing (@least not specified).
find . searches in $PWD which not necessarely is /path/to/rar_files..
A cd /path/to/rar_files can easily fix it

Ciao
 
Old 04-26-2009, 03:31 PM   #11
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Rep: Reputation: 45
I am trying to use the info here to allow for white spaces in a multiple rar extract. I would want to manually state the output directory (I've called the file multiple-extract).

Code:
for archive in $(find . -iname "*.rar")
do
  unrar e "$archive" "$1"/ 
done
I have even tried using single quotes against $archive. so the above works with rar files with no white spaces. I can do one of the below for it to work:

Code:
multiple-extract .
multiple-extract temp\ dir
multiple-extract "temp dir"
When I try with white spaces I get
Code:
[root@localhost test]# ls
evo 1.rar  opera 1.rar
[root@localhost test]# multiple-extract .

UNRAR 3.71 beta 1 freeware      Copyright (c) 1993-2007 Alexander Roshal

Cannot open $archive.rar
No such file or directory
No files to extract

UNRAR 3.71 beta 1 freeware      Copyright (c) 1993-2007 Alexander Roshal

Cannot open $archive.rar
No such file or directory
No files to extract

UNRAR 3.71 beta 1 freeware      Copyright (c) 1993-2007 Alexander Roshal

Cannot open $archive.rar
No such file or directory
No files to extract

UNRAR 3.71 beta 1 freeware      Copyright (c) 1993-2007 Alexander Roshal

Cannot open $archive.rar
No such file or directory
No files to extract
 
Old 04-26-2009, 09:14 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use a while read loop
Code:
find .... | while read FILE
do
 unrar ..... "$FILE" ...
done
 
Old 04-26-2009, 10:33 PM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
I'm not certain if this will work, but try
find /path/to/dir -iname "*.rar" -execdir unrar x '{}' \;

The -execdir option may execute the unrar command in the directory of the file found.

You might want to test if using -okdir unrar x '{}' \; first. If it fails on the first match, you can abort.
 
Old 04-27-2009, 06:39 AM   #14
Jongi
Senior Member
 
Registered: Aug 2003
Distribution: Debian Sid 32/64-bit, F10 32/64-bit
Posts: 1,070

Rep: Reputation: 45
Quote:
Originally Posted by ghostdog74 View Post
use a while read loop
Code:
find .... | while read FILE
do
 unrar ..... "$FILE" ...
done
yep that did the trick. thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"You don't have the right permissions to extract archives in the folder " please help redhatman13 Linux - Software 18 10-15-2008 06:52 AM
Zip multiple directories to seperate archives PlymWS Linux - Software 3 06-16-2007 05:40 AM
Help with winrar - adding multiple files into separate archives some_random_dude Linux - Software 4 02-04-2007 12:05 PM
How to Unrar/unzip multiple archives at once containing a single file? SentralOrigin Linux - Software 11 11-30-2006 06:57 PM
untarring archives that span multiple tapes? BrianK Linux - General 2 11-29-2005 10:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration