LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-11-2012, 10:47 AM   #1
rvsmith
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 06-11-2012, 04:34 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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)/*).
 
1 members found this post helpful.
Old 06-11-2012, 05:43 PM   #3
ppoulsen
LQ Newbie
 
Registered: May 2012
Posts: 5

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 06-12-2012, 08:07 AM   #4
rvsmith
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
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!
 
Old 06-12-2012, 08:21 AM   #5
rvsmith
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ppoulsen View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK runs in command line, but not in script PaulVanLil Linux - Newbie 4 03-03-2011 08:07 AM
[SOLVED] PHP script runs from the command line...but not on Apache custangro Linux - Enterprise 3 07-02-2010 07:38 PM
Script runs fine from command line, but not when attempted from Cron hwest Linux - Newbie 8 12-11-2008 07:48 PM
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-03-2008 12:01 AM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 05:14 PM


All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration