Register
It is currently Wed Oct 22, 2014 9:02 pm

script to auto-extract compressed files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Mon Jul 11, 2011 11:32 am   

Joined: Mon Jul 11, 2011 11:24 am
Posts: 5
i have been using transmission for torrents downloading , many of which are compressed into many parts ..
i am total newbie but i wrote a script to check sfv file if it's ok before extracting the rar files , it's working fine but i need the script to execute through all the subdirectories not only the main one, i hope you could help
Code:
#!/bin/bash
#checking if sfv file exists in the first place
if [ -f *.sfv ]; then
  echo "one file has been found, it's gonna be checked against rars"
#storing the output into temporary file (tmp) 
cksfv -q -c -f *.sfv>tmp
#using grep confirm all files are downloaded and sfv file was successfully checked
  sfvfine=`cat tmp|grep "Everything OK" `
  echo $sfvfine 
    if [ $sfvfine=="Everything OK" ]; then
     unrar e *.r**
   fi
fi


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 11:52 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
http://linux.die.net/man/1/cksfv

Code:
#!/bin/sh

if ! cksfv -r -c | grep -q 'Errors Occured'; then
    find . -type f -name '*.rar' -exec unrar x {} +
fi


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 12:41 pm   

Joined: Mon Jul 11, 2011 11:24 am
Posts: 5
thanx jsz, i didn't know of this option, I made things harder than they should be,,,
one thing left, the extraction occurs at the directory from which the script was executed , i would like files to be extracted to their corresponding directories , to avoid any conflict in case files names are identical ,, :)


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 12:44 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
I edited my post before you responded. : -)


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 1:59 pm   

Joined: Mon Jul 11, 2011 11:24 am
Posts: 5
I noticed,
thanx but the script not working as intended
Extracting from ./ll/unrar_clean.sh.rar
============
OK , finally i figured it out :))

Code:
if ! cksfv -r -c | grep -q 'Errors Occured'; then
    for f in $(find ./ -name "*.rar");do unrar e $f ${f%/*};done
fi


thank you


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 3:00 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
It's because the {} + syntax makes find(1) executes only one instance of unrar(1) and treat every file for once, but since unrar(1) also accepts output directories, you can't rely on this -exec syntax.
So you have to replace the find(1) command by this:

Code:
find . -type f -name '*.rar' -exec unrar x {} \;


I don't get why you're using the e option of unrar(1) since you said:
abjdiat wrote:
i would like files to be extracted to their corresponding directories

and the man page says:
Code:
e      Extract files to current directory.
x      Extract files with full path.


Also, you should NOT use this for syntax. ( e.g., « for x in $(command) » )
for is used to iterate arguments, not strings, neither outputs.

And always quote your parameter expansions ( i.e., « "$f" » and « "${f%/*}" » ) in case the variable contains spaces, newlines... or funny characters.


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 3:15 pm   

Joined: Mon Jul 11, 2011 11:24 am
Posts: 5
Code:
find . -type f -name '*.rar' -exec unrar x {} \;

using this code it worked like charm..
Quote:
I don't get why you're using the e option of unrar(1) since you said:

i used -e option because i was intending to specify the output directory, i tried using x option first , but it gave me "no file to extract " error

Quote:
Also, you should NOT use this for syntax. ( e.g. « for x in $(command) » )
for is used to iterate arguments, not strings, neither outputs.

And always quote your parameter expansions ( i.e. « "${f%/*}" » ) in case the variable contains spaces, newlines... or funny characters.

you are right, thanx for the tips and correcting my mistakes
I don't pay much attention to following proper syntax but some of I've used i saw in linux forums ,but i will try to avoid making same mistakes again

regarding quoting parameter expansions , i followed this guide http://tldp.org/LDP/abs/html/string-manipulation.html , no suggestions were made about it , but i think it's safer to quote special characters


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 3:22 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
The TLDP bash guide is outdated, deprecated, and sometimes just plain wrong.
Consider this one instead: http://mywiki.wooledge.org/BashGuide . You won't regret it. ; -)

Concerning the same subject: http://mywiki.wooledge.org/BashFAQ/100 -- How do I do string manipulations in bash?


Top
 Profile  
 PostPosted: Mon Jul 11, 2011 4:10 pm   

Joined: Mon Jul 11, 2011 11:24 am
Posts: 5
jsz wrote:
The TLDP bash guide is outdated, deprecated, and sometimes just plain wrong.
Consider this one instead: http://mywiki.wooledge.org/BashGuide . You won't regret it. ; -)

Concerning the same subject: http://mywiki.wooledge.org/BashFAQ/100 -- How do I do string manipulations in bash?

bookmarked , thanx :) ..


Top
 Profile  
 PostPosted: Tue Jul 12, 2011 3:38 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
jsz wrote:
The TLDP bash guide is outdated, deprecated, and sometimes just plain wrong.

You might report errors to the author. I tried a couple of years ago and he was quite responsive and cooperative.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP