BASH Tricks
Make a list of directory names
We often organize subject data so that each subject gets their own directory. Freesurfer uses a subjects file when batch processing. Rather than manually type out each folder name into a text file, it can be generated in one line of code:
ls -1 -d */ | sed 's\/\\g' > subjects
This lists in 1 column all the directories (-1 -d) and uses sed to snip off the trailing forward slashes in the directory names
Make a series of numbered directories
FreeSurfer BOLD data goes in a series of directories, numbered 001, 002, ... , 0nn. A one-liner of code to create these directories in the command line:
for i in $(seq -f "%03g" 1 6); do mkdir ${i}; done #this will create directories 001 to 006. Obviously, if you need more directories, change the second value from 6 to something else
Protip: If you want to also make the runs
file that some of our scripts use at the same time, the above snippet can be modified:
for i in $(seq -f "%03g" 1 6); do mkdir ${i}; echo ${i} >> runs; done
Restart Window Manager
This has happened a couple times before: you step away from the computer for awhile (maybe even overnight) and when you come back, you find it is locked up and completely unresponsive. The nuclear option is to reboot the whole machine:
sudo shutdown -r now #Sad for anyone running autorecon or a neural network
Unfortunately, that will stop anything that might be running in the background. A less severe solution might be to just restart the window manager. To do this you will need to ssh into the locked-up computer from a different computer, and then restart the lightdm process. This will require superuser privileges.
ssh hostname
Then after you have connected to the frozen computer:
sudo restart lightdm
Any processes that were dependent on the window manager will be terminated (e.g., so if you had been in the middle of editing labels in tksurfer, you will find that tksurfer has been shutdown and you will need to start over), however anything that was running in the background (e.g., autorecon) should be unaffected.
Renaming Multiple Files
If you don't have access to the rename command (Mac OSX), you can fake it:
PREFIX=LO for file in `find . -name "*.txt"`; do mv ${file##*/} ${PREFIX}_${file##*/}; done
Source: [1]
Replacing Text in Multiple Files
sed -i 's/oldtext/newtext/g' *.ext