Repository navigation
BASH tutorial (Linux and MacOS command prompt)
- Basic BASH commands 1.1 Navigating through files 1.2 Manipulating files
- For loops 2.1 Rationale 2.2 Basic loop structure + examples 2.3 For loops with 2 variables
In the command prompt, you are by default working from the home directory on your drive.
First, you may list files in the current directory using ls
This will display everything, so the list may be long. If it's too long, you can always press CTRL + C to interrupt the current command.
To move throughout the directories, use cd. For example:
cd /home/minc/Desktop/FEOBV/analyses/
This will bring me from the home directory to my FEOBV analyses directory.
Now, if I use ls, it will display everything in my analyses directory.
It's also possible to display the content of another file with ls path/to/your/folder/
To go back, use .., for example:
cd ../../MK6240/analyses/
This will bring me 2 directories out from the current directory - the Desktop - then into a different analyses folder.
For loops allow you to perform operations on all objects that correspond to your criteria. This is a very efficient way of, for example, processing neuroimaging data.
For loops require you to first define the objects you're interested in (eg. all MK6240 images).
In BASH, this usually means using something in the file's name to identify it.
Using MINC functions, you can name an output based on the input. However, this process is based on the length of the file names, so this only works well if the files are named consistently. Ideally, all IDs should be the exact same length to allow you to name the outputs efficiently.
Also, I recommend all of your files either be in the same folder or be nested into a subject folder.
Your folder could therefore look like this:
PersonalFolder/MK/SUVR/
for [VarName] in [criteria for the specific files you want] ;
do [The command you want to be executed on all selected files] ;
done
You refer to your file in the command using $VarName When creating a new file based on parts of the existing file name, you can use the file name using
${VarName:0:18} #using 18 characters starting from character 0
Example: Display a series of images on Display (they will open 1 at a time, and the next one will automatically open when you close the previous one)
for file in * ; do Display -gray -range 50 900 "${file:0:10}"*.mnc -label ../Template_labels_Left/"${file:0:10}"*.mnc ; done
Example: obtain the mean SUVR within a mask File structure is [ID(7 character long)]/[ID+filetype]
for file in * ;
do mincstats -mask ../WM_0.99_new.mnc -mask_binvalue 1 "${file:0:7}"/"${file:0:7}"-FEOBV-*avg_suv_autoreg_ST.mnc -mean ;
done
The output will be a series of SUVR values based on the file's alphabetical order
FOR loops are also super useful to collect and move files around. For example:
for i in */*T2*.mnc ; #Collecting all T2 images in minc format nested 2 layers deeper than the current directory
do cp $i ../../../etienne/ ; #Copying all the images inside my folder
done
Adding one character to a file name to make it longer:
!!! If you modify a file like this, I recommend you run it with the echo function first to see if it's well built.
for i in MRT*.mnc ; #Collecting all files with "MRT" in the ID
do echo mv $i "${i:0:7}"O"${i:7:100}" ; #Adding an "O" at the end of the ID to bring it to 8 characters long
done
#output (1 line per file):
mv MRT6123_segmented_labels.mnc MRT6123O_segmented_labels.mnc
Also, remember that if something goes wrong, you can stop the for loop using the CTRL + C keys
The function looks good, we can now run it:
for i in MRT*.mnc ; #Collecting all files with "MRT" in the ID
do mv $i "${i:0:7}"O"${i:7:100}" ; #Adding an "O" at the end of the ID to bring it to 8 characters long
done
You can add variables in your for loop that is generated based on the text output of a command within the loop using something like
REF=$($echo [command you want the output of])
The output must ONLY be what you're interested in. In Minc commands, this generally implies using -quiet Echo means outputting the command's result as text
Example: Standardize a PET image based on the signal in a ROI
for file in * ; do REF=$($echo mincstats -mask ../../refMask_fullCG_res.mnc -mask_binvalue 1 $file -mean -quiet) ;
mincmath -div -const $REF "${file:0:15}"*_ST.mnc "${file:0:18}"_SUVR_ST.mnc ; done
A second example of ref region SUVR
for i in FEOBV*.mnc ; do REF=$($echo mincstats -mask c2xSVBSS_er.mnc -mask_binvalue 1 $i -mean -quiet) ;
mincmath -div -const $REF $i s"${i:0:13}"_PrevST_SUVR.mnc ; done