0774 First Steps On The Linux Command Line
0774 First Steps On The Linux Command Line
0774 First Steps On The Linux Command Line
2
First Steps on the Linux Command Line
Prerequisites
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.
Preparations
Copy the file Exercises.zip from
https://github.com/krother/Linux_Commandline_Tutorial/raw/master/Exercises.zip) to a
computer with Ubuntu (or some other Linux) installed.
Unzip the file.
Type:
3
First Steps on the Linux Command Line
Make this tutorial and a ‘Unix/Linux Command Reference’ document available (see
PDFs in exercise material).
Your Task
In this tutorial, you will be looking for a word with 22 characters. All characters are hidden in
the exercises below. All exercises can be solved using the Unix command line.
License
(c) 2010 Dr. Kristian Rother
This tutorial is published under the Creative Commons Attribution Share-alike License 4.0
4
Directories and files
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 ..
cd
ls -a
The second character, is in the same directory as the first one, but in a hidden file.
5
Directories and files
Use cd .. to go back to the directory exercise_1/directoryB/. When listing its contents, you
should see a program file. To find the third character, you need to execute the program. In
Unix, this is done by typing
./program_name
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. When you want
to obtain the fourth character, type
./file_size_check
Hint
When typing names of directories or files, try typing the first three characters, and press
<TAB>. Unix tries to guess what you are typing.
6
Edit text files
more <filename>
gedit <filename>
Create a text file with a name of your choice in the exercise_2 directory that contains the
phrase
./text_file_check
Hint
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'.
7
Edit text files
8
Copy and remove files
Use ls solution/ afterwards to see whether the file is there. The program
./check_code
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 pro*' will delete all of profile, 'protein.txt and
prototype.doc. To get characters nine and ten, please run the program
./check_junk
9
Copy and remove files
WARNING
On Unix, it is not possible to undelete files!
Hint
To remove an empty directory, you can use
To delete a directory and everything in it, the command rm -r exists. In combination with the
'*' symbol, this is dangerous, because it gives you the possibility to wipe out all your data
with a single command (e.g. if you type the wrong directory by accident). Thus, first make a
backup and then play with this command.
10
Process text data
Of course, you can look at the text first using more or a text editor. The 11th character of
the solution is the first character of the third last word in the output of diff.
The symbol '|' is called a pipe and is often used to connect Unix programs to each other. The
12th character of the solution is the first character of the last word of the first line from the
alphabetically sorted file 10000_lines.txt.
Hint
To store the sorted lines in a new file, you can add an output file, like
11
Process text data
command should be used. It produces all lines from the given file that contain the given
word. The grep command is very powerful and can handle fuzzy search patterns called
Regular Expressions (not covered here). The 13th character of the solution is the first
character of the second word in a line containing 'fool'.
Hint:
You can search through many files at once by including a '*' in the filename.
12
Unzip files
5. Unzip files
Please go to the directory exercise_5.
unzip <filename>
gunzip <filename>
The 14th and 15th character of the solution are in a multiply wrapped archive in the
exercise_5 directory.
Hint
To pack a directory and everything within, you can use the command
gzip backup.tar
13
Run useful programs
ls -l
there is one triplet of these permissions for the files owner, one for a group of users, and one
for all others. The chmod command allows to change these permissions, e.g.
grants all users the permission to execute a file, while u-w forbids the current user (oneself)
to write to the file (thereby protecting it from being deleted accidentally). The next two
characters of the solution will be shown when you execute the program
./check_permissions
Hint:
You can grant permissions for a whole directory tree using
df
14
Run useful programs
It lists all hard disk partitions, CD-ROMs, pendrives etc. Your data is stored in /home/, if it
does not exist, in /. All numbers are given in kilobyte (1000 byte or one 1000000th GB). To
obtain the 18th character, run the program
./check_disk
export <variable-name>=<value>
But then the variable will only be present in the same console window where you typed the
command. You can see all variables by the command
env
To obtain the 19th character, you need to set the variable SOLUTION to the value 6.3, and
run the program
./check_variable
Hint:
If you want to have an environment variable to be automatically set for eachconsole window,
write the export command to the file .bashrc in your home directory (it is a hidden file).
Hint:
The env command can be combined nicely with grep, e.g. to check your PATH variable, you
can type:
15
Run useful programs
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.academis.eu) using the command
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
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).
The last two characters of the solution are the first two characters of the second word in
the highlighted bar containing the column labels.
Hint:
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 <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.
16
Acknowledgements
Acknowledgements
This tutorial was created for participants in a bioinformatics workshop organized by Janusz
M. Bujnicki for the EURASNET consortium in 2010. I would like to thank the organizers and
all course participants, as well as Joachim Jacob for reviewing the material. Further thanks
go to the German Academic Exchange Service (DAAD) for financial support.
Special thanks go to Allegra Via for using this tutorial in our course over the years, and to
Pedro Fernandes for suggesting publication via Gitbook.
Contact
Dr. Kristian Rother
krother@academis.eu
www.academis.eu
License
This tutorial is published under the Creative Commons Attribution Share-alike License 3.0
17