ComputerSecurityStudent (CSS) [Login] [Join Now]




|UNIX >> Unix 101 Lessons >> Current Page |Views: 25760

(Unix/Linux Lesson 3)

{ clear, echo, >, >> (append), cat, sort, uniq }


3.0 Prerequisite
  1. Login to your TargetUbuntu01 Server, or whatever server you can practice on. 
  2. For the purposes of this lab, login as the student username or a username for which you have access.
  3. Do all work in your home directory.
3.1 Using "echo" redirection to create a file
  1. clear
  2. 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.
  3. 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
  1. clear
  2. echo "Football\nUFC" >> sports.txt
    • >> - appends the contents of the above echo command to the already created file sports.txt
  3. 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
  1. clear
  2. cp sports.txt sports.txt.nuke
    • Make a copy of the sports.txt file, which will is called sports.txt.nuke
  3. cat sports.txt.nuke
    • Issue the cat command, to see the contents of the file.
  4. > 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.
  5. cat sports.txt.nuke

 

3.4 Using "cat" to append files into one file
  1. clear
  2. echo "Gateraide\nPoweraide" > drinks.txt
    • Create another file called drinks.
  3. cat drinks.txt
  4. 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.
  5. cat combined_file.txt

 

3.5 Using "sort" to create a file
  1. clear
  2. echo "Basketball\nBasketball" >> combined_file.txt
    • Add the contents of echo to the file named combined_file.txt
  3. cat combined_file.txt
    • See the contents
  4. 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
  5. cat sorted_file.txt

 

3.6 Using the "uniq" command
  1. clear
  2. 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
  1. clear
  2. 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
  3. 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
  1. clear
  2. 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
  3. cat uniq_file.txt

 

3.9 Using "uniq" to display duplicate lines
  1. clear
  2. cat combined_file.txt | uniq -d
    • Display the contents called combined_file.txt
    • Use the "-d" flag of uniq to display only duplicate lines

 

Proof of Lab
  1. clear
  2. 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.
  3. Capture the screen by the <Alt> button and <PrtScn> button at the same time.
  4. Paste the screen in a word document
  5. Upload to Moodle Lab 7 A.

 



Help ComputerSecurityStudent
pay for continued research,
resources & bandwidth