I'm trying to figure out how to write a script that will run a command on files in a directory sequentially with a pause for each command to wait to complete. Here's an example.

cat >> text.txt && cat >> text2.txt

Above command works for both files as long as I manually type each file. My personal project has 559 files, i.e., text1.txt...text559.txt.

My last resort is to pull up a list of the file names in the terminal and go to my text editor and just build the script for a one time run. I would like to automate it if possible so any suggestions on how to get started would be appreciated.


1 Answer 1


You probably want to look at a for loop. In bash, it would look something like:

for file in "${list_of_files[@]}"
  run_command "$file"

The "${list_of_files[@]}" can be an array that you set elsewhere, or you may replace this with a literal list of filenames, or it can be a filename globbing pattern such as text*.txt.

This loop will run run_command with a single parameter, the $file that you're working on in that loop iteration, and will wait for each run_command to complete before moving on to the next loop iteration.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.