0% found this document useful (0 votes)
41 views13 pages

Cheating Sheet

Uploaded by

johnkim7972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views13 pages

Cheating Sheet

Uploaded by

johnkim7972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 1.

- SSH
- Make .out file step by step

Login to ssh in Shell:


Type “ssh [email protected]
How to Change password: command “passwd”

Copying Files Between Machines with “scp” command


How to Download file from lab machine to your laptop:
Command: “scp [email protected]: $(file path) .”

How to Upload file to the lab machine


“scp $(file path) [email protected] : $(optional path)”

Copying many files:


“scp *.c [email protected] : $(dir path)”

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

sudo, su, man


$sudo [command] : superuser-do, runs a command as root
$su [user-to-be-switched-to] : Switches the current user to “user-to-be-switched-to”.
Ex) $su yjwon: switches to user “yjwon”.
$su: Switches to “root”
$man [command]: Shows the manual of command

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 “:”).

2. $HOME: The location of user’s home directory


3. $PWD: The current directory
4. $DISPLAY: The identifier for the display that X11 should use by default
5. $LD_LIBRARY_PATH: a colon-separated list of directories that the dynamic
linker should search for shared objects when building a process image after
exec, before searching in any other directories

“which” and -h option


$which [command]: displays the location of command
Ex) $which ls
/bin/ls
-h option: hints, many commands provide hints for how to use them.

Working with Directories


. This directory
.. Parent directory
- Previous Directory
/ Directory separator. Optional for
the last dir.
$cd /var/tmp is same as
$cd /var/tmp/
$cd .. Go to parent dir
$cd -
Go to previous dir (Environment
variable ‘OLDPWD’ has the
previous directory.)

Executing a Program in Current Dir


$program -> if program is not in the $PATH, the shell says “program: command not
found”
$./program -> Right answer
$ /mnt/home/kyoungsoo/program -> Full path is also possible

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!

Listing Files with ls


$ls Options
-a All files including hidden files
-t Sort by last modified time
-d Displays only directory names and not
contents
-R List files recursively
-r Reverse alphabetical order of the file
names
Dealing with Spaces in File Names
$ rm a file -> can’t remove file name “a file”
$ rm ‘a file’ -> GOOD!
$ rm a* -> a*: name starting with ‘a’, followed by zero or more char.

File Permissions
Represented by ten characters
- First character + (3 char for User) + (3 char for Groups) + (3 char for Others)
Changing File Permissions

Default File Creation Mode


Base permission for newly created file:
- 777 for a directory (all can read/write/enter)
- 666 for a file (all can read and write)
How to change the behavior above?
- $umask [-S] [mode]
o Sets file creation mode
o Applied permission = base permission – [mode]
o Mode: 3 octal numbers
o EX) $umask 022
§ Dir: 777 – 022 = 755
§ File: 666 – 022 = 644
o -S displays or sets symbolic notation
Finding Files
$find [path…] [Expression]
- Recursively finds files in the path and match the expression
- If no arguments? List all files in current directory and sub directories

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

1. Build with -g option


a. $gcc -g insertsort.c -o insertsort
2. Run gdb with .out file:
a. $gdb insertsort
3. Set breakpoints if needed.
4. Run:
a. (gdb) run [options: input for .out file]
5. “n”: next, “c”: continue, “step”: step into the function
6. Printing
a. (gdb) print i
7. To examine the function call stack, (gdb) where
8. Quit: (gdb) quit

Running with redirection


(gdb) run < somefile1 > somefile2

(gdb) list/where/up/down
(gdb) list: show the source code
(gdb) list func: show the source code of func( )

Lecture 6. Debugging Techniques

Pass
Lecture 7. Makefile

You might also like