Most lab work — fMRI preprocessing, container runs, dataset transfer — happens on a remote Linux box (workstation or the LRZ cluster). This page is a survival kit: enough shell to navigate, edit configs, move data, manage permissions, and watch processes. Companion pages: tmux for persistent sessions, LRZ Cloud Computing for the cluster, Tips and Tricks for assorted gotchas.
Shells such as Bash, Zsh, and Tcsh provide command-line environments for Unix-like systems, each with slightly different syntax. Bash is the default on most servers; Zsh adds better completion and plugins; Tcsh shows up because AFNI uses it. Stick with Bash unless a tool tells you otherwise.
1 Files, directories, and navigation
Print Working Directory (pwd) shows the current directory.
pwd
List the files in a directory (ls); -l is long format, -h is human-readable sizes, -a includes hidden dotfiles.
ls -lah directory
To list files recursively, use ls -R.
Change directory (cd):
cd to/sub/foldercd ~ # homecd - # previous directorycd .. # up one level
Copy (cp), move/rename (mv), remove (rm):
cp original duplicatecp -r folder1 folder2 # recursive copy for directoriesmv old_name new_namerm file.txtrm -r folder/ # recursive; be careful
Make a new directory (mkdir -p) or remove an empty one (rmdir):
Arithmetic expansion: $[] or $(()) evaluates the expression. echo $[3 + 2*4] outputs 11.
Variable expansion: build strings from variables.
a=subecho ${a}-01.txt # outputs: sub-01.txt
Braces {} disambiguate when the variable name sits next to other characters: echo $ab.txt looks for the variable $ab, whereas echo ${a}b.txt outputs subb.txt.
3 Searching and text processing
grep — find lines
grep PATTERN file prints matching lines. Common flags:
-c: count matches instead of printing
-i: ignore case
-l: list filenames with matches
-n: include line numbers
-v: invert (lines that do not match)
-r: recursive into subdirectories
grep -rni "task-bisection" $BIDS_DIR
wc — counting
wc (word count) prints characters, words, and lines. -c, -w, -l select just one.
For example, count trials with curDur = 0.5 from Exp1.csv (column 8 is curDur):
sort orders lines; -r reverses, -n sorts numerically, -f is case-insensitive. uniq collapses adjacent duplicate lines, so always sort first. uniq -c prefixes each line with its count.
sed 's/unix/linux/' aText.txt # first match per linesed 's/unix/linux/2' aText.txt # 2nd match per linesed 's/unix/linux/g' aText.txt # all matchessed 's/unix/linux/3g' aText.txt # from 3rd match to end of line
The delimiter / can be | (handy when replacing paths).
awk — column-aware tool
awk treats each line as columns. One-liner: print column 2 where column 3 equals 0.5:
awk -F, '$3 == 0.5 {print $2}' Exp1.csv
Task
Suppose you want to rename all filenames containing _run to _task-bisection_run. Think about pipes.
Solution
ls -R | grep _run | sed 's/_run/_task-bisection_run/'
4 SSH, file transfer, and remote work
SSH basics
ssh user@host.lrz.de
Generate a key pair (Ed25519 is the modern default):
ssh-keygen -t ed25519 -C "your.email@lmu.de"
This creates ~/.ssh/id_ed25519 (private — never share) and ~/.ssh/id_ed25519.pub (public — copy to the server). Add the public key to the server’s ~/.ssh/authorized_keys, or use:
ssh-copy-id user@host
~/.ssh/config — your friend
Set up short aliases in ~/.ssh/config:
Host lrz
HostName login.lrz.de
User di12abc
IdentityFile ~/.ssh/id_ed25519
Host workstation
HostName 10.0.1.42
User mystudent
IdentityFile ~/.ssh/id_ed25519
Now ssh lrz is enough. See also LRZ Cloud Computing for cluster-specific setup.
scp vs rsync
scp is fine for a single file. For datasets — and especially anything that might be interrupted — use rsync: it resumes, skips unchanged files, and shows progress.
# single filescp results.csv user@host:/path/to/# directory, resumable, with progressrsync -avz --progress local/directory user@host:/path/to/destination# pull from remotersync -avz --progress user@host:/path/to/directory local/directory
Flags: -a archive (preserves permissions, timestamps), -v verbose, -z compress in transit. Add --dry-run first to preview.
wget vs curl
Both download files. wget is simpler for “grab this URL into a file”; curl is more flexible (REST APIs, custom headers, uploads).
Long jobs over SSH die when your connection drops — unless you run them inside a terminal multiplexer. screen is the old standby; the lab uses tmux (see tmux).
5 Permissions and ownership
Linux permissions cover read (r), write (w), execute (x) for three classes: user, group, other. ls -l shows them as -rwxr-xr--.
chmod 600 ~/.ssh/id_ed25519 # private key: only you can read/writechmod 755 my_script.sh # owner rwx, group/others rxchmod -R u+rwX,g+rX,o-rwx /data/private
Change ownership / group:
chown newuser file.txtchgrp -R labgroup /path/to/shared # recursively assign group
Group ownership matters on shared servers: set the group of a shared dataset to your lab group so collaborators can read it.
6 Disk space and process monitoring
Disk usage
df -h # free space per mount, human-readabledu -sh $BIDS_DIR # total size of a folderdu -sh */ | sort -hr # subdirs of cwd, largest first
Processes
top # live process list (everywhere)htop # nicer interactive top (if installed)ps aux | grep fmriprep # find a specific processkill <PID> # ask process to quitkill -9 <PID> # force kill (last resort)
GPU
nvidia-smi # GPU usage, memory, processeswatch -n 2 nvidia-smi # refresh every 2 s
You’ll edit .bashrc, ~/.ssh/config, job scripts. Two options on every server:
nano (easy)
nano ~/.bashrc
Bottom of the screen shows the shortcuts. Ctrl-O save, Ctrl-X exit, Ctrl-W search.
vim (powerful)
vim ~/.bashrc
Modal: starts in normal mode. Press i to enter insert mode, type, then Esc back to normal. Save and quit with :wq, quit without saving :q!. That’s enough to survive — invest more time later if you stick with it.