Evan’s Unix Tips & Tricks

Collected by Evan Lutkenhoff

Unix Commands for MRI

Command Action
startmricro opens dicom
imagej opens dicom
mricron opens dicom
xmedcon opens dicom
dcm2nii converts dicom to NIFTI
dcm2nii -c N -a N -i Y -r N -x N * converts dicom to NIFTI (puts name, date, and scan-type in name)
dcm2nii -c N * converts dicom to NIFTI and keeps in original directory
dicom2analyze converts dicom to ANALYZE
dicom_hdr view dicom header

FSL BET (Brain Extraction)

bet -c x y z -f 0.4
Opens bet. c flag is center of gravity (x y z coordinates needed). f flag expands the bet-mask (if f<0.5-expands mask, if f>0.5-contracts mask).

Renaming Files

Files: a_1.txt, a_2.txt, a_3.txt…
rename a new_a a*
Renames all files named ‘a’ followed by anything.
Results: new_a_1.txt, new_a_2.txt, new_a_3.txt…

Zip/Unzip Files

tar -cvzf name.tar.gz name
Zips file
tar xvzf name.tar.gz
Unzips file

Differences Between Folders

diff -rq directory1 directory2
Finds differences between two folders.

Commenting Multiple Lines

lines to comment out here
lines to comment out here
lines to comment out here
Comments out multiple lines of code.

Echo (to same line)

echo -n “whatever” >> test.txt
Echo to same line (append) without new line per loop and write out to test.txt.

Removing File Extensions and Paths (only for tcsh)

if var=image.nii.gz, then:
Removes .nii.gz extension.
Removes .nii extension.
Removes absolute path preceding file.
`$FSLDIR/bin/remove_ext $var`
Removes file extension for image using FSL (works in all shells).

Add File as Input for Script

sh script.sh test
Uses test as input file for script.sh, input saved as $1.
echo $1
Outputs test to screen.

Text Manipulation Within a File

tr ‘,’ ‘ ‘ test.txt > out.txt <- Put spaces between single quotes
Replace all commas in a file with spaces.
tr ‘\n’ ‘ ‘ < list.txt <- Put spaces between single quotes
Replace all new lines in a file with spaces.
sed ‘/^$/d’ input.txt > output.txt
Remove blank lines from a file and save to another file without overwriting the original.


du -sh directory
Determine the size of the directory in human-readable format.


find ./temp -type f -name ‘*test*’
Search current directory for a file with name containing word test in directory named temp.
find ./temp -type d -name ‘test’
Search for a directory named test in directory named temp.
find . 2>/dev/null
Suppresses error messages such as ‘Permission denied’.
find . -iname “whichevercase”
Performs a case insensitive search.
find . -name ‘*test*’ -exec cp {} /path \;
Search for files and copy them to a new directory named path.

Does File Exist?

if [-f $file];
then echo “File $file exists”
else echo “file $file does not exist”

Passing Variables Between Scripts

Two options:
1) You can make an environment variable before executing the second script. -> export TESTVARIABLE
2) You can source the second script which allows complex variables (e.g. arrays). -> . /absolute/path/script.sh

Cutting Text Within Complex File Names

for id in MNI152_T1_1mm_brain_mask_evan_A_B_C_3dSkullStrip.nii.gz; do
frontname=`echo $id | awk -F “_evan_” ‘{print $2}’`
name=`echo $frontname | awk -F “_3dSkullStrip.nii.gz” ‘{print $1}’`
echo $name
If input is MNI152_T1_1mm_brain_mask_evan_A_B_C_3dSkullStrip.nii.gz, then output is A_B_C.

IF ‘equal to’ Statement

if [$variable == 5.000]; then echo $id;
else echo “something else”;

Rsync over SSH

*on Monti desktop Ubuntu machine
1) Reformat external hard drive to ext4 (if not ext format already)
2) cd /path/to/destination/folder/
*3) rsync -avhc –rsh=ssh username@alf.psych.ucla.edu:/path/to/source/folder/ .

Replace String Using SED

sed -i “s/tobereplaced/replacement/g” test.txt
Replaces first string with second one WITHIN the same file.
sed “s/tobereplaced/replacement/g” input.txt > output.txt
Creates a new file with the second string replacing the first string.

Delete 1st Line of file Using SED

sed -i 1d file.csv

Complex String Manipulation

Example: old=”test_more_t1_test_t1″ <-We only want to remove the last t1
echo ${old%_*}
output: test_more_t1_test

Removes last part of string after last delimiter.

Replace character within variable (bash)

${id//;/ } replace ; with space in variable id

List Variations

ls -l | grep ‘^d’
Displays all directories.
ls -d */
Displays only directories.
ls -l | grep -v ‘^d’
Displays only files.

GREP Multiple Lines

grep -A1 error
This tells grep to include one line after match.
grep -B1 error
This tells grep to include one line before match.

Adding Comments/”How to use”

Usage() {
echo ” “
echo “your comments”
echo ” “
exit 1
[“$1″ = ” “] && Usage
Allows you to add comments or a “How to use” section to a script missing user input.

Exit Shell (if sub-command returns non-zero status)

At beginning of script, add:
set -e

Copy Directory Structure

cp –parents -r dir1 dir2
Copies folder and parent directory structure.

Read a Text File Line-by-Line

while read line; do
echo ${line}
done < testwhileloop.txt

Add a Preceding 0 to Numbers 1-9 in Cell D1 in Excel


Copy File into Multiple Directories

echo dir1 dir2 dir3 | xargs -n 1 cp file.txt

Copy Folders without Copying Attributes (owner, permissions, …)

cp -r –no-preserve all folder1 folder2

Delete all your SGE (Sun Grid Engine) jobs under your username

sge qdel -u username

Suppress all standard error output from command

command 2>/dev/null

Combine stderr and stdout into the stdout stream for further manipulation:

append to the end of command:


Write out stderr and stdout to separate log files (BASH):

append to the end of command:

2>stderr.log 1>stdout.log

Exclude wildcard matches in BASH

shopt -s extglob turns on extended globbing features


ls excludes anything saved in GLOBIGNORE

unset GLOBIGNORE unset ignore variable

shopt -u extglob turn off extended globbing features

Have Script Send Email Upon Completion

cat /path/to/file | mail -s “your subject” your@email.com

Where ‘path/to/file’ leads to the file where the body of your email will be stored.

Merge PDFs (or other imagetypes) and maintain quality

convert -density 150 input1.pdf input2.pdf output.pdf

*use density=150 for PDFs