This chapter introduces the bash command line, an interactive terminal native to most Linux distributions as well as MacOS.
In the command line, you enter commands to navigate directories, manipulate files, and run programs. You will frequently use the command line to manage your project environment.
A Note on Terminology: While there are subtle distinctions in their definitions, we can use the terms "terminal" and "command line" interchangeably in this course. However, if we wanted to be a bit more technical here, we could refer to the different variants of terminal programs we use in this course (depending on the student's operating system) as part of the "Unix Shell" .
The "Unix Shell" is the application that provides the command line interface for "Unix-like" operating systems, which today are pretty much (1) Linux and (2) MacOS. The specific variant / configuration of the Unix Shell that we focus on in this course is Bash, since this variant is the default Unix shell for Linux. It was previously the default shell for MacOS until about ~2019, when it was replaced by a slightly different variant, called Zsh. However, the slight difference between Bash and Zsh is negligible in this course, and we can -- for all intents and purposes -- treat them the same.
So the next logical question becomes: Why are we so "Unix"-centric in this course?
This is not due to our personal preference / our own bias, but rather due to the state of the industry: Most cloud computing is done on Linux servers, most of the software we use in this course installs more easily on Linux and MacOS, and the Data Science community in general has gravitated towards Linux as its preferred / recommended operating system. Since MacOS is in the same family as Linux (and uses the same command-line language), this makes transitioning to Linux nearly seamless. For Windows users, not using Ubuntu alongside their Windows OS, unfortunately, this is not the case -- but don't worry! Even though we won't use the Windows command line (known as cmd.exe, cmd, or the Command Prompt) in this course, using a tool like Git Bash allows you to run Bash commands inside of your Windows machine.
If you have no previous experience with Unix-like systems or only know a few commands but would like to know more, this tutorial is a great start.
Note:
This tutorial works on the Linux bash, the MacOS terminal,on Windows using the Git bash, given that Python 3 is installed onyour system.
In this tutorial, you will be looking for a word with 11 characters:
All characters are hidden in the exercises below.
All exercises can be solved using a Unix-like terminal (including MacOS terminal) as well as Git Bash for Windows.
- download the Bash Tutorial.
- unzip the file
- open a terminal and navigate to an appropriate workspace / directory.
- navigate into the bash_tutorial directory.
- when you type
lsit should look like this:
The first character is hidden in a file somewhere in the exercise1/
directory tree. Use the commands
cd <directory_name>to move from one directory to the next. (do not type the pointy brackets,
just insert the directory name after the cd command.)
Once you've changed to a new directory, you can use
lslist the directory's contents. Look through subdirectories
until you find one with the name solution_1.1 and list its contents.
If you went to a wrong directory, you can go back one level by typing:
cd ..or go back to your home folder:
cd or
cd ~Show a Hidden File
Some files are not visible immediately. To see them, you need the command
ls -a The second character is in the same directory as the first one, but in a hidden file.
Use cd .. to go back to the directory exercise_1/directoryB/. When
listing its contents, you should see a shell script file called program.sh.
To find the third character, you need to execute the program.
In bash, this can be done with the source command:
source program.shHint:
When typing names of directories or files,
press [TAB] after the first few characters.
Unix tries to guess what you are typing.
Go to the folder exercise_1/directoryC/. To find the fourth
character, you need to find out how big the text file in the directory
is. This is done with the command
ls -lIn the table the command produces, you will find the file size (in bytes) next to the date/time when the file was last modified.
Look up the file size (decimal value) in the
Table of printable ASCII characters
to obtain the corresponding character. For example, if the file size is 65 bytes,
then the fourth character would be the letter A.
table of ASCII characters, Public Domain
Warning:
We've noticed in the past that Windows users, get a slightly different number than the Linux / Mac users, presumably due to a small difference in how the Windows OS treats line breaks.
So for this exercise, we'll give you the answer --until we eventually phase out this question of the tutorial and replace it with something that works the same way on all computers. Please pardon the inconvenience!
Please use cd .. to go back to the top directory of the tutorial
material. Then, change to the directory exercise_2.
In the directory exercise_2/, you will find a text file solution_2.1.txt. The fifth character is inside that file. To see its contents, use the command
less <filename>Hint:
Press q to leave the display mode.
To get character number six, you will need to create a text file in
the exercise_2 directory. On most Unix-like systems, you can do this
using the nano editor. To start using the nano editor directly, you can simply
run the nano command, or you can simultaneously create a new file:
nano <filename>After typing in something in the body of the file (e.g. the letters you have
found so far), you will need to save and exit the program. You'll notice options
at the bottom of the screen referencing the keyboard shortcuts needed for
interacting the the program. (Note that the ^ symbol means the control key).
The sixth character is the letter next to the control key for
saving, or "writing out" a file in nano.
Hint:
If you want to know more about a particular command, type
man
You get shown a help page that you can leave by pressing 'q'.
Please navigate inside the exercise_3 directory.
To find characters seven and eight, create a directory named solution/
and copy the files from the part1/ and part2/ directories into it.
For creating directories, use the command:
mkdir <directory name>To copy the contents of the part1/ and part2/ directories into
solution/, run the commands:
cp part1/* solution/
cp part2/* solution/Type ls -l solution/* to points towards the solution.
Hint:
What does the * symbol mean?
In the data directory, all files with an Y in their name need to be deleted.
To remove a file, use the command:
rm <filename>There are many such files to be deleted in the data directory. To
remove more than one file at once, you can use * symbol as a wildcard:
e.g. rm ju* will delete all of junk.txt, juniper.txt and
june.docx.
To get characters nine and ten, look at the files that remain after
deleting those that contain a Y.
Hint:
To remove an empty directory, you can use
rmdir <directory name>The command
rm -r <directory name>deletes a directory and everything in it.
Warning:
On Unix, it is not possible to un-delete files!
This makes removing files with the * symbol very dangerous,
because you could wipe out everything
with a single command
(e.g. if you type the wrong directory by accident).
Backups become an even better idea after learning this command.
Please go to the directory exercise_4.
To find the final character, you will need to run the python script file.py.
Luckily, python runs natively in the command line (e.g. bash terminal),
so running a python script can be done simply by invoking the python interpreter
followed by the name of the script:
python3 file.pyThe solution to the final character will be printed to the console.
- Create a new directory
secret_chamber/ - Create an empty text file
chestinside it - Store the word treasure inside the chest file
- Lock the chest: remove read/write permission for everybody (Hint: This does not work with Git Bash in Windows)
- Step out of the chamber to the parent directory
- Hide the chamber by renaming it to
.secret_chamber/ - Read the contents of the chest again
- Create a new directory
fruit/ - Create 7 empty text files like
apple,bananaetc. - Store all file names in a text file
- List all file names that contain the letter
a - Store the names with
ain a text file - Use
diffto find all file names not containing ana
- Write a bash script
hello.shthat prints 'Hello World' - Execute the program with
source hello.sh - Modify
hello.shto print a message given as an command-line argument (accessible via$1) - Test the script with
source hello.sh 'Hello World' - Write another bash script that launches
hello.sh10 times with different messages - Execute everything
Recap Questions❓❓❓
- What does
cd ..do? - What is the difference between
cd home/andcd /home? - Explain the output of
ls -la - How can you repeat the last command you entered?
- When does TAB-completion work?
- How can you look up the documentation of bash commands?


