Ejecute un ejecutable en todos los archivos de una carpeta (con subcarpetas)

Tengo un ejecutable test que toma un .txt file as input, for example, we can run in a terminal ./test input.txt > result.txt

I have also a folder folder which contains subfolders and .txt archivos.

I would like to write a bash to do the following task: go through all the .txt dentro folder, run the executable on it, and put the result in a separated file.

Por ejemplo, para folder\subfolder1\01-input.txt, I would like to put the result in a file named subfolder1_01-input.txt (the result is not necessarily named like this, the names are just used to differentiate the results and allow me to trace back to the corresponding input file).

All the results can be put together in a big folder called results.

Does anyone know how to write this bash?

In the first place, to simplify the problem, we could assume that: there is only 1 level of subfolders in folder, y todo el .txt inputs are placed under a subfolder of folder.

preguntado el 27 de noviembre de 13 a las 01:11

What have you tried? You should read at least a beginner's tutorial or do some googling. This kind of question has been asked a million times -

2 Respuestas

for foldername in folder/*; do cd "$foldername"; for filename in *.txt; do ../../test "$filename" > output_"$filename"; done ; cd ../..; done

Note the output filename does not conform to your spec, this exercise is left to the reader ;-)

respondido 27 nov., 13:02

Mind the quoting: "$foldername", "$filename", output_"$filename" - Jo tan

find $topleveldir -type f -exec sh -c 'file="{}"; log="${file//\//_}".txt; log="${log/#.}" ; echo $file $log ; ./test $file > $log' \;

If in doubt put the redirect in a script wrapper and just do a raw -exec ie

find $toplevel -type f -exec testwithitsownrediect.sh {} \;

I am assuming bash AND gun find

respondido 27 nov., 13:02

-1: indeed! You should check your answers before submitting. -exec doesn't take a shell script, but execvp-like arguments... - Jo tan

@JoSo OTOH find is the right way to do this, possibly also with a -name \*.txt - Esteban P.

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.