Command

Intro to Bash

Image generated with ChatGPT
slides by Lukas Steinwender

Bash (Bourne Again SHell)

  • command interpreter for Unix-like operating systems
  • everything interpreted as string on basic level
  • case sensitive
  • metacharacters: \n, \ , \t, |, &, ;, (, ), <, >

used by many high-performance clusters (HPC) around the globe

slides by Lukas Steinwender

Startup Files

  • names and locations:
    • /etc/profile
    • ~/.bash_profile (typically used on Mac)
    • ~/.bash_login
    • ~/.profile
    • ~/.bash_logout
    • ~/.bashrc (typically used on Linux)
  • called whenever a new session is launched
  • can call other scripts from within them
  • allows for detailed customization of your Bash experience
slides by Lukas Steinwender

PATH

  • system variable containing executables
  • thats where your systems looks for executables
echo $PATH
  • adding additional executable
export PATH=$PATH:<path/to/executable>
slides by Lukas Steinwender

Bash Scripts

  • follow this structure
#!/usr/bin/bash
<your code>
  • #!/usr/bin/bash ... shebang, specifies shell environment to use

Sourcing

source <some bash script>
  • similar to import in Python
  • remember venv: source <path/to/venv>/bin/activate
slides by Lukas Steinwender

Useful Commands

slides by Lukas Steinwender

General

#control
sudo <command>      #the command to rule them all (superuser do)
pkill <name>        #kill process of name <name>
passwd              #changing your password                                                 
#navigation
cd <path/to/dir>        #change to dir
cd ~                    #change to home
cd ..                   #change one dir up
cd -                    #change to last checked out dir
pwd                     #print current working directory
ls <path/to/dir> [-a] [-l] [-r] [-t] #list files in dir #`-a`: all #`-l`: long #`-r`: reverse #`-t`: sort by time
#exploration
man <command>       #show manual for command
echo <expr>         #similar to print() #<expr> interpreted as string (use "$(expr)" to evaluate)
history             #display history of commands
head <path/to/file> -n <nlines>         #display first `nlines` lines of a file 
tail <path/to/file> -n <nlines> [-f]    #display last `nlines` lines of a file #`-f` : follow file changes
cat  <path/to/file>                     #display file body
less <path/to/file>                     #hybrid between `head` and `cat`
grep <string> <path/to/file>            #find <string> in a file
find -name <filename>                   #find file of name <filename>
slides by Lukas Steinwender

File Manipulations

touch <filename>                #create new, empty file
mkdir <dirname>                 #create new, empty directory
rmdir <dirname>                 #remove empty directory
rm [-r] [-f] [-i] <path/to/object>   #remove object #`-r`: recursive (for directories) #`-f`: force #`-i` interactive
cp <src> <dest>                 #copy file from `src` to `dest`
mv <src> <dest>                 #move file from `src` to `dest`
chmod <options> <path/to/file>  #change permissions #`-r`: recursive, `777` all permissions for everyone
du -h --max-depth=<n> <path/to/dir> #disk usage up to `n` child directories
ln -s <path/to/dir> <path/to/link/dest> #symbolic link (shortcut) to `dir` in `dest`
unlink <symlink name>                   #removes symlink without modifying original directory

NEVER remove the French language pack:
sudo rm -fr /*

slides by Lukas Steinwender

Interaction With Remote Machines

scp [-r] <user>@<source_host>:<src> <user>@<dest_host>:<dest>   #secure copy files from `source_host` to `dest_host` #`-r` recursive
scp <local/path/to/file> <user>@<dest_host>:<dest>              #local  --> remote
scp <user>@<source_host>:</path/to/file> <local/dest            #remote --> local
sudo rsync -avhP source dest                                    #`-a`: archive #`-v`: verbose #`-h`: human readable #`-P`: progress

Stats

quota       #list available storage resources (on OzStar)
slides by Lukas Steinwender

Keybindings

  • ctrl+r ... reverse search
  • ctrl+c ... kill current running process
  • ctrl+d ... exit shell

ctrl+r: the most powerful Bash command
(besides sudo)

slides by Lukas Steinwender

Let's Set Up a .bashrc!

  • add an alias alias ls="ls -i"
  • see here in case of difficulties
slides by Lukas Steinwender