Cheating Sheet
Cheating Sheet
- SSH
- Make .out file step by step
Useful commands
Building a C Program
Command to build in one line: gcc209 hello.c -o hello
Command to build step by step:
Commands:
Preprocessor: “gcc209 -E hello.c > hello.i”
Compiler: “gcc209 -S hello.i”
Assembler: “gcc209 -c hello.s”
Linker: “gcc209 hello.o -lc -o hello”
Lecture 2. Shells and Commands
Super user (root) VS Normal user
Command “whoami” : get the username
Command “hostname” : get the host name
Directory Layout
Common directories:
- /: root directory. (different from root user) The starting point for all files.
- /bin: binaries and other executable programs
- /etc: system configuration files
- /home: home directories
- /opt: optional or third party software
- /tmp: temporary space, typically cleared on reboot
- /var: variable data, most notably log files
Commands Related to Directories/Files
$ls List directory contents
$ls -l List files with detailed attributes per
each file
$ls -al -a means “all files”
$cd [dir] Switch to dir, if dir missing then home
(~)
$pwd Current working directory
$cat [files] Display the file content
$mkdir / rmdir [dir] Create/remove [dir] (dir must be empty
before rmdir
$rm [files] Remove files
$rm -r dir -r option: Recursively removes all files
under dir if it is a directory
$mv [A] [B] Move file A to file B (Or rename)
Ex)
$mv a.txt ~/tmp/ Relocate a.txt to ~/tmp/
$mv a.txt b.txt Rename a.txt to b.txt
$mv a.txt ~/tmp/b.txt Relocate and rename
$mv ./tmp tmp2 Rename a directory from ./tmp to tmp2
$cp [A] [B] Copy file A to file B
Same as mv except it doesn’t remove
the original one.
$echo [argument] Displays arguments to the screen
$exit/logout/ctrl-d Exits shell or current session
Environment Variables
- A number of variables that are used to run the program
1. $PATH: a colon-seperated list ofdirectories that the shell searches for
commands.
Ex) When we type “$ls”, shell searches ‘ls’ in /bin first, and runs it if /bin/ls is
found. If not, search the next directory (separated by colon “:”).
Creating/Removing Directories
$mkdir/rmdir
Option: -p removes, creates all intermediate directories in dir.
Ex)
$mkdir newdir/product/reviews -> error
$mkdir -p newdir/product/reviews -> OK!
$rmdir newdir -> Not empty, error!
$rmidr -p newdir -> OK!
File Permissions
Represented by ten characters
- First character + (3 char for User) + (3 char for Groups) + (3 char for Others)
Changing File Permissions
Comparing Files
- $diff [file1] [file2]
Searching in Files
- $grep pattern file: Show the lines in file that match the pattern
- $grep -v pattern file: Shows the lines in file that DO NOT match the pattern
- Other options:
o $ -i: case insensitive
o $ -c: count the number of occurences
o $ -n: precede the output with line numbers in the file
I/O Redirection
- >, < symblols
- >> : appends at the end of the file
Pipe
- |
Other commands
Lecture 3. Pass
Lecture 4. Compiler
Building a C program
1. Short cut: “gcc209 hello.c -o hello”
2. Step by step
a. Cpp: gcc209 -E hello.c > hello.i
b. Compiler: gcc209 -S hello.i
c. Assembler: gcc209 -c hello.s
d. Linker: gcc209 hello.o -lc -o hello
Basics
$gcc -help
Lecture 5. Debugging
(gdb) list/where/up/down
(gdb) list: show the source code
(gdb) list func: show the source code of func( )
Pass
Lecture 7. Makefile