LinuxGym Answers
LinuxGym Answers
Redirection
Grep is grabbing a line from a list
File system commands: ls -[lAa], mkdir, cd, pwd, cp, mv, rm
Standard output redirection (>)
Viewing files: cat, less, head, tail
Commands and their options: touch, cat, wc
1. Create a file
Create a file called "hello.world" (without the quotes). Hint: touch
Touch hello.world
2. Create a directory
Create a directory called "otherworld" (not including the quotes).
mkdir otherworld
3. Store a calendar
Store the 2008 calendar produced by the "cal" command in the file "calendar" with no modifications to the
output of the "cal" command. Hint: Standard output redirection using ">".
cal 2008 > calendar
4. List visible files
List all (non-hidden) files in the directory "/usr/local/linuxgym-data/gutenberg" and store this list into a file
called "listvis". Ensure that this is exactly the output of the command "ls".
ls /usr/local/linuxgym-data/gutenberg > listvis
5. List all files
By "all files" we refer to every file which is not a directory, including hidden files whose names begin with
".". List all files in the directory "/usr/local/linuxgym-data/gutenberg" and store this list into a file called
"allfiles". Ensure that this is exactly the output of the command "ls". (Hint: consider the "all" and "almost-all"
options in "man ls")
ls a /usr/local/linuxgym-data/gutenberg > allfiles
6. Top of file
The "cat" function prints out the contents of a file. For example "cat /usr/local/linuxgym-data/census/aj_malenames.txt" shows you an alphabetically ordered list of male names recorded by the US Census
Bureau. Because the list scrolls off the top of the screen, you can not easily read the first 10 names. The
"head" command solves this problem. Experiment with the "head" command and store the first 10 lines
from the file a-j_malenames.txt into a file called "first-ten-names.txt". (Hint: Redirect the output using ">")
10head /usr/local/linuxgym-data/census/a-j_malenames.txt > ten-names.txt
7. Merging files
The "cat" function which prints out the contents of a file, can be used to print the contents of several files by
giving it more than one argument. In the directory /usr/local/linuxgym-data/census/ there are two files: aj_malenames.txt and k-z_malenames.txt. Use "cat" to print out both of these files and store the output in a
file called "a-z_malenames.txt" in the correct order. (Hint: Redirect the output using ">")
cat /usr/local/linuxgym-data/census/a-j_malenames. txt k-z_malenames.txt > a-z_malenames.txt
-1-
8. File size
Look at the files in the directory /usr/local/linuxgym-data/teeny and indentify which is the smallest file. Copy
this file into your ch1-fdr directory. (Hint: cp, ls -l)
ls l /usr/local/linuxgym-data/teeny
cp (smallest) /usr/local/linuxgym-data/teeny/filename filename
-2-
-3-
4000dd
:wq
6. Copy and paste by line numbers
Copy the file "/usr/local/linuxgym-data/teeny/3mwsm10.txt" into your "ch2-vim" directory. Delete the first 10
lines and append them to the end of the file. Save the file and make no other changes.
10yy
OR
10dd
GP
7. Delete to the end of a file
Copy the file "/usr/local/linuxgym-data/gutenberg/7wdvn10.txt" into your "ch2-vim" directory. Delete all the
lines from line 1024 to the end, including line 1024 and save the file.
G1024
(large number)dd
8. Search and replace strings
In the next two questions we have to distinguish between words and strings. A string is a sequence of
characters, while a word is a sequence of letters within a string, surrounded by non-word characters. For
example, the string "This is a testing time" contains the words "This", "is", "a", "testing" and "time". It does
not contain the word "test", but it does contain the string "test". Copy the file "/usr/local/linuxgymdata/vimdata/hermit.txt" into your "ch2-vim" directory. Replace every occurence of the string "and" with the
string "OR". Ensure you make no other changes to the file.
:%s/and/OR/gc
9. Search and replace words [1]
Recall that we have to distinguish between words and strings. A string is a sequence of characters, while a
word is a sequence of letters within a string, surrounded by non-word characters. For example, the string
"This is a testing time" contains the words "This", "is", "a", "testing" and "time". It does not contain the word
"test", but it does contain the string "test". Copy the file "/usr/local/linuxgym-data/vimdata/7herm10.txt" into
your "ch2-vim" directory. Replace every occurence of the word "and" with the word "OR". (Hint: make sure
you don't change words such as "sandy" into "sORy".)
:%s/<and>/OR/gc
10. Cut and paste by markers [1]
Copy the file "/usr/local/linuxgym-data/teeny/4mwsm10.txt" into your "ch2-vim" directory. Cut (remove) the
lines between (and containing) those with "START HERE" and "UPTO HERE" and insert them at the
marker "PASTE HERE". Save the file and make no other changes.
yy copy
dd- copy & delete
P paste
-4-
-5-
vim secret
chmod 600 secret.txt
7. Small Bash script
The file "/usr/local/linuxgym-data/permissions/hw.sh" is a bash script. Copy it into your "ch3-pp" directory
and change the permissions so that you are able to execute it.
cp /usr/local/linuxgym-data/permissions/hw.sh hw.sh
chmod 740 hw.sh
8. Standard error preparation
Create a file called "forgotten.txt" in your "ch3-pp" directory. Change its permissions so that you are not
allowed to see its contents.
#cat = reads whats in da files and display them
cat > forgotten.txt
chmod 222 forgotten.txt
9. Standard error message
If you attempt the command "cat forgotten.txt" (as created in the previous question), because you do not
have permission to read it, there will be an error. The goal of this question is to store this error in a file
called stderr.out in your ch3-pp directory, making sure there are no additional characters or spaces. (Hint:
Do not attempt to edit this file by hand. The ">" operator redirects the standard output while the "2>"
operator redirects the standard error.)
cat > stderr.out
cat forgotten.txt 2> stderr.out
-6-
-7-
touch lastlines.txt
cat lastlines.txt | tail -103 > lastlines.txt
5. Append the output
Copy the file /usr/local/linuxgym-data/census/a-j_malenames.txt to a file called appendix.txt in your ch4pipewild directory. Append to appendix.txt the contents of /usr/local/linuxgym-data/census/kz_malenames.txt making no other modifications. [Hint: cat, ">>"]
touch appendix.txt
cp /usr/local/linuxgym-data/census/a-j_malenames.txt appendix.txt
cat usr/local/linuxgym-data/census/k-z_malenames.txt >> appendix.txt
6. Listing according to a regular expression
Make a list of all the files from the directory "/usr/local/linuxgym-data/gutenberg" who's names contain the
string "rd" and save this list as "rdfiles.txt". Ensure that only the filenames are listed WITHOUT the
pathnames as a prefix.
touch rdfiles.txt
cd /usr/local/linuxgym-data/Gutenberg
ls *rd* > path/filename.txt
7. Copying according to a regular expression
Create a directory called "s2" under your "ch4-pipewild" directory. Copy all the files from the directory
"/usr/local/linuxgym-data/gutenberg" who's names contain the string "s2" into your "ch4-pipewild/s2"
directory.
mkdir s2
cp *s2* /usr/local/linuxgym-data/Gutenberg
or
cd usr/local/linuxgym-data/Gutenberg
ls | grep s2 | xargs cp {} ~/ch4-pipewild/s2
mkdir s2
cd usr/local/linuxgym-data/Gutenberg
ls | grep s2
for i(means every variable) in $( ls | grep s2 ); do cp $i /home/student/ch4-pipeline/s2; done
8. Listing files ordered by their modification time
List the (visible AND hidden) files in the directory "/usr/local/linuxgym-data/spacetime" from least, to most
recently modified. Store this list into the file "timeordered.txt", with only the filenames.
touch timeordered.txt
cd /usr/local/linuxgym-data/spacetime
ls at > ~(etc)timeordered.txt
9. Copying a directory
Copy the whole directory "/usr/local/linuxgym-data/teeny" and its contents into your "ch4-pipewild"
directory. [Hint: You can do it with just one call of cp and recursion.]
cp r /usr/local/linuxgym-data/teeny teeny
10. Listing files ordered by their size
List the (non-hidden) files in "/usr/local/linuxgym-data/gutenberg" ordered by size from large to small, and
store this list in a file called "large_to_small" in your "ch4-pipewild" directory. Include only the file names.
Do not include the path prefixes.
touch large_to_small
cd /usr/local/linuxgym-data/Gutenberg
ls S > ~/ch4-piplewild/large_to_small
-8-
-9-
- 10 -
#!/bin/bash
echo $* 1>&2
OR
echo $* @>&2
OR
echo $@ls
8. List to standard error
Write a bash script called "lsstderr.sh" which will execute ls on the first argument and output the result to
STDERR.
ls $1 > &2
The input stream is referred to as ''standard input''; the output stream is referred to as ''standard
output''; and the error stream is referred to as ''standard error''. These terms are abbreviated to form
the symbols used to refer to these files, namely stdin, stdout, and stderr.
Redirection in Linux
&0 = stdin
&1 = stdout
&2 = stderr
- 11 -
- 12 -
vim
egrep l Anne path/filename/* > filename.txt
Grep -l Anne /Gutenberg/* > anne-files.txt
(-l, -- files with matches only print FILE names containing matches)
6. Grep and words
Create a file called "anne-words.txt" containing only the lines of "/usr/local/linuxgymdata/gutenberg/12frd10.txt" containing the name (WORD) 'Anne'. Note that 'Annette' and 'Annemarie' do
not count. Ensure there are no extra blank lines in the file "anne-words.txt" and the lines should be in the
same order as in 12frd10.txt. [Hint: use grep and redirection]
egrep w Anne path/filename > filename.txt
Grep w Anne /Gutenberg/12frd10.txt* > anne-words.txt
(-w, ---word regexp force PATTERN to match only whole words)
7. Grep and regular expressions
Create a file called "natmat.txt" containing, IN THE ORIGINAL ORDER, only the lines of
/usr/local/linuxgym-data/census/femalenames.txt starting with the string 'NAT' or 'MAT'. Ensure there is no
extra white space in the file "natmat.txt". [Hint: use regular expressions in grep and redirection]
egrep ^[NM]AT path/filename > filename.txt
Or
Grep \<[NM]AT ../femalenames.txt > natmat.txt
(\<, \> the beginning and end of a world
^, $ match the beginning and end of a line, respectively,
. full-stop matches any character.)
8. Name frequency
Create a bash script called namefreq.sh which takes one argument - a name. The output should be the
FREQUENCY of that name's use, which is the entry in the second column of /usr/local/linuxgymdata/census/femalenames.txt For example ./namefreq.sh ANA will return 0.120 and nothing else, which
corresponds to the line "ANA,0.120,55.989,181" of the file "table.csv" you created in the "List manipulation"
question. Hints: 1. Search "table.csv" instead of "/usr/local/linuxgym-data/census/femalenames.txt"; 2.
Grep for "words" rather than strings. 3. Use "cut" to output a particular field.
grep w $1 filename.txt | cut f2 c16-20
(-w for word ANA)
(-c16-20, cut from character number16 to 20)
9. Single file word detection
Write a bash script called gender.sh which takes one argument - a name. The script should print "female" if
the word appears in the file /usr/local/linuxgym-data/census/femalenames.txt, and "male" otherwise. Hints:
1. Redirect stdout so it doesn't get printed, or use the grep "quiet" option; 2. View the exit status $? of grep
vim
#!/bin/bash
egrep iq $1 path/filename
if [ $? == 0 ]; then
echo female
else
echo male
fi
10. Same frequency
Challenge question Create a bash script called samefreq.sh which takes one argument - a name. The
output should be the alphabetically ordered list of all name's with the same frequency (popularity) as
measured in the second column of the table: /usr/local/linuxgym-data/census/femalenames.txt For example
./namefreq.sh ANA will return a list of two names (on separate lines) ANA and RENEE, both of which have
frequency 0.120. Hints: 1. Instead of "/usr/local/linuxgym-data/census/femalenames.txt" search "table.csv"
- 13 -
(created in the question "List manipulation" of this chapter) using words rather than strings; 2. Store the
frequency of the name using backquotes; 3. Search for the frequency while viewing only the first two
columns (use cut) - name and frequency; 4. Sort the final list of names.
vim
#!/bin/bash
f=` grep \<$1\> filename.txt | cut f2 d,` ( ` not )
grep \<$f\> filename.txt | cut f1 d, | sort d
- 14 -
- 15 -
cp /usr/local/linuxgym-data/public_html/fish.jpg fish.jpg
vim picture.html
<html>
<img src = fish.jpg alt = Big Fish>
</html>
7. Html index
An index.html file is automatically accessed by the browser without specifying the filename. The goal of this
exercise is to create an HTML web page accessible at the URL http://localhost/~student/ As the "front
page" of this chapter, give the index.html page links to several other pages in your public_html directory
without using "http" in the address. This is called a "relative link". (A link using "http" in the address is called
"absolute".)
vim index.html
<html>
<a href = picture.html>
<a href = table.html>
etc
</html>
- 16 -