| View previous topic :: View next topic |
| Author |
Message |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Wed Sep 03, 2008 1:47 pm Post subject: SOLVED: Shell Scripting/Join Question |
|
|
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 |
|
 |
slack---line l33t


Joined: 01 Apr 2005 Posts: 893 Location: /uk/sheffield
|
Posted: Wed Sep 03, 2008 1:56 pm Post subject: |
|
|
Could you not just use rsync to mirror everything?
slack _________________ "Ubuntu" - an African word meaning "Gentoo is too hard for me". |
|
| Back to top |
|
 |
Genone Retired Dev


Joined: 14 Mar 2003 Posts: 7742 Location: beyond the rim
|
Posted: Wed Sep 03, 2008 2:02 pm Post subject: |
|
|
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 |
|
 |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Wed Sep 03, 2008 2:03 pm Post subject: |
|
|
| 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 |
|
 |
slack---line l33t


Joined: 01 Apr 2005 Posts: 893 Location: /uk/sheffield
|
Posted: Wed Sep 03, 2008 2:05 pm Post subject: |
|
|
| 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 ) _________________ "Ubuntu" - an African word meaning "Gentoo is too hard for me". |
|
| Back to top |
|
 |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Wed Sep 03, 2008 2:06 pm Post subject: |
|
|
| 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 |
|
 |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Wed Sep 03, 2008 2:12 pm Post subject: |
|
|
| 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 |
|
 |
Genone Retired Dev


Joined: 14 Mar 2003 Posts: 7742 Location: beyond the rim
|
Posted: Wed Sep 03, 2008 2:12 pm Post subject: |
|
|
| 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 |
|
 |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Thu Sep 04, 2008 2:00 pm Post subject: |
|
|
The printf option isn't in this old version of bsd -_-;
::bangs his head against his monitor:: |
|
| Back to top |
|
 |
ppurka Veteran

Joined: 26 Dec 2004 Posts: 1574
|
Posted: Thu Sep 04, 2008 2:14 pm Post subject: |
|
|
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 |
|
 |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Fri Sep 05, 2008 11:54 am Post subject: |
|
|
It worked! Thanks for that clever use of \1!
^^ |
|
| Back to top |
|
 |
kiyote n00b

Joined: 10 May 2007 Posts: 17
|
Posted: Fri Sep 05, 2008 12:11 pm Post subject: |
|
|
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 |
|
 |
|