Caitlin
Caitlin

Reputation: 525

execute command on all files and store in new directory using the file name in script argument

I want to run a script on all files in my directories with a .fa extension. The script has an argument for the output directory and I want to use the file name for this argument.

To run the script on one file, I would use:

metaerg.pl  --dbdir /projects/p30777/db DeMMO1_10.fa --sp --tm --outdir DeMMO1_10  --force

where DeMMO1_10.fa is the file name and DeMMO1_10 is the name of the new directory being created for the output. There are ~500 of these files split into six directories. I want to run something like:

find . -name '*.fa' -exec metaerg.pl  --dbdir /projects/p30777/db {} --sp --tm --outdir {}  --force \;

but I'm not sure what to put for the --outdir argument so that it passes in just the file name without the .fa extension or the rest of the path.

Upvotes: 0

Views: 126

Answers (1)

vtronko
vtronko

Reputation: 488

find . -name '*.fa' -exec sh -c 'metaerg.pl  --dbdir /projects/p30777/db {} --sp --tm --outdir `basename $1 .fa`  --force' sh {} \;

We create a subshell and pass a {} placeholder from find-exec to it as a $1 parameter (last sh being $0 just for the sake of consistency). Within subshell, we take $1 parameter and use it within backticks to evaluate the basename() and emplace the resulting text as an argument to outdir.

Upvotes: 1

Related Questions