Gentoo Forums
Gentoo Forums
Quick Search: in
SOLVED: Shell Scripting/Join Question
View unanswered posts
View posts from last 24 hours

rackathon
 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Wed Sep 03, 2008 1:47 pm    Post subject: SOLVED: Shell Scripting/Join Question Reply with quote

I've been banging my head over this problem for the past few days. If anybody here has a solution (and an explanation if its sufficiently complicated) I would greatly appreciate it:

My goal right now is to create a back up of certain files while maintaining the directory tree. For example, if I'm trying to save all .xml files in any subdirectory of /cache, and there is a file in /cache/foo/ called bar.xml, I want to copy /cache/foo/bar.xml to /backup/foo/bar.xml. Unfortunately there's a catch: our software doesn't store bar.xml as bar.xml, but in a directory with the same name with the actual file called "current". So in the end, I have to copy /cache/foo/bar.xml/current to /backup/foo/bar.xml.

Here is the code I've generated so far:

Code:

find /cache/ -name "*.xml" > temp.txt
sed -e 's;cache;backup;' < temp.txt > temp_to.txt

sed -e 's;$;\/current;' <temp.txt >temp_from.txt



Here's my problem: how do I match up all of my lines from temp_to.txt and temp_from.txt so that I have a file temp_path that's formatted like:

/cache/foo/bar.xml/current /backup/foo/bar.xml

so that I can finish the code up with a

Code:

sed -e 's/^/cp -f /' < temp_path | sh -x



I've tried the command join -o 1.1 2.1 temp_from.txt temp_to.txt but this didn't seem to work :(

Thank you for any help!


Last edited by kiyote on Fri Sep 05, 2008 11:55 am; edited 1 time in total
Back to top
View user's profile Send private message
slack---line
l33t
l33t


Joined: 01 Apr 2005
Posts: 893
Location: /uk/sheffield

PostPosted: Wed Sep 03, 2008 1:56 pm    Post subject: Reply with quote

Could you not just use rsync to mirror everything?


slack
_________________
"Ubuntu" - an African word meaning "Gentoo is too hard for me".
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 7742
Location: beyond the rim

PostPosted: Wed Sep 03, 2008 2:02 pm    Post subject: Reply with quote

I'd suggest that you look into the -printf and -path options of find to directly generate the desired output, instead of hacking around with the output later. You can avoid teh /cache prefix in the output by first changing in the directory, and then calling find on the current directory (cd /cache; find . -name ...)

Last edited by Genone on Wed Sep 03, 2008 2:03 pm; edited 1 time in total
Back to top
View user's profile Send private message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Wed Sep 03, 2008 2:03 pm    Post subject: Reply with quote

I'm actually doing this on an old bsd machine. Even if I could, my boss wants the files automatically updated to the right name.
Back to top
View user's profile Send private message
slack---line
l33t
l33t


Joined: 01 Apr 2005
Posts: 893
Location: /uk/sheffield

PostPosted: Wed Sep 03, 2008 2:05 pm    Post subject: Reply with quote

kiyote wrote:
I'm actually doing this on an old bsd machine. Even if I could, my boss wants the files automatically updated to the right name.


If your boss "knows" that it can be done is he not kind enough to let you in on the secret (or is it an initiation test :lol: )
_________________
"Ubuntu" - an African word meaning "Gentoo is too hard for me".
Back to top
View user's profile Send private message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Wed Sep 03, 2008 2:06 pm    Post subject: Reply with quote

Quote:

I'd suggest that you look into the -printf and -path options of find to directly generate the desired output, instead of hacking around with the output later. You can avoid teh /cache prefix in the output by first changing in the directory, and then calling find on the current directory (cd /cache; find . -name ...)



But wouldn't I need the complete path for when I execute the copy command?
Back to top
View user's profile Send private message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Wed Sep 03, 2008 2:12 pm    Post subject: Reply with quote

My boss knows nothing of the sort, she just wants it that way and I have to do my best to give it to her that way. :sigh:
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 7742
Location: beyond the rim

PostPosted: Wed Sep 03, 2008 2:12 pm    Post subject: Reply with quote

kiyote wrote:
Quote:

I'd suggest that you look into the -printf and -path options of find to directly generate the desired output, instead of hacking around with the output later. You can avoid teh /cache prefix in the output by first changing in the directory, and then calling find on the current directory (cd /cache; find . -name ...)



But wouldn't I need the complete path for when I execute the copy command?

You could add that back in the formatting string.
Back to top
View user's profile Send private message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Thu Sep 04, 2008 2:00 pm    Post subject: Reply with quote

The printf option isn't in this old version of bsd -_-;

::bangs his head against his monitor::
Back to top
View user's profile Send private message
ppurka
Veteran
Veteran


Joined: 26 Dec 2004
Posts: 1574

PostPosted: Thu Sep 04, 2008 2:14 pm    Post subject: Reply with quote

How about something like this:
Code:
find /cache -name "*.xml" -type d | sed -e 's;/cache\(.*\)$;cp -i /cache\1/current /backup\1;' > cp.txt

_________________
... ppurka ...
Back to top
View user's profile Send private message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Fri Sep 05, 2008 11:54 am    Post subject: Reply with quote

It worked! Thanks for that clever use of \1!

^^
Back to top
View user's profile Send private message
kiyote
n00b
n00b


Joined: 10 May 2007
Posts: 17

PostPosted: Fri Sep 05, 2008 12:11 pm    Post subject: Reply with quote

Quick note:

In order to create directories that don't already exist, the -R option needs to be included, so the final code is:

Code:

find /cache -name "*.xml" -type d | sed -e 's;/cache\(.*\)$;cp -i -R /cache\1/current /backup\1;' > cp.txt


Once again, thank you!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum