| Schedule | LabAssignments | LabInstructions |
Lab One
Lab Wiki Page
Create the wiki page for your assignment. Use your first name and the lab number for the title, like DebbieLabOne. Only use your first name, and you'll need to use the word One instead of the number. Go ahead and link the page to the LabAssignments page.
Exercises
Exercise 1.1
Suppose ls shows you this:
Makefile biography.txt data enrolment.txt programs thesis
What argument(s) will make it print the names in reverse, like this: -r
thesis programs enrolment.txt data biography.txt Makefile
Exercise 1.2
What does the command cd ~ do? What about cd ~hpotter? cd ~ changes directories and takes you to the home directory. Cd ~hpotter takes you to hpotter's directory
Exercise 1.3
What command will show you the first 10 lines of a file? The first 25? The last 12? first = head -id# last = tail -id#
Exercise 1.4
What do the commands pushd, popd, and dirs do? pushd adds a directory to the top of the directory stack, or rotates the stack, making the new top of the stack the current working directory. With no arguments, exchanges the top two directories. popd removes entries from the directory stack. With no arguments, removes the top directory from the stack, and cd's to the new top directory. dirs displays the list of currently remembered directories. Directories find their way onto the list with the pushd' command; you can get back up through the list with the popd' command.
Exercise 1.5
How would you send the file earth.txt to the default printer? lpr. How would you check it made it (other than wandering over to the printer and standing there)? lpq.
Exercise 1.6
The instructor wants you to use a hitherto unknown command for manipulating files. How would you get help on this command? type man or help with the command
Exercise 1.7
diff finds and displays the differences between two text files. For example, suppose you had a file named earth.txt, that contained the following lines:
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
and you modified it so it now looked like this:
Name: Earth Period: 365.26 days Inclination: 0.00 degrees Eccentricity: 0.02 Satellites: 1
You can then compare the two files like this:
$ diff earth.txt earth2.txt 3c3 < Inclination: 0.00 --- > Inclination: 0.00 degrees 4a5 > Satellites: 1
(The rather cryptic header "3c3" means that line 3 of the first file must be changed to get line 3 of the second; "4a5" means that a line is being added after line 4 of the original file.)
What flag(s) should you give diff to tell it to ignore changes that just insert or delete blank lines? What if you want to ignore changes in case (i.e., treat lowercase and uppercase letters as the same)? To ignore changes or delete blank lines use -B and to ignore changes in case, use -i
Exercise 1.8
-rwxr-xr-x 1 aturing cambridge 69 Jul 12 09:17 mars.txt -rwxr-xr-x 1 ghopper usnavy 71 Jul 12 09:15 venus.txt
According to the listing of the data directory above, who can read the file earth.txt? Who can write it (i.e., change its contents or delete it)? When was earth.txt last changed? What command would you run to allow everyone to edit or delete the file? Alan Turing and the group cambridge. Only Alan can write. It was last changed on July 12, 1969. The command to allow everyone to edit or delete the file is chmod.
Exercise 1.9
Suppose you want to remove all files whose names (not including their extensions) are of length 3, start with the letter a, and have .txt as extension. What command would you use? For example, if the directory contains three files a.txt, abc.txt, and abcd.txt, the command should remove abc.txt , but not the other two files. rm a??.txt
Exercise 1.10
You're worried your data files can be read by your nemesis, Dr. Evil. How would you check whether or not he can, and if necessary change permissions so only you can read or write the files? chmod -R o-rwx
Exercise 1.11
What's the difference between the commands cd HOME and cd $HOME? the $ takes to the home directory but without it, it takes you to HOME.
Exercise 1.12
Suppose you want to list the names of all the text files in the data directory that contain the word "carpentry". What command or commands could you use? grep
Exercise 1.13
Suppose you have written a program called analyze. What command or commands could you use to display the first ten lines of its output? What would you use to display lines 50-100? To send lines 50-100 to a file called tmp.txt? analyze my file | head -100 | tail -50 > tmp.txt
Exercise 1.14
The command ls data > tmp.txt writes a listing of the data directory's contents into tmp.txt. Anything that was in the file before the command was run is overwritten. What command could you use to append the listing to tmp.txt instead? use two greater sign such as ls data >> tmp.txt
Exercise 1.15
What command(s) would you use to find out how many subdirectories there are in the lectures directory? find ./lecture -type "d"
Exercise 1.16
What does rm *.ch? What about rm *.[ch]? rm *.ch removes every file that ends in .ch and rm *.[ch] removes every file that ends with ch
Exercise 1.17
What command(s) could you use to find out how many instances of a program are running on your computer at once? For example, if you are on Windows, what would you do to find out how many instances of svchost.exe are running? On Unix, what would you do to find out how many instances of bash are running? To find out how many programs are running, use ps aux |grep "username". To find out how many instances of bash are running, type in ps aux |grep "
Exercise 1.18
A colleague asks for your data files. How would you archive them to send as one file? How could you compress them? By compressing them. Create the file by using tar -cf (filename).tar Desktop, then use gzip to compress the file.
Exercise 1.19
You have changed a text file on your home PC, and mailed it to the university terminal. What steps can you take to see what changes you may have made, compared with a master copy in your home directory? Diff v.1 against v.2
Exercise 1.20
How would you change your password? passwd to change
Exercise 1.21
grep is one of the more useful tools in the toolbox. It finds lines in files that match a pattern and prints them out. For example, assume the files earth.txt and venus.txt contain lines like this:
Name: Earth Period: 365.26 days Inclination: 0.00 Eccentricity: 0.02
grep can extract lines containing the text "Period" from all the files:
$ grep Period *.txt earth.txt:Period: 365.26 days venus.txt:Period: 221.70 days
Search strings can use regular expressions, which will be discussed in a later lecture. grep takes many options as well; for example, grep -c /bin/bash /etc/passwd reports how many lines in /etc/passwd (the Unix password file) that contain the string "/bin/bash", which in turn tells me how many users are using bash as their shell.
Suppose all you wanted was a list of the files that contained lines matching a pattern, rather than the matches themselves—what flag or flags would you give to grep? What if you wanted the line numbers of matching lines? -x force PATTERN to match only whole lines. -n give us the line numbers.
Exercise 1.22
Suppose you wanted ls to sort its output by filename extension, i.e., to list all .cmd files before all .exe files, and all .exe's before all .txt files. What command or commands would you use?
Exercise 1.23
What does the alias command do? When would you use it? It allows one command to look like another. It is usually used to make long commands short.
