"

Level 3 Linux Exercises

Level 3 Background Information

Linux Quick Reference

When working with linux, here are some comment commands. When creating new file or directory names, avoid names with blanks (spaces) on linux. Although they can be supported, they require the use of double quotes.

  1. man – show manual for a command or program:
    1. man  g++
    2. hit q to exit the man page.
  2. mkdir – to organize files and directories. This will make a new directory:
    mkdir CS202_Programs
  3. rmdir – to remove a directory (which must be empty):
    rmdir CS202_Programs
  4. cd – change into another directory:
    1. To change into a specific directory: cd  CS202
    2. To change into a directory using a wild card: cd  CS*
    3. To change back to the directory above: cd  ..
    4. To change to the root directory (regardless): cd
  5. ls – list directory contents:
    1. To list the contents of the current directory: ls
    2. To list the contents of specific files (e.g., that start with a capital P): ls  P*
    3. To list the contents of a specific directory: ls  CS202
    4. To list the contents of directories using a wild card: ls  CS*
    5. Add flags to get more detailed displayed: ls -l /etc
    6. Use the wildcard (*) to list all of the .cpp files in the current directory: ls  *.cpp
  6. cat – to display the contents of a file:
    cat test1.cpp
  7. tail – displays the last few lines of a file
    tail  -2  test1.cpp
  8. head – same as tail, but shows the first few lines the file
    head  -5  test1.cpp
  9. more – outputs one page of a file and pauses. You can also pipe ( | ) to be used with other commands:
    ls -l CS* | more    cat test1.cpp | more
  10. pwd – to determine your current path and directory name : pwd
  11. cp – copy a file or directory:
    1. For example: cp source dest if you want to copy a directory use the -R option for recursive. The forward slash (in front of the path) means that we are working from the root directory:      cp  -R  ./source   ./dest
    2. The source and destinations may be complete paths or a path from the current directory; the following will copy the entire CS202 directory and all of its files into the CS202_Copy directory:mkdir CS202_Copy
      cp  -R  CS202   CS202_Copy
  12. mv – move a file or directory. You could think of this as a rename but it can also move a file to another directory! ***Make sure to pay close attention to the order of the arguments. ***The first argument (source) is the file or directory that you want to copy from and the second argument (dest) is the file that you want to copy to (e.g., the file to be created). Keep in mind that a move does not leave the original (unlike copy) so use it with great caution!
    1. Syntax: mv source dest
    2. Move a file: mv program1.cpp  new_name.cpp
    3. Rename a directory: mv CS202  new_directory_name
  13. rm – remove a file: rm test.cpp
  14. grep – pattern matcher utility that takes a regular expression.  These are a couple of useful flags; use man to learn about additional flags:
    1. -n will display the line where a match took place!
    2.  -c will count the number of matches
      Here are some examples using grep:
    3. Syntax: grep  pattern   file(s)
    4. Syntax with flags: grep flags pattern file(s)
    5. Find where main is: grep  -n main *.cpp

       

License

CS Linux & Vim Manual Copyright © by Karla Fant. All Rights Reserved.

Share This Book