ComputerSecurityStudent (CSS) [Login] [Join Now]




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

(Unix/Linux Lesson 2)

{ cp, mv, rm, rmdir, cat, more, head, tail, grep, wc }

 


Before you get started

  1. Use TargetUbuntu01 or whatever Unix/Linux server you have access too.

  2. Login as user account "student" or whatever Unix/Linux account you have access to on the particular server.

2.1 Copying Files

cp (copy)

  1. pwd - Shows you your current working patch (Where you are right now).
  2. touch practice1.txt - This will create a zero-byte file for the purposes of testing.
  3. cp practice1.txt practice2.txt
  4. ls -lrt practice* - Show you the long listing of files sorted by ascending by time, all files starting with practice.

 

2.2 Moving Files

mv (Move)

  1. mv practice1.txt practice3.txt
  2. ls -lrt practice* - Show you the long listing of files sorted by ascending by time, all files starting with practice.
    • Notice practice1.txt is not displayed.

 

 

2.3 Removing Files

rm (Remove)

  1. rm practice3.txt
  2. ls -lrt practice* - Show you the long listing of files sorted by ascending by time, all files starting with practice.
  3. touch practiceA.txt - create another file
  4. touch practiceB.txt - create another file
  5. touch practiceC.txt - create another file
  6. ls -lrt practice* - Show you the long listing of files sorted by ascending by time, all files starting with practice.
  7. rm practice* - remove all files that start with the word practice, using the wild card "*".
  8. ls -lrt practice*

 

2.3 Removing directories

rmdir (Remove Directory)

First, we will create some directories and files that we will later remove.

  1. mkdir folderA - mkdir folderA
  2. mkdir folderB - mkdir folderB
  3. touch folderA/fileA.txt - Create a practice file in folderA called fileA.txt
  4. touch folderB/fileB.txt - Create a practice file in folderB called fileB.txt

Option 1:  rmdir (Remove Directory)

  1. rmdir folderA
    • Notice you get a message saying the directory is not empty.
    • You will have to empty the directory before you can remove it.
  2. cd folderA
  3. ls -l
    • You will see fileA.txt that you will need to remove.
  4. rm fileA.txt
    • This removes the file
  5. cd ../
    • cd out of the directory
  6. rmdir folderA
    • Now you can remove folderA, because it is empty.
  7. ls -l | grep folderA
    • Do a long list of all file in the directory and search for anything called folderA

Option 2:  rm -rf (Remove Directory)

  • -f, mean to force
  • -r, mean to recursive remove a directory and its contents.
  1. rm -rf folderB                          
    • Remove folderB and its' contents
  2. ls -l | grep folderB | wc -l
    • Do a long listing, search for folderB, and count how times folderB is found.
    • Notice "0" is returned, because folderB is now gone.
  3. Note
    • rm -rf is a very dangerous command.
    • Never, Never, Never issue rm -rf *, unless you absolutely need to issue this command.

2.4 How to display a file with "cat"

Prep Work

  1. cd
    • This will place you back in your home directory.
  2. cp /etc/passwd PASSWD.TXT
    • You will see a file called PASSWD.txt

Option 1:  cat

  1. cat PASSWD.TXT
    • Notice that the entire file will be printed to the screen.

  2. clear
    • This command will clear your screen
2.5 How to display a file with "more"

Option 2:  more

First, we will create some directories and files that we will later remove.

  1. more PASSWD.TXT
    • Press the <Enter> key to scroll line by line.
    • Press the <spacebar> to scroll page by page
  2. clear
    • This command will clear your screen

This is what the output should look like

 

2.6 How to display a file with "less"

Option 3:  less

  • less is the opposite of more.
  • less is similar to more.
  • less allows both backward and forward movement in a file.
  • less does not have to read the entire file before starting.  (This is really handy for large files).
  1. clear
  2. less PASSWD.TXT
    • You will see output very similar to more.

  3. Now search for the username "snort" by using the forward "/"
    • /snort

  4. Type "q" to escape the less editor

 

2.7 How to display a file with "head"

Option 4:  head

  • Output the first part of the file first.
  • head by default shows 10 lines at a time.
  1. clear
  2. head PASSWD.TXT

  3. clear
  4. head -5 PASSWD.TXT
    • Show me the first 5 lines of the PASSWD.TXT file.

 

2.8 How to display a file with "tail"

Option 5:  tail

  • Output the last part of the file first.
  • last by default shows 10 lines at a time.
  1. clear
  2. tail PASSWD.TXT

  3. clear
  4. tail -5 PASSWD.TXT
    • Show me the last 5 lines of the PASSWD.TXT file.

 

2.9 How to search a file using "grep"

Command: grep

  • grep, prints lines matching a certain pattern.
  • grep <pattern> filename
  1. clear
  2. grep snort PASSWD.TXT
    • Search and display results for the pattern "snort" in the PASSWD.TXT file.

  3. grep false PASSWD.TXT
    • Search and display results for the pattern "false" in the PASSWD.TXT file

  4. grep -c false PASSWD.TXT
    • Count the number of results.

  5. grep false PASSWD.TXT | wc -l
    • Count the number of results

 

2.9.1 How to search a file using "egrep"

Command: egrep

  • egrep, prints lines matching a certain pattern.
  • egrep '(pattern1|pattern2|pattern3)' filename
  1. clear
  2. egrep '(snort|student|instructor)' PASSWD.TXT
    • Search for snort, student and instructor

 

2.9.1 How to search a file using "egrep"

 

Command Description
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
cat file display a file
more file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file

 

2.9.2 Proof of Lab Assessment
  1. cat .lesshst
  2. Capture the screen by the <Alt> button and <PrtScn> button at the same time.
  3. Paste the screen in a word document
  4. Upload to Moodle Lab 7 B.

 



Help ComputerSecurityStudent
pay for continued research,
resources & bandwidth