1. Terminal, Shell, & Bash#
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:
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
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.
1.1. wtf is terminal?#
Terminal is a graphical-based application, providing access to the CLI
This is preinstalled on MacOS as the
terminal
appWindows has variations called
Command Prompt
andPowerShell
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)
1.1.1. Example: Opening the Terminal App#
You can find the terminal by:
Manually looking through your files for it
Open
launchpad
-> Look forterminal
.Open
Finder
->Applications/
->terminal
Search
Press
cmd
+[space]
. Then typeterminal
.
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.
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
)
1.2. $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 ascsh
,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
.”
1.2.1. Example: Bash Shell Commands#
In a shell
, predefined commands
are computer processes.
Commands
are typed and interpreted by the computer to run processesUsers provide
input
in the form of commands, and the computeroutputs
the resultTo 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:
pwd
echo
touch
cat
tail
head
1.4. What are Paths?#
Two types of paths:
Absolute - relative to the root directory (e.g.,
/home
,/Users
).Relative - relative to your current working directory.
File or directory references use one of these paths.
1.5. 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!
1.5.1. Copy using cp
#
Use
cp
to copy files or foldersThe
[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 inRemember to go back a folder, we run
cd ../
?../
points back on directory while./
means the current one
1.5.2. 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
1.6. 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
withNewFileName
orNewFolderName
1.6.1. 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
1.7. 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
1.7.1. Deleting Multiple Files#
# Deleting 2 files named, file1.txt and file2.txt
rm file1.txt file2.txt
1.7.2. 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/