# Find command and basename



## jontheil (Feb 13, 2013)

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:

```
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.

```
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


----------



## phoenix (Feb 13, 2013)

Why do it all in one line?  


```
#!/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.


----------



## jontheil (Feb 13, 2013)

*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...


----------



## tobik@ (Feb 13, 2013)

Try this:
`# find . -name "*.rrd" -exec sh -c "rrdtool dump {} > `basename {} .rrd`.xml" \;`


----------



## jontheil (Feb 13, 2013)

*It almost works*


```
find . -name "*.rrd" -exec sh -c "rrdtool dump {} > `basename {} .rrd`.xml" \;
```
_adds_ the extension _xml_, here is part of the result:

```
# 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.


----------



## tobik@ (Feb 13, 2013)

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.


----------



## phoenix (Feb 13, 2013)

jontheil said:
			
		

> I must admit that my knowledge about shell scripting is very limited. So it might be a stupid question...



No, it doesn't.  Missed that part of the OP.


----------



## jontheil (Feb 13, 2013)

*Seems to work (almost) with a little adjustment*


```
find . -name "*.rrd" -exec sh -c "rrdtool dump {} > \`basename {} .rrd\`.xml" \;
```


```
# find . -name "*.xml"
./.xml
```


```
find . -name "*.rrd" -execdir sh -c "rrdtool dump {} > \`basename {} .rrd\`.xml" \;
```


```
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.


----------



## jontheil (Feb 13, 2013)

*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.


----------



## SirDice (Feb 14, 2013)

I'd probably take a slightly different approach... As with pretty much everything UNIX there are hundreds of ways to solve it.


```
#!/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


----------



## jontheil (Feb 14, 2013)

*Only works for top level directory*

I just changed a few things:

```
#!/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:

```
find . -name "*.xml" | grep -c ""
22
```


```
# find . -d 1 -name "*.rrd" | grep -c ""
22
```


```
find . -name "*.rrd" | grep -c ""
1026
```
Regards,
Jon


----------



## jontheil (Feb 14, 2013)

*No, I did not really  work*

I only got these _.xml_ (no file name) files in each subdirectory.
Any more suggestions?


----------



## jontheil (Feb 15, 2013)

*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.


```
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


----------



## tobik@ (Feb 15, 2013)

Try this:

```
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?


----------



## jontheil (Feb 15, 2013)

```
# 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
```


----------



## tobik@ (Feb 15, 2013)

That's really odd... What shell are you using?


----------



## jontheil (Feb 15, 2013)

```
/bin/tcsh
```


----------



## tobik@ (Feb 15, 2013)

Try the command in sh or bash.


----------



## jontheil (Feb 16, 2013)

*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

```
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:

```
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


----------

