Linux shell script - find all files and run a command on each one of them

I'm trying to port a windows batch file to a linux shell script (bash).

Here is the part that is giving me some headache:

for /r %%G in (*Metadata*.xml) DO (
java -jar %SAXON%\saxon9he.jar -o:%%~nG.csv "%%G" %WORKDIR%\transformBQT.xsl)

What this does is find all .xml file containing the text Metadata and then running the an XSLT transformation on each of these files. This takes 3 arguments

  • -o is the output file (this will be a .csv with the same name as the .xml)
  • next is the target file
  • final argument is the .xsl file

I am thinking of using the following:

find /results/ -type f -name "*Metadata*.xml" -exec 
java -jar $SAXON/saxon9he.jar -o:??? {} $WORKDIR/transformXMI.xsl

but this doesn't quite work as I don't know how to make the output file have the same name as the .xml (with .csv extension)

Any tips?


You could process the results from find line by line and transform <file>.xml into <file>.csv:

find /results/ -type f -name "*Metadata*.xml" | while read file; do java -jar $SAXON/saxon9h3.jar -o:${file%.xml}.csv $file $WORKDIR/transform.XMI.xsl; done

This simple approach fails in case the file names have spaces in their paths/names.

Need Your Help

Is it better to better to close the access DB after every operation of keep it open for later operations ms-access

I am working on a VB.NET project that grabs data from an Access DB file. All the code snipeets I have come across open the DB, do stuff and close it for each operation. I currently have the DB open...

Can someone please explain these Python string formatters

python string python-2.7 documentation

I am confused at the difference between the Python string formatters %d and %i. The Python manual page explains them as the same thing, 'signed integer decimal' - confused !