1

I have a code that is svr_vars. I have a directory with 300 different files. I want to loop it and to run the code on each file in the directory. The svr_vars code outputs a generic filename of svr_vars.txt. Therefore, every time the code is run, it overwrites the svr_vars.txt filename. So, it needs to be renamed after every run of the code.

1995051200_72259.txt <= what one file in the directory looks like

I want the output to be renamed from svr_vars.txt to svr.1995051200_72259.txt

3

If I understand the question correctly, you basically do

svr_vars 1995051200_72259.txt

and the program generates output called svr_vars.txt and you want this renamed to svr.1995051200_72259.txt

Assuming that all the 300 files begin with a number and end in .txt then you can do a loop similar to

#!/bin/bash
for file in [1-9]*.txt
do
  svr_vars "$file"
  mv svr_vars.txt "svr.$file"
done

We can add some sanity checks to this loop to cause it to abort if things don't work properly. eg

#!/bin/bash
for file in [1-9]*.txt
do
  if [ -e "svr.$file" ]
  then
    echo "$file already processed.  skipping"
  else
    svr_vars "$file"
    if [ ! -e svr_vars.txt ]
    then
      echo "Failed to create results for $file"
    else
      mv svr_vars.txt "svr.$file"
    fi
  fi
done

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.