Extra exercises¶
These exercises are meant as extra (optional) training and can be done either during classes if there is time, or later.
Useful files for these examples are found in exercises/06.linux-intro/patterns from the tarball, except for the two last sections which mainly uses exercises/06.linux-intro/awk-qol and exercises/06.linux-intro/script.
Model solutions to these exercises can be found in model solutions.
The Linux File System - Wildcards¶
- Using wildcards, match all file names that begin with
thisfile, followed by one or more numbers, and end with.txt - Using wildcards, match all file names with a number in them.
- Using wildcards, match all file names that has a
0in them.
Modifying the file tree - cp, mv, rm¶
- Create a directory named
bio. Change to the directory. Create two subdirectories. Enter one of them. Create three files (using eithertouchor an editor). - Create another directory, at the same level as the directory named
bio. Copy the files and directories formbiointo your new directory. - Enter your new directory. Create four files. Create a subdirectory. Move the files to the new subdirectory.
- Enter the directory
bio. Rename one of the subdirectories. Create another subdirectory. Remove a file. Remove one of the subdirectories. Try removing one with files in it and one without. What extra option do you need to remove a directory, a directory with files in, and a regular file?
Modifying the file tree - symbolic links¶
- Create a symbolic link in your home directory to the
patternssubdirectory inside the extracted tarball.
Data handling - archiving and compressing¶
- Create a tarball containing the
exercises/06.linux-introfiles and directories, but not the exercises from the other sections. - Use
rsyncto transfer files between two of your directories.
Pipes and filters¶
- Using
catand redirect, create a new file namedmyfancyfile.txtwith several lines of text (at least 10 lines). Save it as in the exercise on https://hpc2n.github.io/bioinformatics-hpc/06.linux-intro/pipesfilters/#exercises - Use
headandtailto see the first and last few lines of text. - Use
wcto count the number of lines, the number of characters, and the number of words in your file. - Use
sortto sort the lines.
Finding patterns¶
- Use pipes to first do
wcon a file and thensortthe output. - In the directory
exercises/06.linux-intro/patterns, usegrepto search for the wordstringand the wordtext. Do the same while adding-ito ignore case. - Standing in the top level directory of exercise directories for the “Introduction to Linux” section (
exercises/06.linux-intro), use find to find all files with the suffix.txt. - Use regular expressions to find all lines in the file
myfile3.txtin theexercises/06.linux-intro/patternsdirectory that has a word starting withAat the beginning of the line.
Linux tools: awk¶
The directory exercises/06.linux-intro/awk-qol has two files file.dat and myfile.txt which are useful for these exercises.
- Search for the pattern
omnivorein the filefile.datand print out the line. - Search for the pattern
isin the filemyfile.txtand print out the second column of lines with that pattern. - Print column 1 and 4 from file
file.dat, but only those rows that contain the letter āvā. - Print all lines of
file.datthat has more than 20 characters
Scripting¶
- Create a script that greets you when run.
- With any editor, open the new file
hello.sh. - Enter the following into the editor:
- Save
- Set the executable permissions:
chmod +x hello.se - Run the script.
- With any editor, open the new file
- Create a script that asks for input and does something with it:
- With any editor, open the new file
addinput.sh. - Enter the following into the editor:
- Save and set correct permissions, then run it.
- With any editor, open the new file
- Combine the two programs to create a program that asks for your name and then says
Hello, <your-name>!. - Create a script that uses IF-ELSE to say if a number is less or greater that 2026.
- With any editor, open the new file
ifelse.sh - Save. Set executable permissions. Run the script.
- With any editor, open the new file