0

I am trying to automate a process which take a file type NIFTI, preprocess it, and places the new processed file in an output folder.

deepbrain-extractor -i <input-dir> -o <output-dir>

I wrote this bash script to automate this process for all files in a directory:

for file in path/*.nii
do
    deepbrain-extractor -i $file -o path/newfiles
done

The problem is every time the code runs, it overwrites the old files (since all automatically get the same name). Is there a way to prevent that?

5
  • In this case, the shell has no control, as it is deepbrain-extractor that defines what happens to path/newfiles. Commented Nov 13, 2020 at 20:46
  • Copy the files into a backup directory, run deepbrain-extractor, then move them back (with changed names, maybe <file>-old.nii Commented Nov 13, 2020 at 20:48
  • Do you want path/newfiles to be different for each input file? What behavior do you want to happen, instead of the behavior you actually get? Commented Nov 13, 2020 at 20:52
  • 2
    If what you want is to create an output file name based on the input name, then it might be something like deepbrain-extractor -i "$file" -o "${file%.nii}.out" (and this question would be a duplicate of ones already in our knowledge base). Commented Nov 13, 2020 at 20:57
  • How to create one output file for each file passed to a loop in bash is one such duplicate. Commented Nov 13, 2020 at 20:59

2 Answers 2

2

Thanks for the suggestions and comments! seems like using this code gives me what I want.

deepbrain-extractor -i "$file" -o "${file%.nii}.out" 

Thanks a lot!!

Sign up to request clarification or add additional context in comments.

Comments

0

A common way to get a unique file name is to use the current date and time. You can combine this with the input file name to ensure that output files are never overwritten.

if [ ! -f "$FILE" ]; then
    echo "$FILE does not exist."
    for file in path/*.nii
    do
    deepbrain-extractor -i $file -o path/newfiles_${file##*/}_$(date +"%Y-%m-%d_%H-%M-%S")
    done
fi

so the output directory has a name like newfiles_input_2020-11-13_12-34-56

Also see How to create one output file for each file passed to a loop in bash?

1 Comment

Duplicative questions should be closed, not answered. See the Answer Well-Asked Questions section of How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.