2.2. BASH commands#
Interactive page
This is an interactive book page. To solve quizzes, press the launch button at the top right side of the page (look for a small rocket icon) and then Live Code.
To begin familiarizing yourself with the terminal, we first provide an overview of commonly used BASH commands (Table 2.1), which are explained in more detail below on this page. The overview is not exhaustive - if you want to perform an action not covered by the table, search online to find an appropriate command. To run a command on your computer, simply open your terminal, type in the command, and press Enter.
Command |
Effect |
Details |
---|---|---|
|
Print working directory |
Display the location (path) of the directory in which you’re currently working. |
|
Change directory |
Navigate between directories. To go to the home directory, use |
|
Make a new directory |
Creates a new empty directory with the user-defined name. |
|
Remove a directory |
Deletes a directory if it’s empty. See also |
|
List contents of a directory |
Use with |
|
Open a file to view its contents |
Doesn’t allow for file editing. |
|
Print file contents to terminal |
If used with more than one file, it concatenates them and prints the result in the terminal. |
|
Copy |
Copy a file to another directory, or make a file copy in the same location. To copy a directory with all its contents, use |
|
Move / Rename |
Moves or renames files. Renames directories. To move directories with their contents, use |
|
Remove |
Deletes a file. |
|
Search |
Search for text patterns. |
|
Clear terminal window |
Clears commands and outputs to declutter the terminal. |
|
Current folder |
|
|
One folder up (parent folder) |
|
|
Wildcard |
Replaces any text. |
|
Redirect |
Instead of printing output of running a script in the terminal, with |
Online information
Don’t hesitate to look for information online (Google, StackOverflow, ChatGPT) when you’re not sure how to do something, or what is the exact command that you need to use.
2.2.2. Manipulating directories#
With the terminal you can easily create (mkdir A/
) or delete an empty directory A/
(rmdir A/
), where A/
can be something like Movies/
in the figure above.
You can also use rm
to delete directories, cp
to copy, and mv
to move and rename them.
If you have a directory with files inside (i.e, a non-empty directory), you cannot use rmdir
to delete it. In order to delete a non-empty directory with all its contents, you need to use rm
in a recursive fashion: rm -r A/
.
Similarly, if you wish to make a copy of a directory (with all its contents), this also has to be done recursively. Notice the nuanced differences between the following commands:
cp -r A/ new_A/
- makes a copy of the source directoryA/
with all its contents; the new folder is callednew_A/
.cp -r A new_A/
- same as previous command.cp -r A/ existing_A/
- copies the contents of the source directoryA/
to the existing destination directoryexisting_A
.cp -r A existing_A/
- makes a copy of the directoryA/
with all its contents inside the existing directoryexisting_A
. Therefore, the presence or absence of the trailing slash/
in the source directory determines whether the entire directory is copied as a subdirectory or only its contents are copied.
In addition to copying, we can also move directories.
To move directory A/
with all its contents into directory B/
, we can use mv A/ B/
(note: mv
doesn’t differentiate based on the presence or absence of a trailing slash in the source directory).
We can also use mv
to rename a directory like this: mv A/ new_name_A/
.
Try to use rm
with one of your non-empty folders. What message does the terminal return?
In your terminal:
Navigate to your desired working directory (this could be, e.g.,
Documents/
orDesktop/
).Create two new directories: name them
A
andexisting_A
.Copy any file to directory
A/
. Because we don’t yet know how to do it in the terminal, you can do it using your mouse and GUI.From your working directory, try to run the
cp
andmv
commands mentioned above and observe what the effects are. Note: you may need to “reset” your setup with two folders depending on which command you ran.
Recalling previous commands
Sometimes you want to use the same command you used recently in your terminal, either to run it again, or make a small modification and then run it.
There’s an easy way to fetch your previously executed commands instead of retyping them: you can simply press arrow up (↑) once or multiple times.
To print a full list of recent commands, use history
command.
Try it in your terminal!
2.2.3. Working with files#
Several commands that apply to working with directories also apply to files. Let’s take a look at what the following commands mean:
cp a.txt b.txt
- copiesa.txt
tob.txt
in the same directory. Usingmv
instead ofcp
would renamea.txt
intob.txt
.cp a.txt ../
- copiesa.txt
to one directory up (it can also be used withmv
to move the file).cp a.txt A/
- as previous command, but copiesa.txt
in the directoryA/
(can also be used withmv
).cp ../a.txt .
- copiesa.txt
from one directory up to the current (.
) directory (it can also be used withmv
).
2.2.3.1. Wildcard#
Sometimes you will want to copy or move mutiple files or directories at the same time.
To avoid typing each command individually or recalling the same command and changing it multiple times, you can use the wildcard *
to replace any text.
For instance, let’s say we are located in the directory Physics
:
If we are in the directory Physics
and want to move all Notes files into directory Materials/
, we could use mv Notes* Materials/
.
With Notes*
, we included all three .docx
files. Therefore, *
effectively means Nov21.docx
or Nov24.docx
or Oct16.docx
.
We could alternatively use mv N* Materials/
because our three files with notes are the only files beginning with letter N, so mv N*
already defines our selection. Another option would be to use mv *docx Materials/
because Notes are the only .docx
files in the directory Physics/
.
Note that it’s also possible to use multiple wildcards in one go if there are several patterns you wish to generalize. E.g., to delete all .docx
files that have Nov in their name, you can use rm *Nov*.docx
.
import micropip
await micropip.install("jupyterquiz")
from jupyterquiz import display_quiz
import json
with open("question2.json", "r") as file:
questions=json.load(file)
display_quiz(questions, border_radius=0)
2.2.3.2. Reading files#
For taking a glance at the contents of a text file in read-only mode, less
comes in very handy.
To open a file for reading, we use less file.txt
.
We can further navigate through the text of a file by scrolling or search for specific text patterns like this: /Pattern
(note that this is case sensitive, i.e., there is a difference between /Pattern
and /pattern
).
To navigate to the end of the file, use g
. To move to the beginning of the file, use G
.
To close the file, simply press q
(for “quit”) on your keyboard.
An alternative command used to read the contents of a file is cat
.
Unlike the less
command which opens a file, cat
prints a file’s contents to your terminal.
If you use it with more than one file, e.g., cat fileA.txt fileB.txt
, it will concatenate the files and print the output in the terminal.
2.2.3.3. Editing files with Vim#
Vim is a powerful text editor which you can use from within the terminal.
To open a file for editing, you can simply type vim file.txt
.
If a file with the given name doesn’t exist, Vim will create a new empty file carrying that name.
Unlike text editors such as Notepad or MS Word, Vim is entirely command-based.
That means that even simply writing some new text in the file requires extra steps instead of just starting to type (concretely, you have to press i
to let Vim know that you wish to type new text).
Upside: you can’t accidentally add new text, which is actually quite useful when working with scripts.
Vim does take some getting used to, but is ultimately a powerful text editor and usually comes pre-installed (e.g., when you remotely connect to a supercomputing cluster such as DelftBlue).
Command |
Effect |
Details |
---|---|---|
|
Insert text |
|
|
Escape current mode |
If we’re in insert mode with |
|
Undo |
|
|
Delete |
Delete current line. To delete characters, you can go in the |
|
Delete a character |
Deletes a character at the position of the cursor. |
|
Copy-paste |
|
|
Replace |
Replaces |
|
Pattern search |
|
|
Next |
Pressing |
|
Previous |
Opposite of |
|
Beginning |
|
|
End |
|
|
Move cursor |
Moves cursor one position left/down/up/right. |
|
Line numbers |
Turns on line numbering. |
|
Visual mode |
For identifying pieces of text to be manipulated. See more here. |
|
Function |
Allows for defining custom functions. |
|
Write changes |
Save made changes in the file. |
|
Close file |
|
Open your terminal and navigate to your working directory (e.g., Documents/
). Execute the following command: vim my_file.txt
. Try to first add some text to your new file using i
mode. Explore other commands outlined in the table above before saving and closing your my_file.txt
with :wq
.
Advanced example: a custom replace function
Commands in Vim can be concatenated. In this example, we use a set of commands for replacing character “A” with character “B” in all columns of 1,000 subsequent rows. Note that if we wish to replace only a specific “A” to “B” in 1,000 lines, this task would become tedious in MS Word or another similar program. In Vim, we would position our cursor on first out of 1,000 “A” characters that need to be changed and use these commands:
q a i Delete B Esc j q 1000@a
These commands translate to:
q
= start function definition
a
= function name
i
= begin text edit mode
Delete
= delete “A”
B
= replace it with “B”
Esc
= exit text edit mode
j
= move cursor to line below
q
= finish function
1000@a
= execute function a
1000 times