LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-07-2007, 04:58 PM   #1
mstarap
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
Best tools to use for this script? Bash? Perl?


Hi All!

I am having trouble determining which are the best tools to get this job done.

I need to find files in a directory structure older than X days and move those files to another server (leave the files younger than X days). The tricky part is that I need to preserve part of the directory structure. For example:

On server1, the file /data/a/anderson/anderson_2007-08-02.gz is found to be over 30 days old. (I need to leave files alone younger than 30 days.)

I need to move it to server2 into /data/backups/a/anderson/. The first subdirectory under backups is the first letter of the file name and the subdirectory under that is the person’s last name (letters up to the first ‘_’ in the filename). So in this example, anderson_2007-08-02.gz, that’s how we get the /a/anderson/ subdirectories. I don’t believe the directory structure is necessary the way they’ve done it but it’s one of those “this is how we do it” things they want to preserve.

My line of thinking so far:

Use the find command to get my list of files and then move them to /tmp/workingdir somehow preserving the “a/anderson/” portion of directory structure so that I have /tmp/workingdir/a/anderson/anderson_2007-08-02.gz.
Then I can rsync this working directory structure to the other server using the –remove-sent-files option to delete the files afterward.
Lastly I’ll check to be sure there are no files left hanging around and use find /tmp/workingdir –type d –empty –exec rmdir {} \; to cleanup the directory structure since the –remove-sent-files only deletes files.

My questions are, is this the smartest way to do this or is there a better way? If this is ok, is Perl or bash better? And lastly, how do I pull out the first letter and last name from the filename? I know how to do that in Perl but not bash. Of course, the other portions of this script I know how to do in bash but not Perl. =) So, I’m kinda stuck and wondering if I’m going about this the right way at all.

THANK YOU for any suggestions!
Tara

P.S. If you need to know, this is on Ubuntu server (Dapper).

Last edited by mstarap; 09-07-2007 at 04:59 PM.
 
Old 09-07-2007, 09:21 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,485

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
I would have thought either would be o.k. If you have most of it in bash, use that.
`basename <something>` (in back-ticks) works in bash. As for stripping the bit in the middle, if you have the regex already (for perl), surely similar parsing would work with something like sed.
 
Old 09-10-2007, 03:10 PM   #3
mstarap
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks. For those who may be interested, here are a couple tools I ended up using:

I did my find (redirected to a temporary file) and got the list of file names that looks something like this: aarode_2007-08-14_16-56-41.041_1258.upload.lsx.gz

Then I used this code to get just the last name (the part up to the first "_"):
for file in `cat /tmp/list.$$`; do
NAME=${file/_*/} # this substitutes nothing for everything after the first _
...
done

Then I used this piece to get the first letter
FIRST=`perl -e "print substr($NAME,0,1)"`

Doesn't feel pretty but it works.
 
  


Reply

Tags
bash, linux, perl


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
why this script is running in bash but not in perl ratul_11 Linux - General 2 08-17-2007 10:31 AM
Possible to have perl code into bash script ? frenchn00b Programming 3 07-03-2007 01:56 PM
embedded bash in perl script paul_mat Linux - Software 3 11-21-2005 02:46 PM
Help: Perl or Bash Script help embsupafly Programming 1 08-11-2005 07:01 PM


All times are GMT -5. The time now is 12:07 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration