Skip to content

Linux bash cheat sheet

Alexander Holmes edited this page Jul 5, 2023 · 11 revisions

Frequently Used Commands

Basic

$pwd

  • Will print the current working directory (e.g., the folder you currently have open in your terminal)

$cd

  • Will change the current directory to a new directory
    • cd ~ will take you to your home directory
    • cd ../ will take you to the previous directory (e.g., moving from ~/kg98/Shared to ~/kg98

$ls

  • Will return a list of all files and directories inside your current directory
    • ls –al [more files than $ls, due to showing invisible files]
    • ls –alrt [sorting by modify time]
  • Can also show certain files with specific names
    • ls *.nii
      • Will show all files ending in .nii (NIFTI file extension)
    • ls sub*.nii.gz
      • Will show all files ending in .nii.gz AND beginning with sub
    • Try experimenting with the wildcard characters * and ? to generate lists of filenames
  • $ls *.nii > list.txt
    • Adding ‘> list.txt’ will save your ls outputs to a text file named list.txt (or whatever you want to save it as
    • Very useful to generate a list of subject IDs from directory names or NIFTI files, etc

$mkdir [dirname]

  • Create new directory titled ‘dirname’

$rmdir [dirname]

  • Remove an EMPTY directory

$cp [source] [target]

  • Copies files
  • e.g., to copy a file called sub1_T1_a.nii and create a copied file called duplicate.nii:
    • cp sub1_T1_a.nii duplicate.nii
  • Can also copy and paste files into a different directory than the current working directory
    • cp /path/to/first/dir/sub1_T1_a.nii /path/to/second/dir/duplicate.nii
  • For directories, use “-r or –R” [copy directories recursively]

$mv [source] [target]

  • Move OR rename files and directories
  • mv sub1_T1.nii test.nii
    • Will rename sub1_T1.nii to test.nii
  • mv /path/to/first/dir/sub1_T1.nii /path/to/second/dir/sub1_T1.nii
    • Will move sub1_T1.nii from /first/dir to /second/dir

$rm [filename]

  • Remove file or directory
  • Be VERY careful

Useful management command that will return the file storage quotas on kg98 or the disk usage amounts of your current directory

  • $user_info
  • $du –csh

Advanced

Variables

Defining variables

export ${variable}=${some-value} or ${variable}=${some-value}

  • Define a variable called "${variable}" for a given string, file, or any value of interest
  • e.g., export i=sub-005 or export FS_LICENCE=~/kg98/my_fs_license.txt

echo ${variable}

  • Display and return a given variable
  • e.g., echo $i will return the value of your variable titled $i and print this output in your terminal

Applications of variables in subsequent commands

export DirName=test

mkdir $DirName

mkdir ${DirName}2

  • Will create two directories, one titled "test" and another titled "test2"
  • Curly brackets or quotes can be used when calling variable names to help separate the variable name from the rest of the string
  • This is why mkdir ${DirName}2 would create a directory titled test2, while mkdir $DirName2 would not (as the second instance would unsuccessfully try to find a variable called "DirName2" that does not exist

For Loops

General Use

for ${variable} in ${list of values}; do ; done

  • This is the general structure of a for loop in bash
  • e.g., for i in 1 2 3; do echo $i; done
  • This will print each value of $i in a new line in your terminal:

1

2

3

Applied Use

  • Suppose you have a list of subject IDs saved to a text file "subject_list.txt"

for subjectID in cat subject_list.txt; do echo $subjectID; done

  • will concatenate this text file and return every subject ID listed within.

  • This can then be used in more complex commands, for example:

for subjectID in cat subject_list.txt; do recon-all $subjectID -all; done

  • will run each subject through FreeSurfer's recon-all processing pipeline.

Note: These for loops run everything SERIALLY

  • This means you need to wait for the first value in the loop to finish running before the next value will run.
  • Basically, everything runs one at a time, which can be slow in some bigger datasets.
  • To run everything in PARALLEL, consider using a job array in SLURM.

Some other useful keys and shortcuts you’ll use often (in a terminal):

  • Highlighting text will copy it automatically
  • Middle button of mouse will paste
    • Otherwise use Edit – COPY/PASTE if you don’t have a middle button
  • Ctrl + C will cancel any code you’re currently running
  • Ctrl + A will move to the start of the line
  • Tab key = Auto-complete (e.g., if I want to open a directory titled ‘FreeSurfer’, typing cd Free then pressing tab will fill in cd FreeSurfer - VERY useful)
    • If there are multiple possible options, press the Tab key twice to see all the options
  • Arrow up/down key to check previous commands (also very useful)
  • Including an ampersand (&) at the end of your command will cause that command to run in the background. Otherwise, your terminal will display the entire process as it runs and wait until the command is finished before letting you run another command. However, not including an & doesn't mean you can't open a second terminal while your command runs tethered in the first terminal.
Clone this wiki locally