LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to write a script/command line that runs through multiple sub-directories? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-a-script-command-line-that-runs-through-multiple-sub-directories-4175410874/)

rvsmith 06-11-2012 09:47 AM

How to write a script/command line that runs through multiple sub-directories?
 
Hello!

I am currently trying to write a command/script that goes thought multiple directories and execute the same file conversion command.
The data structure is as follow:
The main directory, raw-data, contains 100 sub-directories (100 subjects folders), each with three sub-directories, the last of which contains the files I am trying to convert (raw-data/subj001/023/12/3)
I face the following problem. The package I am currently using sends output only to the working directory. Therefore, I was hoping that I could get some help with what to include in a command line or a script that would continuously change the working directory so that the output of each subject will be send back to the same directory the input came from.
I came up with something like this:

find . -mindepth 4 -type d -exec tractor -d -r -b preproc RunStages:1 Interactive:FALSE'{}' \;

I don`t have much linux scripting experience so your help will be much appreciated.

MensaWater 06-11-2012 03:34 PM

Since you want to change to the directory you might want to do a for loop:

find . -mindepth 4 -type d -exec tractor -d -r -b preproc RunStages:1 Interactive:FALSE'{}' \;

for dir in $(find . -mindepth 4 -type d)
do echo DIRECTORY is $dir
cd $dir
tractor -d -r -b preproc RunStages:1 Interactive:FALSE
done

The above would show you what directory it found then cd to that directory then run your tractor command.
Since I'm not familiar with the command "tractor" I'm not sure what it expects. Do you give it the directory name or the names of the files in the directory? You may need to add the directory name (e.g. "./" or "$(PWD)) to your command and/or the file spec (e.g. if all files it might be $(PWD)/*).

ppoulsen 06-11-2012 04:43 PM

I believe this will work.

Code:

find `pwd` -mindepth 4 -type d | xargs -I {} bash -c 'cd {}; tractor -d -r -b preproc RunStages:1 Interactive:FALSE;'
I'm unfamiliar with the tractor command, so that portion may require tweaking. I assumed it executes in whatever directory it is located, but you may need to add a . before the semicolon for a filepath if necessary.

To break it down:
Code:

find `pwd` -mindepth 4 -type d
Generate list of directories. The use of `pwd` ensures absolute filepath names.

Code:

| xargs -I {}
Pipe this list to xargs, which will execute a command on each line. The filepath will be represented by {}.

Code:

bash -c
Because we want to execute multiple commands, open a bash shell (or specify other shell) with multiple commands passed in as arguments.

Code:

'cd {}; tractor -d -r -b preproc RunStages:1 Interactive:FALSE;'
Change to a directory specified as a line of output from the find command and execute the tractor command in that directory.

rvsmith 06-12-2012 07:07 AM

Quote:

Originally Posted by MensaWater (Post 4700913)
Since you want to change to the directory you might want to do a for loop:

find . -mindepth 4 -type d -exec tractor -d -r -b preproc RunStages:1 Interactive:FALSE'{}' \;

for dir in $(find . -mindepth 4 -type d)
do echo DIRECTORY is $dir
cd $dir
tractor -d -r -b preproc RunStages:1 Interactive:FALSE
done

The above would show you what directory it found then cd to that directory then run your tractor command.
Since I'm not familiar with the command "tractor" I'm not sure what it expects. Do you give it the directory name or the names of the files in the directory? You may need to add the directory name (e.g. "./" or "$(PWD)) to your command and/or the file spec (e.g. if all files it might be $(PWD)/*).

tractor uses the current directory therefore so far not been sure about the script I have been manually changing the working directory for each participant. This command converts all files within the specified directory to a nifti format. Thank you. I am definitely going to try this!

rvsmith 06-12-2012 07:21 AM

Quote:

Originally Posted by ppoulsen (Post 4700953)
I believe this will work.

Code:

find `pwd` -mindepth 4 -type d | xargs -I {} bash -c 'cd {}; tractor -d -r -b preproc RunStages:1 Interactive:FALSE;'
Code:

| xargs -I {}
Pipe this list to xargs, which will execute a command on each line. The filepath will be represented by {}.

Code:

bash -c
Because we want to execute multiple commands, open a bash shell (or specify other shell) with multiple commands passed in as arguments.

Code:

'cd {}; tractor -d -r -b preproc RunStages:1 Interactive:FALSE;'
Change to a directory specified as a line of output from the find command and execute the tractor command in that directory.

New to linux, I have never used xargs before. This was exactly what I was trying to do. Although, it seems like a better idea to use a loop instead. Thank you!


All times are GMT -5. The time now is 06:14 AM.