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