(Unix/Linux
Lesson 3)
{ clear, echo, >, >> (append), cat, sort, uniq }
- Login to your TargetUbuntu01 Server,
or whatever server you can practice on.
- For the purposes of this lab,
login as the student username or
a username for which you have access.
- Do all work in your home directory.
3.1 Using "echo"
redirection to
create
a file |
- clear
- echo "Basketball\nBaseball"
> sports.txt
- echo - is used to display a line of text
- <Example>: echo "Whatever
you want to type"
#Remember to use the "quotes"
- \n - produces and end of
line character or a hard return that puts Baseball on the line below
Basketball.
- > - Is a redirection operator.
- In the above command on line number 2, we are redirecting the
string phrase "Basketball\nBaseball"
into a file called sports.txt.
- cat sports.txt
- This displays the results of the above echo command on line #2.
3.2 Using "echo"
redirection to
append
to a file |
- clear
- echo "Football\nUFC"
>> sports.txt
- >> - appends the contents of the
above echo command to the already created file sports.txt
- cat sports.txt
- This displays the results of the above echo command on line #2.
3.3 Using ">"
operator to
delete the
contents of a file |
- clear
- cp sports.txt sports.txt.nuke
- Make a copy of the sports.txt file, which will is called
sports.txt.nuke
- cat sports.txt.nuke
- Issue the cat command, to see the contents of the file.
- > sports.txt.nuke
- The ">" operator is a very
powerful and dangerous tool.
- The ">" operator empties out the
contents of the file, leaving you with a zero-byte, zero-line file in
which you have not undo option.
- cat sports.txt.nuke
3.4 Using "cat" to
append files into one file |
- clear
- echo "Gateraide\nPoweraide"
> drinks.txt
- Create another file called drinks.
- cat drinks.txt
- cat sports.txt drinks.txt >
combined_file.txt
- Here we are using cat to create one file called
combined_file.txt from the contents of both sports.txt and drinks.txt.
- cat combined_file.txt
3.5 Using "sort" to
create a file |
- clear
- echo "Basketball\nBasketball"
>> combined_file.txt
- Add the contents of echo to the file named combined_file.txt
- cat combined_file.txt
- sort combined_file.txt >
sorted_file.txt
- Sort alphabetically the contents of combined_file.txt
- Create a new file named sorted_file.txt using the ">"
operator
- cat sorted_file.txt
3.6 Using the "uniq"
command |
- clear
- cat combined_file.txt | uniq
- Display the contents of combined_file.txt.
- Then remove all duplicate lines using the "uniq" command.
3.7 Using the both
the "sort" and the "uniq" commands |
- clear
- sort combined_file.txt | uniq -c
- Sort the contents in combined_file.txt
- Count the number of occurence
- Notice, Basketball has a number 3 in front of the word, because
there were 3 occurrences of the word "Basketball" in the file
combined_file.txt
- sort combined_file.txt | uniq
- Sort and Display the contents of combined_file.txt.
- Then remove all duplicate lines using the "uniq" command.
3.8 Using the both
the "sort" and the "uniq" commands to
create
a file |
- clear
- sort combined_file.txt | uniq >
uniq_file.txt
- Sort the contents in combined_file.txt
- Make the contents unique with no duplicates
- Create a file using the ">"
called uniq_file.txt
- cat uniq_file.txt
3.9 Using "uniq" to
display duplicate lines |
- clear
- cat combined_file.txt | uniq -d
- Display the contents called combined_file.txt
- Use the "-d" flag of uniq to display only duplicate lines
- clear
- ls -lrta | grep `date +%Y-%m-%d`
- Do a long listing of all files sort in reverse order by time
- Using grep to only display files created today.
- Capture the screen by the <Alt> button and <PrtScn> button at the same
time.
- Paste the screen in a word document
- Upload to Moodle Lab 7 A.
|
|