LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script help required - move uncompress and rename (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-help-required-move-uncompress-and-rename-756434/)

truxntrax 09-19-2009 02:45 PM

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.

platinumedge 09-19-2009 03:11 PM

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

slakmagik 09-19-2009 03:35 PM

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.

truxntrax 09-19-2009 05:37 PM

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

TB0ne 09-19-2009 06:24 PM

Quote:

Originally Posted by truxntrax (Post 3690044)
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>".

platinumedge 09-19-2009 06:58 PM

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

slakmagik 09-19-2009 07:08 PM

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. ;)

truxntrax 09-20-2009 02:40 PM

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

chrism01 09-21-2009 07:54 PM

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.


All times are GMT -5. The time now is 07:03 PM.