Bash

Bash is a languauge used in UNIX environments. The basic/most used commands are:

ls         # list files
cd         # change directory
mkdir      # make directory
touch      # make file
cat        # print contents of file
mv         # move file
cp         # copy file
rm         # remove file
chmod      # change file permissions
man        # view mannual pages

You can see a more comprehensive list here

Useful bash commands

Keyboard shortcuts

Ctrl + A    # move to start of the line
Ctrl + E    # move to end of the line
Ctrl + C    # kill the current command
Ctrl + Z    # put command to sleep (bg & fg used to run in background & foreground respectively)
Ctrl + R    # search previous commands
Ctrl + L    # clear the screen

Commands

cd -                            # return to previous directory
du -h --max-depth=1 | sort -hr  # check folder size & return largest to smallest
export PATH=$PATH:/usr/bin      # add /usr/bin to the path
find . -name "pattern" -print   # print files & directories in the current directory that match pattern
tar -xvzf                       # extract .tar.gz file
which                           # find location of executable
grep <pattern> <filename>       # find pattern in file
sed 's/a/b/g'                   # replace a with b
uname -a                        # shows kernel information
df -h                           # return storage/disk spaace information
wget                            # download file

Scripting

varname=value           # declare a variable
array=(valA valB valC)  # define an array
${array[i]}             # displays array's value for this index
$(UNIX command)         # command substitution: runs the command and returns standard output
if condition
then
  statements
[elif condition
  then statements...]
[else
  statements]
fi

for x in {1..10}
do
  statements
done

for name [in list]
do
  statements that can use $name
done

while condition; do
  statements
done

Other

Store official binaries in: /usr/bin Store unofficial binaries in: /usr/local/bin

Tools

Use axel to download large files in parrallel

awk

Append chr to bed file:

awk '{print "chr" $0;}' genes_hg19.bed > chr_genes_hg19.bed

Print columns from comma separated file:

awk -F',' '{print $3}' myfile.txt

Read file as tab delimited:

awk 'BEGIN {OFS = FS} {print $3}' file.tsv

Print columns tab separated:

awk '{print $1,"\t",$2}' file.tsv

Print lines where the value in the first column is true:

awk '$1 == "true" {print $0}' file.txt

tmux

Tmux lets you run multiple sessions/programs within one terminal. On Mac iTerm2 has good support for tmux

# open new tmux session
tmux

# run command

# disconnect 
Ctrl + B, D

# list sessions
tmux ls

# attach session
tmux attach
# or
tmux a -t myname

# leave
exit

See a more complete list of tmux commands here

Previous
Next