+ All Categories
Home > Documents > Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly ...

Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly ...

Date post: 21-Jan-2021
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
20
CMPSC 311: Introduction to Systems Programming Page 1 Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA Systems and Internet Infrastructure Security i i Unix Essentials Devin J. Pohly <[email protected]>
Transcript
Page 1: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

CMPSC 311: Introduction to Systems Programming Page 1

Institute for Networking and Security ResearchDepartment of Computer Science and EngineeringPennsylvania State University, University Park, PA

Systems and Internet Infrastructure Security

i

i

Unix Essentials

Devin J. Pohly <[email protected]>

Page 2: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 2CMPSC 311: Introduction to Systems Programming

The Unix Philosophy

• Write programs that do one thing and do it well.

• Write programs to work together.

• Write programs to handle text streams, because that is a universal interface.

‣ Doug McIlroy, Unix patriarch

Page 3: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 3CMPSC 311: Introduction to Systems Programming

Why use the CLI?

• Efficient and powerful

• Easy to automate/script

• Simple and reliable‣ Works even when

everything else is completely b0rked!

Page 4: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 4CMPSC 311: Introduction to Systems Programming

CLI and the filesystem

• Fairly typical structure‣ Everything under one root

directory, called “/”‣ Separate directory names

with additional slashes‣ To back out to the parent

directory, use “..”

• Examples:/usr/share/dict/words

/usr/bin/../share/dict/words

‣ These are called paths.

Page 5: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 5CMPSC 311: Introduction to Systems Programming

Top-level directories

• Organized by the type of files they contain

• /usr: installed software‣ /usr/bin, /usr/lib, ...

• /etc: configuration

• /home: users’ own files

• /dev: devices

• /tmp: temporary files

Page 6: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 6CMPSC 311: Introduction to Systems Programming

Navigating the filesystem• Current working directory

‣ Default point of reference‣ Shown in prompt

• Change directory: cd‣ cd /usr/share/dict‣ cd by itself takes you back

home

• List files: ls

• Special directory names‣ . means the working directory

‣ .. means the parent directory◾ So cd .. takes you up a level

Page 7: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 7CMPSC 311: Introduction to Systems Programming

Practice

• Find the configuration files for the APT package manager.

• View the list of sources using the less program.‣ Press q to quit less.

Page 8: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 8CMPSC 311: Introduction to Systems Programming

Working with files

• Copying files:‣ cp src dest

• Moving/renaming files:‣ mv src dest

• Removing files:‣ rm file

• Creating/removing empty directories:‣ mkdir newdir‣ rmdir dir

Page 9: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 9CMPSC 311: Introduction to Systems Programming

Getting help

• Manual pages: man‣ Standard layout‣ man operator‣ man ascii‣ man man!

‣ Man pages are full of info, you just have to know where to look.

• Keyword search: man k‑• (Also Google)

Page 10: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 10CMPSC 311: Introduction to Systems Programming

Common command syntaxProgram: ls

Arguments tell the program what to operate on: ls /home /usr

Options modify program behavior: ls -l

Options and arguments can be combined: ls -l -a /home ls -l --all /home ls -la /home

Page 11: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 11CMPSC 311: Introduction to Systems Programming

man practice

• Find the command that prints a sequence of numbers.

• Tell it to print the numbers 8 through 12.

• Now tell it to pad the numbers with zeroes so everything is the same length (08, 09, 10, 11, 12).‣ Remember: man -k to search, man to view.

Page 12: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 12CMPSC 311: Introduction to Systems Programming

Editing text files

• Unix philosophy again: text is important!‣ Configuration? Text files.‣ Scripting? Text files.‣ Programming? Text files.

• Vim (and vi)‣ Does one thing and does

it well‣ Integrates nicely with

other CLI tools like Make

Page 13: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 13CMPSC 311: Introduction to Systems Programming

Vim setup

sudo apt-get install vim

wget tiny.cc/311setup

sh 311setup

Page 14: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 14CMPSC 311: Introduction to Systems Programming

Understanding Vim

• Several different modes‣ Indicator in bottom-left

corner

• Normal mode (default)‣ Navigate the file‣ Most operations except

typing text‣ When in doubt, Esc gets

you to Normal mode.

• Insert mode‣ Typing text

Page 15: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 15CMPSC 311: Introduction to Systems Programming

Getting around in Vim• Avoid those arrow keys!

‣ Moving: h, j, k, l‣ Paging: Ctrl f/b, Ctrl u/d‑ ‑‣ Other keys work too

• Searching: /‣ (Regular expressions)

• Practice: edit /etc/services

• To quit without saving, type :q! and press Enter in Normal mode.

h j k l

Page 16: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 16CMPSC 311: Introduction to Systems Programming

Making changes• Getting to Insert mode

‣ i: insert at cursor‣ A: append to this line‣ o: open a line below this‣ O: open a line above this

• Esc: back to Normal mode

• Also worth knowing‣ u: undo‣ Ctrl-R: redo

• Write and quit: :wq

Page 17: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 17CMPSC 311: Introduction to Systems Programming

Salutations!

• Change to your home directory

• Open the file yo.c in Vim

• Fix the program by adding#include <stdio.h>

at the top (hint: open a line above)

• Write and quit: :wq‣ Shortcuts: ZZ to save and quit, ZQ to quit without saving.

Page 18: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 18CMPSC 311: Introduction to Systems Programming

Compile and run

• Run the compiler:gcc -o yo yo.c

• Execute the program:./yo

• Edit it again‣ Delete a printf line by

moving to it and pressing dd.

‣ Now add another printf by using the o command.

• Save, quit, compile, run!

Page 19: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 19CMPSC 311: Introduction to Systems Programming

Basic shell scripting

• Eventually you’ll get tired of repeatedly typing:

gcc -o yo yo.c./yo

• Shell script = list of commands to run

• Put these two lines in a file called go.

• Change yo.c, then run:sh go

Page 20: Unix Essentials - Pennsylvania State University · 2015. 1. 3. · Unix Essentials Devin J. Pohly  CMPSC 311: Introduction to Systems Programming Page 2

Page 20CMPSC 311: Introduction to Systems Programming

Learning Vim• Many, many features

‣ Try everything, and find the ones that work for you

‣ :help◾ :help user-manual

• Use it on your own system!‣ GVim for Windows, MacVim

for Mac

• Run vimtutor at the CLI‣ Simple, hands-on introduction‣ Do lessons 1.1–3.4 and learn

the commands and concepts


Recommended