Skip to content
Advertisement

List and change directory and run command in Shell

I am coming back to shell after almost 6 years or so. I have a directory structure let’s say,

MainDirectory –> subDirectory1/some.xml
                     –> subDirectory2/some.xml
                     …
                     …
                     –> subDirectoryN/some.xml

And I want to execute some.xml in all the subdirectories and get the output to a file using “>” operator I guess. What would be the proper script for this? I tried a couple of option and played around but couldn’t achieve this. Can anybody please help?

Advertisement

Answer

EDIT :

You can do something like this :

cd <path_to_main_directory>
for subdir in */
do
    pushd "$subdir"
    <do_smth> some.xml > output.txt
    popd
done
Advertisement