June 2018
How to run multiple programs in a sequence using a bash script?
27 06 18 • 02:11& Filed in: Coding
How to write a bash script that goes through each subdirectory inside a parent_directory and executes a command in each subdirectory?
Suppose there are 10 subdirectories inside a parent_directory with the following structure:
***parent_directory (name could be anything - doesn't follow a pattern)
— subdir1 (subdirectory names follow a specific pattern, e.g. fixed characters + number)
input.data (input of the code)
code.f (main code, here is a Fortran code)
Makefile (makefile to execute and run the code)
— subdir2
input files + codes + Makefile
— subdir3
input files + codes + Makefile
.
.
.
— subdir10
Suppose we want to run the codes in the first five subdirectories, i.e. subdir1-subdir5, wait for the processes to finish and then continue running the codes in the next five subdirectories, i.e. subdir6-subdir10.
The example of the bash script is as (download the bash script file
You can run the bash script (called "run.sh") by:
Suppose there are 10 subdirectories inside a parent_directory with the following structure:
***parent_directory (name could be anything - doesn't follow a pattern)
— subdir1 (subdirectory names follow a specific pattern, e.g. fixed characters + number)
input.data (input of the code)
code.f (main code, here is a Fortran code)
Makefile (makefile to execute and run the code)
— subdir2
input files + codes + Makefile
— subdir3
input files + codes + Makefile
.
.
.
— subdir10
Suppose we want to run the codes in the first five subdirectories, i.e. subdir1-subdir5, wait for the processes to finish and then continue running the codes in the next five subdirectories, i.e. subdir6-subdir10.
The example of the bash script is as (download the bash script file
):#!/bin/bash## exit on the first error.set -e# Add the full path of parent_directory to run the codes in the subdirectoriespath=$(pwd)## just for checking the path of the parent_directoryecho path= $path## the min and the max values of the int variable i, changing from 1 to 5 i=1imax=5## do loop from i to imaxwhile [ $i -le $imax ]; do## path of each subdirectory defined as "subdir" + integer ipathi=$path/subdir$i## just for checking the path of each subdirectoryecho pathi= $pathi## cd to each subdirectory of the loopcd $pathi## compile and run the code in each subdirectory using Makefile ("Main" is the executable file)make -f Makefilenohup ./Main &## adding the counterlet i=i+1done## Wait for the processes to finishwaiti=6imax=10## do loop from i to imaxwhile [ $i -le $imax ]; do## path of each subdirectory defined as "subdir" + integer ipathi=$path/subdir$i## just for checking the path of each subdirectory using Makefile ("Main" is the executable file)echo pathi= $pathi## cd to each subdirectory of the loopcd $pathi## compile and run the code in each subdirectorymake -f Makefilenohup ./Main &## adding the counterlet i=i+1doneYou can run the bash script (called "run.sh") by:
$ bash run.shHow to copy a file to all subdirectories in a directory using a command-line?
26 06 18 • 13:32& Filed in: Coding
This will put the file "code.f" (in the current working directory) in all of the subfolders, but not their subfolders:
But this will put the file "code.f" in all of the subfolders and their subfolders:
for d in */; do cp code.f "$d"; doneBut this will put the file "code.f" in all of the subfolders and their subfolders:
find . -type d -exec cp code.f {} \;How to delete all files of a specific extension in a directory and all subfolders using a command-line?
26 06 18 • 12:54& Filed in: Coding
To remove all files with a specific extension (e.g. .data) in the current directory and all subfolders run
But first use "find" to see exactly which files you will remove:
find . -name "*.data" -type f -deleteBut first use "find" to see exactly which files you will remove:
find . -name "*.data" -type f