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.
man
– show manual for a command or program:man g++
- hit
q
to exit the man page.
mkdir
– to organize files and directories. This will make a new directory:
mkdir CS202_Programs
rmdir
– to remove a directory (which must be empty):
rmdir CS202_Programs
cd
– change into another directory:- To change into a specific directory:
cd CS202
- To change into a directory using a wild card:
cd CS*
- To change back to the directory above:
cd ..
- To change to the root directory (regardless):
cd
- To change into a specific directory:
ls
– list directory contents:- To list the contents of the current directory:
ls
- To list the contents of specific files (e.g., that start with a capital P):
ls P*
- To list the contents of a specific directory:
ls CS202
- To list the contents of directories using a wild card:
ls CS*
- Add flags to get more detailed displayed:
ls -l /etc
- Use the wildcard (*) to list all of the .cpp files in the current directory:
ls *.cpp
- To list the contents of the current directory:
cat
– to display the contents of a file:
cat test1.cpp
tail
– displays the last few lines of a file
tail -2 test1.cpp
head
– same as tail, but shows the first few lines the file
head -5 test1.cpp
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
pwd
– to determine your current path and directory name :pwd
cp
– copy a file or directory:- 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
- 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
- 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:
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!- Syntax:
mv source dest
- Move a file:
mv program1.cpp new_name.cpp
- Rename a directory:
mv CS202 new_directory_name
- Syntax:
rm
– remove a file:rm test.cpp
grep
– pattern matcher utility that takes a regular expression. These are a couple of useful flags; use man to learn about additional flags:-n
will display the line where a match took place!-
-c
will count the number of matches
Here are some examples using grep: - Syntax:
grep pattern file(s)
- Syntax with flags:
grep flags pattern file(s)
- Find where main is:
grep -n main *.cpp