This tutorial makes you familiar with bash, the Linux command line. You will learn to:
- navigate directories
- manipulate files
- execute programs
If you have no previous experience with Unix-like systems or know a few commands but would like to know more, this tutorial is for you.
This tutorial was prepared for Ubuntu Linux, but it works on MacOS, Cygwin and the Git bash as well, given that Python 3 is installed on your system.
In this tutorial, you will be looking for a phrase with 25 characters:
The characters are hidden in the exercises below.
- clone the repository
- locate the
exercises/
folder - open a
bash
terminal
The first two characters is hidden in a file somewhere in the exercise1 directory tree. Use the commands
cd <directory_name>
(do not type the pointy brackets, just insert the directory name) and
ls
to move from one directory to the next. 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
1.2. Show a hidden file
Some files are not visible immediately. To see them, you need the command
ls -a
The next two characters, 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
program.sh
. To find the fifth character, you need to execute the
program. On bash, this is done by typing source and the name of the
program:
source program.sh
Go to the folder exercise_1/directoryC/
. To find the sixth
character, you need to find out how big the text file in the directory
is. This is done with the command
ls -l
In the table the command produces, you will find the file size in bytes, the file’s owner, permissions to read and modify it, and the date/time of the last modification.
To obtain the fourth character look up the file size in the Table of printable ASCII characters:
ASCII Table, Public Domain
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 seventh character is inside that file. To see its contents, use the command
less <filename>
To get character number eight, you will need to create a text file in
the exercise_2
directory. On Ubuntu, you can do this using the editor
nano
. You can start it typing the name of the program, or
nano <filename>
To exit nano, type Ctrl-X
Create a text file with the characters you have found so far.
The eighth character is the first letter of the command represented by Ctrl-X
If you want to know more about a particular command, type
man <command>
You get shown a help page that you can leave by pressing 'q'.
Please go to the directory exercise_3.
To find characters nine and ten, you need to create a
subdirectory named solution in exercise_3/
and copy the files from
the part1/
and part2/
folders into it.
For creating directories, use the command:
mkdir <directory name>
For copying, you can use the command
cp <filename from> <filename to>
Type ls -l solution/*
afterwards to see the solution.
In the data
directory, all files with an Y
need to be deleted. To do
so, use the command:
rm <filename>
Also, there are more files to be deleted in the data directory. To
remove more than one file at once, you can use *
as a wildcard, i.e.
rm ju*
will delete all of junk.txt, juniper.txt
and june.docx
.
To get characters eleven and twelve, look at the names of the files that remain after
deleting all that contain a Y
.
To remove an empty directory, you can use
rmdir <directory name>
The command
rm -r <directory name>
deletes a directory and everything in it.
On Unix, it is not possible to undelete 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.
There are two different versions of a quote, ai.txt
, and
artificial_intelligence.txt
. To find out, how they differ, Unix
provides the command
diff <filename1> <filename2>
Of course, you can look at the text first using less
or nano
. The
13th character of the solution is the single mistyped character in which the
two files differ.
Unix has a small program to sort text files alphabetically. It is called by
less <filename> | sort
The symbol '|' is called a pipe and is often used to connect Unix programs to each other. The 14th character of the solution is the third to the last character of the last word in the alphabetically sorted file elephant.txt. And the 15th character is the second to the last one of the animal part mentioned in the text.
less <filename> | sort > result.txt
To look for specific words in a text file, use the command
grep <word> <filename>
It produces all lines from the given file that contain the given word.
The grep
command is very powerful and can handle Regular Expressions.
To find the 16th character, search for the word has in the file
datascience.txt
and take the first character of the second matching line in the output.
The last two exercises may not work on Git Bash.
Please go to the directory exercise_5.
Unzipping compressed files is a very basic and important task. On Unix, you often encounter WinZip archives, .tar archives and .gz compressed files. For unpacking Win zip files, use
unzip <filename>
for .tar and .tar.gz files
tar -xf <filename>
and for .gz files,
gunzip <filename>
The 14th and 15th character of the solution are in a multiply wrapped archive in the exercise_5 directory.
To pack a directory and everything within, you can use the command
tar -cf backup.tar <directory>
To subsequently compress it, use
gzip backup.tar
Please go to the directory exercise_6
.
Each file on Unix has separate permissions for reading 'r', writing 'w', and executing 'x'. Displaying them with:
ls -l
There is one triplet of permissions for the owner of the file owner, one
triplet for a group of users, and one for all others. The chmod
command allows to change these permissions, e.g.
chmod a+x <filename>
grants all users the permission to execute a file, while chmod u-w forbids the current user (oneself) to write to the file (thereby protecting it from being deleted accidentally).
To see characters 19+20 of the solution, make the program
permissions.sh
executable. Then execute it with:
./permissions.sh
You can grant permissions for a whole directory tree using
chmod -R a+x <directory>
To find out, how much disk space you have left, you can use the command
df
df
lists all hard drive partitions, CD-ROMs, pendrives and some
logical partitions Unix uses. All numbers are given in kilobyte (1000
byte or one 1000000th GB).
To obtain the 21st character, execute df and get the first character of the header row of the output.
From the same header, the 22nd character is the first character of the title of the fourth column.
Just guess the last two characters! But do read on below to learn more - the characters that will result there are not part of our solution##
To install some programs, it is necessary to set so-called environment variables. These can be set using the command
export <variable-name>=<value>
You can see all variables by the command
env
To obtain the 22nd character, you need to use export
to set the
variable GIVEME to the value SOLUTION.
echo $GIVEME
Find out the characters position in the alphabet with:
echo $GIVEME | wc -c
By default, changes to environment variables only affect the current terminal.
If you want to set environment variables for each console window, write
the export command to the file .bashrc
in your home directory (it is a
hidden file).
The easiest way to check from the Unix command line whether the internet connection works, is to send a request to a known server (e.g. www.spiced-academy.com) using the command
ping <web address>
The command reports, how long a message takes back and forth to the given server. To interrupt the messages, press Ctrl+C. You can use the program
./check_ping
The 20th character is the ping
option that sets the maximum number
of requests sent. Check the documentation with:
man ping
To see what programs are running on your machine, type
top
It displays you a list of all currently active programs. Shift+P sorts
them by the CPU time they are using, Shift+M by the amount of memory
they are using (if you don't see any program consuming lots of memory,
start a web browser). Quit top
by pressing q.
The last two characters of the solution are the first two characters of the second word in the line containing the column labels.
If you want to get rid of one of the programs you started (e.g. because it crashed), you can do so by typing
kill -s 9 <pid>
You find the pid number in the first column of the top output. Of course, you may only interrupt your own programs, not those owned by root, the system administrator.
© 2010 Dr. Kristian Rother
This tutorial is published under the Creative Commons Attribution Share-alike License 4.0
You can find the full sources on https://github.com/krother/bash_tutorial.
I thank Janusz M. Bujnicki, Allegra Via, Pedro Fernandes and Joachim Jacob for their help with testing and reviewing the material. Further thanks go to the German Academic Exchange Service (DAAD) for financial support.
Dr. Kristian Rother