LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-19-2009, 02:45 PM   #1
truxntrax
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Bash script help required - move uncompress and rename


Hi all,

Done some searches but can't find a script that does this or nearly does this for me to "borrow".

My requirement is as follows. I want to search a directory recursively looking for new .rar/ .zip files. When a new file is found I want to extract the contents to another directory. To top things off would like to rename the source file as something like original.rar.extracted.

Many thanks if anyone could start me in the right direction!

All the best.

Trux

To add: using ubuntu netbook remix - jaunty.

Last edited by truxntrax; 09-19-2009 at 02:47 PM. Reason: More details
 
Old 09-19-2009, 03:11 PM   #2
platinumedge
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
this might help you

Hi this is my first post....

For extracting the rar/zip must have to install rarlinux
and use unrar command in your shell script.
for details on unrar command
I hope this will help you
 
Old 09-19-2009, 03:35 PM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
In additiont to the unrar platinumedge mentioned, you'll want unzip, of course. Also, you'll want the find command for recursive directory search with time constraints. You'll need mv for the rename. As far as the '.extracted' thing, I'm not sure why that'd be necessary, but unzip's -d flag may be what you want, but I'm not very familiar with rar. The man pages for these tools and your shell should have all the details you need.
 
Old 09-19-2009, 05:37 PM   #4
truxntrax
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Hi thanks for reply's. I understand which tools I need and how to do this without a script but how do I write this into a bash script which runs recursively?

The renaming is to ensure when the script checks the directory n minutes later the same file will not be unrared/ unzipped again. I will also be able to see which files the script has successfully run against.

Thanks again!

Trux
 
Old 09-19-2009, 06:24 PM   #5
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,203

Rep: Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472Reputation: 2472
Quote:
Originally Posted by truxntrax View Post
Hi thanks for reply's. I understand which tools I need and how to do this without a script but how do I write this into a bash script which runs recursively?

The renaming is to ensure when the script checks the directory n minutes later the same file will not be unrared/ unzipped again. I will also be able to see which files the script has successfully run against.

Thanks again!

Trux
I suggest you take a look at the man pages for find, and check out Google for any one of the many bash scripting tutorials.

But if you already know how to do it from the command line, you already know how to write the script. Essentially, a script file is the same thing as a Windows batch file...just a bunch of commands, in a text file.

This:
Code:
#!/bin/bash
<your command one>
...
...
...
<end of your commands>
is a script. Obviously, put your commands in it... The #!/bin/bash executes a new child shell, which is handy to make sure your environment variables and such get passed. You can create this in any editor (vi, emacs, gedit, etc.), and save it as plain text. After, do a "chmod 755" on that file name (read manpage for the chmod command), to make it an executable program. After that, run it with "./<filename>".
 
Old 09-19-2009, 06:58 PM   #6
platinumedge
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
as TBOne said for a recursive directory traversal you need to use find command

here is some sample script code that might help you...

for fname in $(find <your fullpath to search> -name "*.rar" -print0)
do
.....your code for unrar the files.....

done

for fname in $(find <your fullpath to search> -name "*.zip" -print0)
do
.....your code for unzip the files.....

done

here fname is a temp variable used to store a single filename

you have to rename that file after the extract operation complete as
mv $fname $fname.extracted


I think this will do the trick

Last edited by platinumedge; 09-19-2009 at 11:17 PM.
 
Old 09-19-2009, 07:08 PM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
I wouldn't run my scripts as './script' unless for a throwaway. I'd recommend creating a personal scripts directory and putting it on your path so you can execute your frequently used scripts like any other command.

He may not need a loop at all - find's '-exec' flag could do.

And, truxntrax, you can specify to find that it only find files of a certain age, so it won't unpack anything older, so you shouldn't need the renaming to protect against multiple unpacks. As far as seeing what was done, you could just put some logging in your script. I dunno - it's up to you and people almost never completely specify what they're trying to accomplish, so whatever works for you.
 
Old 09-20-2009, 02:40 PM   #8
truxntrax
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Wow - thanks for all the help.

You are correct I can do this all in command line it's just stringing things together into a script. I do not understand the syntax.

Got enough to play with from your suggestions.

Being an IT programme manager I should know better I did ask how to achieve a solution rather than telling you my requirements. Anyway I've got enough to get me started.

Thanks for going easy on a newby.

Trux
 
Old 09-21-2009, 07:54 PM   #9
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,232

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
You'll find this useful:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://rute.2038bug.com/index.html.gz

Also, I'd have an archive dir and mv the orig zip file to that after processing. Always keep the input dir clear.
You might want to embed the date/time of processing into the archived filename.
 
  


Reply

Tags
bash, script, shell, unrar, unzip


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Please, need a bash script move... kukata Programming 3 05-06-2009 11:45 AM
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 12:01 PM
Bash script: Move files, ignore case Fredde87 Linux - Software 3 09-03-2008 03:34 AM
Bash development - move chmod options to bash jhwilliams Programming 9 06-26-2007 12:13 PM
After a piece of software/a bash script to mass-move files [Fedora Core 6] Alux Linux - Desktop 3 11-23-2006 02:33 PM


All times are GMT -5. The time now is 02:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration