{"id":134,"date":"2010-09-24T02:00:15","date_gmt":"2010-09-24T02:00:15","guid":{"rendered":"http:\/\/brainybehavior.com\/neuroimaging\/?p=134"},"modified":"2011-04-15T20:30:52","modified_gmt":"2011-04-15T20:30:52","slug":"bash-for-loops-plus-a-little-scripting","status":"publish","type":"post","link":"https:\/\/brainybehavior.com\/neuroimaging\/2010\/09\/bash-for-loops-plus-a-little-scripting\/","title":{"rendered":"Bash For Loops (Plus a Little Scripting)"},"content":{"rendered":"<p>I&#8217;ve written about <a href=\"http:\/\/brainybehavior.com\/neuroimaging\/2010\/08\/bash-scripting-for-neuroimaging\/\">Bash scripting previously<\/a>, as it applies to neuroimaging. Scripting is extremely useful; it can save hours worth of work. But what if you want to do something that is repetitive but do not really need to create a script? This is where the <em>for <\/em>loop is useful.<\/p>\n<p>First I will start with a more complicated example before I get to the <em>for<\/em> loop. This one requires a little bash scripting. You have a problem, you want to create 400 subject directories (don&#8217;t ask me why, I&#8217;m just providing an example) but don&#8217;t want to create them one by one. A simple script could save a lot of time:<\/p>\n<blockquote><p>#!\/bin\/sh<\/p>\n<p>t=0 #this specifies a starting value<\/p>\n<p>read -p &#8220;How many subject directories would you like to create? &#8221; SUBS ;<\/p>\n<p>let SUBS1=${SUBS}+1 #creates a new variable (SUBS1) that is one value greater than the input<\/p>\n<p>condition ()<\/p>\n<p>{<\/p>\n<p>((t++))<\/p>\n<p>if [ $t -lt ${SUBS1} ]<\/p>\n<p>then<\/p>\n<p>return 0\u00a0 # true<\/p>\n<p>else<\/p>\n<p>return 1\u00a0 # false<\/p>\n<p>fi<\/p>\n<p>}<\/p>\n<p>read -p &#8220;Enter the full path (no trailing \/) to the directory where you would like to create the directories: &#8221; SUBSDIR<\/p>\n<p>while condition<\/p>\n<p># \u00a0 \u00a0 ^^^^^^^^^<\/p>\n<p># \u00a0 \u00a0 Function call &#8212; ${SUBS1} loop iterations.<\/p>\n<p>do<\/p>\n<p>mkdir ${SUBSDIR}\/sub${t} ; #The &#8220;sub&#8221; could be changed to whatever you want the prefix to be (e.g., blind, subject, con, exp).<\/p>\n<p>done<\/p><\/blockquote>\n<p>This script was modified from one on\u00a0<a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/loops1.html\">this site<\/a> (about bash loops) with input from\u00a0<a href=\"http:\/\/www.linuxconfig.org\/Bash_scripting_Tutorial#17-1-bash-addition-calculator-example\">this site<\/a> (about bash mathematics). I created that script even though you (or I) probably will not use it very much or at all; it is useful for showing a quick way to create a lot of consecutively numbered directories somewhere on your computer. This script also can be modified as needed in order to do similar functions. I actually only wrote the script so I would have a list of directories to use for this example. Here is the result of the script:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/subjects_dir_screen.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-135\" title=\"subjects_dir_screen\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/subjects_dir_screen.png\" alt=\"\" width=\"546\" height=\"247\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/subjects_dir_screen.png 683w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/subjects_dir_screen-300x135.png 300w\" sizes=\"(max-width: 546px) 100vw, 546px\" \/><\/a><\/p>\n<p>Now I will move on to the real purpose of this post: not scripting but <em>for<\/em> loops.<\/p>\n<p>Let&#8217;s say that you want to create a <em>labels <\/em>directory within a series of subjects&#8217; FreeSurfer directories (or anywhere else). We can pretend that those subjects directories I created above are my FreeSurfer subjects. For this we do not need to write a whole script; a simple for loop will suffice.<\/p>\n<p>First cd to your FreeSurfer subjects directory &#8211; e.g., <em>cd \/Applications\/freesurfer\/subjects<\/em><\/p>\n<p>Then create a text file with the list of subject directories you want to deal with: <em>ls &gt;&gt; blinds.txt<\/em><\/p>\n<p>Next, open that file to remove any unwanted items: <em>open -a TextEdit blinds.txt<\/em><\/p>\n<p>After it only contains a list of the subjects you are ready to save it, close it, and proceed.<\/p>\n<p>Now we are ready to use a for loop to create a bunch of directories called <em>labels <\/em>(one within each FreeSurfer subjects directory):<\/p>\n<p><em>for blind in `cat blinds.txt`; do mkdir \/Applications\/freesurfer\/subjects\/${blind}\/labels; done<\/em><\/p>\n<p>That&#8217;s it. Pretty simple! A <em>for<\/em> loop requires a <em>for<\/em>, a <em>do<\/em>, and a <em>done<\/em>. The semi-colons ( ; ) are required when running a multi-step command like this on a single line.<\/p>\n<p>Here is a screenshot of what my subjects directory (pretend this is my FreeSurfer subjects directory) now looks like:<\/p>\n<p><a href=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/labels_folders_subjects.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139\" title=\"labels_folders_subjects\" src=\"http:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/labels_folders_subjects.png\" alt=\"\" width=\"605\" height=\"466\" srcset=\"https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/labels_folders_subjects.png 605w, https:\/\/brainybehavior.com\/neuroimaging\/wp-content\/uploads\/2010\/09\/labels_folders_subjects-300x231.png 300w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/a>Also notice the blinds.txt file that I created by listing the subjects directory and saving that output to a text file (which I then edited).<\/p>\n<p>That is one example of a simple <em>for<\/em> loop. Any repetitive task that we can use bash for can be put in a simple loop like that. Here is another example using a FreeSurfer command (all the dashes are double dashes): <code>for blind in `cat blinds.txt`; do mri_annotation2label --subject ${blind} --hemi lh --outdir labels; done<\/code><\/p>\n<p>Or, you could copy a number of files from one location to another: <code>for blind in `cat blinds.txt` ; do mkdir \/my_new_imaging_directory\/${blind} ;\u00a0cp .\/${blind}\/${blind}_T1.nii.gz \/my_new_imaging_directory\/${blind}\/${blind}_T1.nii.gz ; done<\/code><\/p>\n<p>That loop is slightly more complicated, it has a couple commands in the <em>do<\/em> part. First, I make a series of new target directories (you wouldn&#8217;t need to include that if the target directories exist already): mkdir&#8230; ; then, I copy images from one location to another: cp &#8230;. A command like this is useful in a script if you want to create temporary files to process further and then remove. I like to create copies of files so I do not accidentally and permanently alter the original images. Just make sure you remove the temp files when you are done or you will end up with a lot of redundant files.<\/p>\n<p>Looping is one reason why it is important to be consistent in labeling files and directories. Name directories that make sense; keep all your images and files organized. If you are consistent, you can throw a simple <em>for <\/em>loop together and save a lot of time without having to write a script. I use loops at least weekly but I create or use scripts less often.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve written about Bash scripting previously, as it applies to neuroimaging. Scripting is extremely useful; it can save hours worth of work. But what if you want to do something that is repetitive but do not really need to create &hellip; <a href=\"https:\/\/brainybehavior.com\/neuroimaging\/2010\/09\/bash-for-loops-plus-a-little-scripting\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[39,66,29],"_links":{"self":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/134"}],"collection":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":11,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/posts\/134\/revisions\/193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/media\/137"}],"wp:attachment":[{"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainybehavior.com\/neuroimaging\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}