Terminal, Shell, & Bash#

https://raw.githubusercontent.com/van-richard/CodingNotes/main/_static/gif/terminal-header.gif

Fig. 1 Example of command line in the terminal application of MacOS. The line you are working on is noted with the $. The format of this line is: computer name, ; , username, and then $. Bash commands are typed by line, and ran by hitting the [enter] key.where, (1) :term:echo will return "[some text]" as an output, (2) :term:echo to output “2 + 2”, the | (pipe) takes this as input for the next command, bc -l (calculator), (3) echo prints the variable, $SHELL, which points to the program /bin/bash, and (4) cowboy.#

In this section, we’ll delve into the basics of the terminal, its role, and why it’s crucial in computational research.

Approaches to “using” computer programs:

  1. A common way makes use of a Graphical User Interface (GUI) - Programs that use a mouse and keyboard with on-screen menus, buttons, sliders, etc. - Relies on the user to click on-screen options for specific instructions

  2. Alternatively, computer programs can be operated via the Command Line Interface (CLI) - Uses text-based instructions, only requiring input from the keyboard - Often used for computing many files or automating repetitive tasks.

Wtf is a Terminal?#

Terminal is a graphical-based application, providing access to the CLI

  • This is preinstalled on MacOS as the terminal app

  • Windows has variations called Command Prompt and PowerShell

Using command line is a fundamental tool in computational research, offering advantages like:

  • More control in running applications

  • Faster management across operating systems (OS)

  • Ability to automate repetitive tasks (scripting)

Example: Opening the Terminal App#

You can find the terminal by:

Manually looking through your files for it

  • Open launchpad -> Look for terminal.

  • Open Finder -> Applications/ -> terminal

  • Search

  • Press cmd+[space]. Then type terminal.

To use a Linux terminal on a Windows machine, you will need to install the Ubuntu application. This will differ if you’re using Windows 10 or Windows 11.

I will write this up, but here is my reference.

Install Ubuntu on WSL

Several data analysis tools and computing servers require proficiency with the command line. It may have a steep learning curve and be a bit confusing initially, but once you get the hang of it. Automation will be E Z!

  • Notation and pre-defined commands will depend on your shell!

Users can send instructions to the computer by running commands

  • Like GUIs, these instructions can be for manipulating files or running software

  • Often, a command is an abbreviation of common words (copy = cp, move = mv)


$SHELL#

When you open terminal, a shell session starts immediately, containing the current state or environment

  • Only one session per shell exists

Shells are programs that understand and execute user commands:

  • interpreting human language (text input) into computer language (output)

  • Various shells are available, such as csh, zsh

I use bash shell, so my notes will only cover bash!

An interactive prompt is when users run each “command” line by line.

Note

Later, we will see that combining multiple command lines into one file is called a “shell script.”

Example: Bash Shell Commands#

In a shell, predefined commands are computer processes.

  • Commands are typed and interpreted by the computer to run processes

  • Users provide input in the form of commands, and the computer outputs the result

  • To update the output, run the command again.

Some commands can be modified by options following the command with a hyphen -. Others take arguments as input. Look at the following examples:

  1. pwd

  2. echo

  3. touch

  4. cat

  5. tail

  6. head


What are Paths?#

Two types of paths:

  1. Absolute - relative to the root directory (e.g., /home, /Users).

  2. Relative - relative to your current working directory.

File or directory references use one of these paths.


File Management#

  • How to copy, move, rename, and/or delete files?

  • Tasks like copying, moving, renaming, and deleting files/folders (cp, mv, rm) need to be typed!

Copy using cp#

  • Use cp to copy files or folders

    • The [source] is the original file/folder or /path/to/OriginalFile or /path/to/OriginalFolder

    • The [destination] is where you want the new copy to go, this can be in the same folder, or to another path (/different/path/to/NewFile)

cp [source] [destination]
  • Again, both [source] and [destination] can use absolute or relative paths

# Copy file in current working directory, OriginalFile.txt, to another directory in /path/to/destination and name it NewFile.txt
cp OriginalFile.txt /path/to/destination/NewFile.txt 

# Copy file from another directory here (.)
cp /path/to/OriginalFile.txt . 
  • The . means ./ or the current directory you’re in

    • Remember to go back a folder, we run cd ../ ?

    • ../ points back on directory while ./ means the current one

Copy Folders with cp -r#

  • Copying a directory requires the option -r.

# Option, -r, tells cp to copy the entire folder to a new path
cp -r directory /path/to/destination

# Can also copy things "here" (.)
cp /path/to/directory .

# Copy directory in the current working directory using the name, directory2
cp -r directory directory2

Moving & Renaming with mv#

  • Both are performed with mv

  • Follows the same notation as cp

  • Does not require the option, -r !!

mv [source] [destination]
  • [source] can be a file, folder, /path/to/file, or /path/to/folder

  • [destination] can be /path/to/file, or /path/to/folder with NewFileName or NewFolderName

Move/Rename Files and Folders#

  • Moving a file is straightforward; you can even rename it at the destination

# Move file.txt to the folder, destination, in /path/to, and rename it to newname.txt
mv file.txt /path/to/destination/newname.txt
  • Same for folders

mv directory path/to/destination/newname

Deleting Files and/or Folders with rm#

Caution

This command is permanently delete your files / folders No trash can to recover “accidental” events

  • Use rm (Remove) to delete files/folders.

# Delete file1.txt
rm file1.txt

Deleting Multiple Files#

# Deleting 2 files named, file1.txt and file2.txt
rm file1.txt file2.txt

Deleting Folders#

  • Like cp, you need the option -r to delete a folder

  • -f can force delete

# Delete entire folder called directory/
rm -r directory/

# Force delete all contents with `-f`
rm -rf directory/