Bash: How to Traverse Directory Structure and Execute Commands

Bash: how to traverse directory structure and execute commands?

For this kind of thing I always use find together with xargs:

$ find output-* -name "*.chunk.??" | xargs -I{} ./myexecutable -i {} -o {}.processed

Now since your script processes only one file at a time, using -exec (or -execdir) directly with find, as already suggested, is just as efficient, but I'm used to using xargs, as that's generally much more efficient when feeding a command operating on many arguments at once. Thus it's a very useful tool to keep in one's utility belt, so I thought it ought to be mentioned.

How to go to each directory and execute a command?

You can do the following, when your current directory is parent_directory:

for d in [0-9][0-9][0-9]
do
( cd "$d" && your-command-here )
done

The ( and ) create a subshell, so the current directory isn't changed in the main script.

Shell script to iterate through sub-directories of a directory and execute a command

You could do something like

cd path/to/main/directory
find . -name '*.c' -exec gcc -c {} \;
gcc -o app *.o

Traverse a directory structure using a for loop in shell scripting

This is the way I have implemented to get it working

for names in $(find /tmp/files/ -type f); 
do
echo " ${directoryName} -- Directory Name found after find command : names"

<== Do your Processing here ==>

done

Names will have each file with the complete folder level

/tmp/files is the folder under which I am finding the files

How to loop over files in directory and change path and add suffix to filename

A couple of notes first: when you use Data/data1.txt as an argument, should it really be /Data/data1.txt (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming /Data/data1.txt and .txt files only:

#!/bin/bash
for filename in /Data/*.txt; do
for ((i=0; i<=3; i++)); do
./MyProgram.exe "$filename" "Logs/$(basename "$filename" .txt)_Log$i.txt"
done
done

Notes:

  • /Data/*.txt expands to the paths of the text files in /Data (including the /Data/ part)
  • $( ... ) runs a shell command and inserts its output at that point in the command line
  • basename somepath .txt outputs the base part of somepath, with .txt removed from the end (e.g. /Data/file.txt -> file)

If you needed to run MyProgram with Data/file.txt instead of /Data/file.txt, use "${filename#/}" to remove the leading slash. On the other hand, if it's really Data not /Data you want to scan, just use for filename in Data/*.txt.

Run bash command in a python loop

One way to solve this is to get the current folder before the loop starts and you call chdir() for the first time. Then you can chdir() back to that folder.



Related Topics



Leave a reply



Submit