Shell basics

This lesson covers some basic commands in Shell terminal. First, let’s take a quick look at some useful commands.

Basic commands

  • pwd
  • ls
  • cp: copy file
  • mv: move file
  • rm: remove file
  • rmdir: remove directory
  • mkdir: create directory
  • touch: create file
  • uname: retreive important information about system

In jupyter notebook, we can run it by adding a leading exclamation mark.

!uname 
Darwin


Note: Some command comes with their own flags. For example, three important flags of ls are -1, -a, and -l.

  • ls -1 will list the subdirectories and files vertically.
  • ls -a will list ALL subdirectories and files, including the dot files.
  • ls -l will list the subdirectories and files while including the permission for write/read/execute access.

Note: Shell scripting is a bit different as it is about collecting a set of commandas and running them together one afther another. It gives you a power for manipulating files. So, let’s say you want to reduce the running time of a function. To manage different versions of the same function, you use git (Read more here: https://nimasarajpoor.github.io/learn_git/Git.html), and have different versions of the same function in different branches. But, how are you going to compare the results? One way is to switch between branches and run the code. Another way is to write a shell script that checkout each branch, run the script, and write the result into a text file.

#!/bin/bash

git checkout main

rm -rf runtime_data.txt
touch runtime_data.txt
 
for i in "$@"
do
echo "branch: $i" >> runtime_data.txt
git checkout $i | python check_runtime.py | tee -a runtime_data.txt
echo "-----------------------------------" >> runtime_data.txt
done

git checkout main

Some resources:

  • https://www.learnshell.org
  • https://ryanstutorials.net/bash-scripting-tutorial/
  • Corey Schafer Videos