1.2. Terminal, Shell, & Bash#

Fig. 1.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:
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.2.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.2.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.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
.”
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:
1.2.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.2.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.2.5.1. Copy using cp
#
# `cp` - copy files or folders
- 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`)
```bash
cp [source] [destination]
```
- Again, both `[source]` and `[destination]` can use absolute or relative paths
```bash
# 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`.
```bash
# 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
```
To copy file1 to file2 (new name):
```bash
cp [FILE_1] [FILE_2]
```
Copy folders:
```bash
cp -r [FOLDER_1] [FOLDER_2]
```
Use `cp` to copy files or directories from one place to another.
- `cp` creates NEW versions of the sources, so editing the copy won't affect the original (and vice versa).
**Note that it will overwrite the destination if it already exists.**
```bash
cp srcFile.txt clone.txt
```
or
```bash
cp -r srcDirectory/ dst/ # recursively copy
```
Copying can be down with paths
```bash
cp -r /path/to/dir_1 /path/to/dir_2
```
1.2.6. Moving & Renaming with mv
#
# `mv` - move files/folders from one place to another.
- Both are performed with `mv`
- Follows the same notation as `cp`
- **Does not require the option, `-r` !!**
```bash
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
```bash
# 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
```bash
mv directory path/to/destination/newname
```
`mv` is similar to `cp`, but it deletes the source.
`mv` is also useful for renaming files!
```bash
mv source.txt new.txt
```
1.2.7. Deleting Files and/or Folders with rm
#
# `rm` - delete files
```{caution}
This command is permanently delete your files / folders
No trash can to recover "accidental" events
```
- Use `rm` (Remove) to delete files/folders.
```bash
# Delete file1.txt
rm file1.txt
```
### Deleting Multiple Files
```bash
# 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
```bash
# Delete entire folder called directory/
rm -r directory/
# Force delete all contents with `-f`
rm -rf directory/
```
:::{code} bash
rm file.txt
:::
:::{code} bash
rm -r directory/
:::