In today's lab, you will be writing files. As you will have seen in the lectures, you can write and append to files using output redirects from commands:
echo "#!/bin/sh" > myscript.sh
This is very convenient for command output but it's not a pleasant, interactive editing experience. For that, you will need to use an editor! In this course
we will teach you how to use vim (a modern version of the powerful vi editor). Vim is a powerful plain text editor, meaning that it writes pure ascii text without
markups (so no fonts or formatting like you'd see in a word processor like Microsoft Word or TextEdit). To open it, simply type vim and the name
of the file you wish to edit.
The big difference between vim and other editors is that vim has multiple modes:
Normal mode is what you get when you open vim. In this mode you can navigate the file, but you CANNOT ENTER TEXT! Keypresses will do
unusual things when you press them. For example, G and / allow you to move and search as you would in less.
You can move about the file using arrow keys or h,j,k,l keys.
To return to normal mode from another mode, press the Escape key.
Insert mode allows you to edit text. To enter insert mode, type i.
Command mode allows you to enter commands. To enter command mode, type : and then enter your command. Press Enter to
execute your command. Important commands are w to write (save) a file and q to quit vim.
Vim has additional modes for selecting text and replacing text, but those three will be enough to get you started.
That should be enough to get you started. If you want to run through a full tutorial on vim, run vimtutor. For a
quick cheat sheet on vim commands, see https://vim.rtorr.com/.
HELP! I am stuck in vim! Press the escape key to return to normal mode. Then save and exit by typing :wq.
If you wish to exit without saving, type q!.
Questions
What is the difference between the following sets of commands?
cd /lab cd ./week3 head foo.txt
cd /lab/week3 head foo.txt
head /lab/week3/foo.txt
head /lab/week3/../week3/../../lab/week3/./././foo.txt
No difference: they all do the same thing
What would you expect to see in each of the following files, based on the file extension?
stuff.txt: a human-readable text file
stuff.sh: an executable shell script
stuff.jpg: an image (not readable as raw text)
stuff: could be anything. Most likely a directory or an executable of some kind
There is a copy of Frankenstein in this week's directory, but it is compressed (zipped).
How big is the compressed copy of Frankenstein?
ls -l # 169453 bytes
Compress Alice in Wonderland. How does it compare to Frankenstein?
gzip Alice_in_Wonderland.txt
ls -l
Uncompress both files and compare their uncompressed sizes.
gunzip Alice_in_Wonderland.txt
gunzip Frankenstein.txt
ls -l
The following questions refer to arcade.csv, which holds user data for an online game. Solve them using
cut, sort, head, tail, uniq.
You are adding a high score screen for your game which will display the Score column first, then the Player, then Team. No other
columns will be shown, and data should be sorted with the highest score appearing first. Write a series of commands to do this
using cut and paste.
In the week3 directory there is a script called runme.sh, but it is not executable. Modify this
script so that any user can run it with ./runme.sh.
chmod +x /lab/week3/runme.sh
In the week3 directory there is a file called DENIED.txt. Print the contents of this file to the terminal.
chmod 600 DENIED.txt; cat DENIED.txt
Your friend has a broken keyboard, and has written an email (woonky.txt) to the keyboard company asking for repairs. Unfortunately, the
keyboard problem has ruined the letter. Write a pipeline using tr to fix the email.
tr '0' 'o' < woonky.txt | tr -s '[A-Za-z]'
Use head, tail and | to construct a pipeline which prints line 999 from Alice_in_Wonderland.txt.
A pipeline is a series of commands connected together by pipes (eg. date | wc).
head Alice_in_Wonderland.txt -n 999 | tail -n 1
There is a fun command called cowsay which works like echo but adds some decoration to the input string. To try it out,
type cowsay moo. We will make our own version of cowsay called sandwichsay that prints the input string
between two slices of bread. For example, ./sandwichsay "ham and cheese" should look like this:
%============================%
ham and cheese
%============================%
Each slice of bread should be approximately 30 characters wide. Feel free to change the appearance of the sandwich if you like.
Note: Make sure you surround your input string in double quotes ("input") so that it is treated as a single command-line
argument by your script. Otherwise each space in your input string will begin a new argument.