Find command and basename

Hi forum,

I have to convert a lot of rrd files from x86 to amd64 format. The files are kept in a lot of subdirs making up an archive.
I found this method for Linux:
Code:
find . -name *.rrd -exec rrdtool dump {} >`basename {} .rrd`.xml \;
The find syntax is obviously different on FreeBSD and I have tried many different combinations of single quotes, double quotes, accents, brackets etc e.g.
Code:
find . -name "*.rrd" -exec rrdtool dump {} > `basename {} rrd`.xml \;
which doesn't work. It just creates a single huge file called .xml in the top directory.
In fact, I'm just looking for at way to extract the file names and change the extension from "rrd" to "xml".
Any ideas?

Regards,
Jon
 
Why do it all in one line? ;)

Code:
#!/bin/sh

fnames=$( ls *.rrd )

for name in $fnames; do
  shortname=$( echo $name | cut -d . -f 1 )
  rrdtool dump $shortname.rrd > $shortname.xml
done

Note: completely untested. :)
 
Is this script supposed to traverse the subdirectories?

I must admit that my knowledge about shell scripting is very limited. So it might be a stupid question...;)
 
It almost works

Code:
find . -name "*.rrd" -exec sh -c "rrdtool dump {} > `basename {} .rrd`.xml" \;
adds the extension xml, here is part of the result:
Code:
# find . -name "*.xml" | more
./extra/archive/2013-02-12/192.168.1.5_1_f.rrd.xml
./extra/archive/2013-02-12/192.168.1.5_2_f.rrd.xml
./extra/archive/2013-01-15/192.168.1.5_1_f.rrd.xml
./extra/archive/2013-01-15/192.168.1.5_2_f.rrd.xml
./extra/archive/2013-01-27/192.168.1.5_2_f.rrd.xml
./extra/archive/2013-01-27/192.168.1.5_1_f.rrd.xml
./extra/archive/2013-02-01/192.168.1.5_2_f.rrd.xml
./extra/archive/2013-02-01/192.168.1.5_1_f.rrd.xml
./extra/archive/2013-02-06/192.168.1.5_1_f.rrd.xml
The rrdtool does not complain, and I guess that I should use something like this, when I am going to use rrdtool restore from the other server. But if the same things happen, I will end up with triple extensions blahblah.rrd.xml.rrd. I don't think that will work.
 
You're right. This one seems to work correctly:
# find . -name "*.rrd" -exec sh -c "rrdtool dump {} > \`basename {} .rrd\`.xml" \;

The backquotes need to be escaped.
 
Seems to work (almost) with a little adjustment

Code:
find . -name "*.rrd" -exec sh -c "rrdtool dump {} > \`basename {} .rrd\`.xml" \;
Code:
# find . -name "*.xml"
./.xml
Code:
find . -name "*.rrd" -execdir sh -c "rrdtool dump {} > \`basename {} .rrd\`.xml" \;
Code:
find . -name "*.xml"
./extra/archive/2013-02-12/.xml
./extra/archive/2013-01-15/.xml
./extra/archive/2013-01-27/.xml
./extra/archive/2013-02-01/.xml
./extra/archive/2013-02-06/.xml
./extra/archive/2013-01-18/.xml
./extra/archive/2013-01-12/.xml
./extra/archive/2013-01-01/.xml
./extra/archive/2012-11-01/.xml
./extra/archive/2013-02-11/.xml
./extra/archive/2013-01-16/.xml
./extra/archive/2013-02-02/.xml
./extra/archive/2013-01-24/.xml
./extra/archive/2013-02-08/.xml
./extra/archive/2012-10-01/.xml
./extra/archive/2012-12-01/.xml
./extra/archive/2013-02-05/.xml
./extra/archive/2013-01-23/.xml
./extra/archive/2013-01-29/.xml
./extra/archive/2013-01-30/.xml
./extra/archive/2013-02-07/.xml
./extra/archive/2013-01-13/.xml
./extra/archive/2013-01-19/.xml
./extra/archive/2013-01-14/.xml
./extra/archive/2013-01-26/.xml
./extra/archive/2013-01-31/.xml
./extra/archive/2013-01-28/.xml
./extra/archive/2013-02-04/.xml
./extra/archive/2013-01-17/.xml
./extra/archive/2013-02-09/.xml
./extra/archive/2013-02-03/.xml
./extra/archive/2013-01-25/.xml
./extra/archive/2013-02-10/.xml
./extra/.xml
./archive/2011-07-01/.xml
./archive/2013-01-01/.xml
./.xml
I seems the command makes a file .xml in all the directories containing subdirectories. I guess I can live with that, if all the other files are converted correctly. But it will take me some time to go through all the files. :-)
 
How would I do that?

I am the OP :-). And as I said, shell scripts are my top competence.
From another thread, it seems to be possible by tuning the find command. But in fact, I would appreciate a little more knowledge on scripting.
 
I'd probably take a slightly different approach... As with pretty much everything UNIX there are hundreds of ways to solve it.

Code:
#!/bin/sh

find '*.rrd' | while read rrdfile
do
   BASE=`basename $rrdfile .rrd`
   rrdtool dump $rrdfile > "${BASE}.xml"
done
NOTE: Not tested!

This site has always helped me, http://www.grymoire.com/Unix/Sh.html
 
Only works for top level directory

I just changed a few things:
Code:
#!/bin/sh

/usr/bin/find /var/db/rrd/. -name '*.rrd' | while read rrdfile
do
   BASE=`basename $rrdfile .rrd`
   /usr/local/bin/rrdtool dump $rrdfile > "${BASE}.xml"
done
It take ages (it is an ancient machine), but after completion, I get this:
Code:
find . -name "*.xml" | grep -c ""
22
Code:
# find . -d 1 -name "*.rrd" | grep -c ""
22
Code:
find . -name "*.rrd" | grep -c ""
1026
Regards,
Jon
 
No, I did not really work

I only got these .xml (no file name) files in each subdirectory.
Any more suggestions?
 
In lack of own competences

I would really appreciate if you could help me fixing the command. It is almost there. In another part of my thread, a kind user suggested another solution based on a shell script. By that I approach, I could convert all of the rrd files in a directory - but not in the subdirectories. And since I have a lot of subdirectories, I would take me much time to run the script in every directory. And after that, I would have do go through the restore process again one directory at a time.

Code:
find . -name "*.rrd" -execdir sh -c "rrdtool dump {} > \`basename {} .rrd\`.xml" \;
was very close to be right. But I guess another small adjustment would make it work. So if you have even more suggestions, I would be glad to try again.

Regards,
Jon
 
Try this:
Code:
find . -name "*.rrd" -exec sh -c "echo rrdtool dump {} TO \`dirname {}`/\`basename {} .rrd\`.xml" \;

If the output looks correct, remove echo and replace TO with >
otherwise can you post part of the output you get?
 
Code:
# find . -name "*.rrd" -exec sh -c "echo rrdtool dump {} TO \`dirname {}`/\`basename {} .rrd\`.xml" \;
rrdtool dump ./192.168.1.11_swp14.rrd TO ./.xml
rrdtool dump ./extra/192.168.1.11_swp12.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-10/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-10/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-09/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-09/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-03/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-03/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-25/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-25/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-17/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-17/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-28/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-28/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-04/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-04/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-31/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-31/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-13/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-02-13/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-26/192.168.1.5_1_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-26/192.168.1.5_2_f.rrd TO ./.xml
rrdtool dump ./extra/archive/2013-01-14/192.168.1.5_2_f.rrd TO ./.xml
 
Both rrdtool dump and rrdtool restore working now

There were a couple of issues.
The proposed command for running rrdtool dump missed a backslash for escaping a backquote. The right version is
Code:
find . -name "*.rrd" -exec sh -c "rrdtool dump {} > \`dirname {}\`/\`basename {} .rrd\`.xml" \;
I had to change shell to sh to make it work.
On the newer amd64 machine, I successfully ran rrdtool restore like this:
Code:
find . -name "*.xml" -exec sh -c "rrdtool restore {} \`dirname {}\`/\`basename {} .xml\`.rrd" \;
I don't think I would ever have solved this by my self. So thank you very much for the help! :-)

Regards,
Jon
 
Back
Top