philipovic
philipovic

Reputation: 59

Change working directory while looping over folders

Currently I am trying to run MRI software (TBSS) on imaging files(scan.nii.gz) on the Linux command line.

The scans are all stored in separate folders for different participants and the file names are identical,so:

/home/scans/participant1/scan.nii.gz

/home/scans/participant2/scan.nii.gz

/home/scans/participant3/scan.nii.gz

What this software does is it creates the result of the analysis in the current working directory.Since the scans have the same image name, they get overwritten al the time.

I would like to loop through all the participant folders, make it my working directory and then execute the tbss command, which is simply tbss_1_preproc scan.nii.gz. In this way, the file will be stored in the current working directory,which is the participant directory.

Is there any sensible way of doing this in Linux ?

Thanks so much !

Upvotes: 1

Views: 66

Answers (1)

NaN
NaN

Reputation: 8601

Try it in BASH. The code below is untested, but it should give you a clue

#! /bin/bash

find . -name scan.nii.gz | while read line
do
   cd $(dirname "${line}")
   tbss_1_preproc $(basename "${line}")
done

Put it in a file and make it executable. Copy it to your scans folder and execute it.

Upvotes: 0

Related Questions