Skip to content

Linux bash tips and tricks

Mehul Gajwani edited this page Aug 6, 2024 · 1 revision
  • You should be familiar with the bash syntax discussed in Andy's Brain Book (see Linux-and-bash-scripting).
  • One more important thing to add to that content is the use of wildcards. We suggest this tutorial.
  • A nice cheat-sheet with reminders of simple commands can be found here
  • For some more complex scripting commands, see reminders here.

Using export to define variables

Many pipelines (e.g. AFNI) require you to manually set up some information (e.g. where subject data is located or where files are installed) before they can be run. This is done by defining variables which can be easily accessed by sub-processes. Have a look at this tutorial for how export works.

source vs ./

Whilst using bash, you will commonly see two alternatives for running commands: > ./myscript ('executing') and > source myscript ('sourcing'). The difference is:

  • When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.
  • When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.

-- Lesmana at superuser.com

Assorted tips and tricks

  • 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
    • Or Ctrl + Shift + C to copy and Ctrl + Shift + V to paste
  • Ctrl + C will cancel any code you’re currently running
  • Ctrl + A will move to the start of the line, Ctrl + E to the end
  • 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.

Tips and tricks for MASSIVE

  • 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.
  • 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

Clone this wiki locally