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 linebasename 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
Generate Public Ssh Key from Private Key
Errors While Installing Opencv and Ffmpeg Through Rightscripts
Having Trouble Finding the Method _Kernel_Vsyscall Within the Linux Kernel
Running Multiple Compass-Sass Watch Operations Automatically
Remove a Specific Line from a File Without Using Sed or Awk
Simulating a Spinner for Progress in Bash
Difference Between Kernel, Kernel-Thread and User-Thread
What's the Purpose of the Ud2 Opcode in the Linux Kernel
Haskell Ghc Compiling/Linking Error, Not Creating Executable. (Linux)
Preserve Environments Vars After Shell Script Finishes
How to Install the Specific Version of Postgres
What Is Echo $? in Linux Terminal
Storing Pid File for a Daemon Run as User
Linux: Handling a Segmentation Fault and Getting a Core Dump
Temporarily Prevent Linux from Shutting Down
Windows Equivalent of ./ (Current Directory)
How to Count Most Occuring Sequence of 3 Letters Within a Word with a Bash Script