34
A Brief Introduction to Unix Sean Barag <[email protected]> Drexel University March 30, 2011 Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 1 / 17

A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag Drexel University March30,2011 Sean Barag

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

A Brief Introduction to Unix

Sean Barag<[email protected]>

Drexel University

March 30, 2011

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 1 / 17

Page 2: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Outline

1 Directories

2 Commands

3 Editors

4 Using the Unix Shell

5 Permissions

6 Moving Forward

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 2 / 17

Page 3: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Outline

1 Directories

2 Commands

3 Editors

4 Using the Unix Shell

5 Permissions

6 Moving Forward

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 2 / 17

Page 4: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Outline

1 Directories

2 Commands

3 Editors

4 Using the Unix Shell

5 Permissions

6 Moving Forward

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 2 / 17

Page 5: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Outline

1 Directories

2 Commands

3 Editors

4 Using the Unix Shell

5 Permissions

6 Moving Forward

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 2 / 17

Page 6: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Outline

1 Directories

2 Commands

3 Editors

4 Using the Unix Shell

5 Permissions

6 Moving Forward

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 2 / 17

Page 7: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Outline

1 Directories

2 Commands

3 Editors

4 Using the Unix Shell

5 Permissions

6 Moving Forward

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 2 / 17

Page 8: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Basics

In Unix, everything is considered a file, even directories!Forward slash (/) instead of backslash (\)Root directory /Home directory ~Current directory . or ./Parent directory .. or ../

Windows C:\Documents and Settings\[user]\Desktop\Presentation.pdfUnix /home/[user]/Desktop/Presentation.pdf

or ~/Desktop/Presentation.pdf

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 3 / 17

Page 9: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Basics

In Unix, everything is considered a file, even directories!Forward slash (/) instead of backslash (\)Root directory /Home directory ~Current directory . or ./Parent directory .. or ../

Windows C:\Documents and Settings\[user]\Desktop\Presentation.pdfUnix /home/[user]/Desktop/Presentation.pdf

or ~/Desktop/Presentation.pdf

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 3 / 17

Page 10: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Interesting Directories

/ Root Directory/bin Executable programs/dev Device Files/etc System-level configuration files/lib Essential libraries/opt Packages and software not typically part of the system/tmp Temporary files (Cleaned on restart)/usr User file system/usr/man Manuals/usr/src Source code for utilities and libraries

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 4 / 17

Page 11: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

How to Get Help

man – an interface to the on-line reference manualsNearly every command has an entry (if not, try -h or --help)Typically:

SynopsisDescriptionOptionsDefaultsExamplesReferencesRelated Commands

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 5 / 17

Page 12: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

How to Get Help

man – an interface to the on-line reference manualsNearly every command has an entry (if not, try -h or --help)Typically:

SynopsisDescriptionOptionsDefaultsExamplesReferencesRelated Commands

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 5 / 17

Page 13: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Working with Directories

Command Actionpwd Prints “present working directory” (where you are)cd [path] Change directorymkdir [path] Makes directoryrmdir [path] Removes an empty directory

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 6 / 17

Page 14: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Working with Files

Command Actionls List names of all files in the current directorycp [foo] [bar] Copy foo to bar, overwrites bar if it existsmv [foo] [bar] Moves foo to bar, overwrites bar if it existsrm [foo] Removes named filescat [foo] Prints contents of a file to the command linewc [foo] Counts lines, words, and characters for any named files

There is no “Recycle Bin,” unless you specifically set one up!

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 7 / 17

Page 15: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Options for ls and Pattern Matching

Option Result[none] All the files in the current directory-t Sorts by date modified (most recent first)-u Same as -t, but sorts by time of last use-l Shows results as a detailed list-r Uses reverse order when sorting-a Show all, including hidden files

Command Resultls foo Show the file fools * Show all the filesls *.pdf Show all files who’s filename ends with .pdfls b[aeiou]n Show any file with name ban, ben, bin, bon, or bunls ?ob Show any file that ends with “ob” and has one character before that.ls *.mp[345] Show all files that end in .mp3, .mp4, or .mp5Many others, and you can use any of the above options with these

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 8 / 17

Page 16: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Options for ls and Pattern Matching

Option Result[none] All the files in the current directory-t Sorts by date modified (most recent first)-u Same as -t, but sorts by time of last use-l Shows results as a detailed list-r Uses reverse order when sorting-a Show all, including hidden files

Command Resultls foo Show the file fools * Show all the filesls *.pdf Show all files who’s filename ends with .pdfls b[aeiou]n Show any file with name ban, ben, bin, bon, or bunls ?ob Show any file that ends with “ob” and has one character before that.ls *.mp[345] Show all files that end in .mp3, .mp4, or .mp5Many others, and you can use any of the above options with these

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 8 / 17

Page 17: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Working with Files (cont’d)

Command Resultgrep [pattern] [foo] Print lines in foo matching a patterngrep -v [pattern] [foo] Print lines in foo that don’t match the patterntail [foo] Print last 10 lines of footail -n [foo] Print last n lines of footail +n [foo] Start printing foo at line ncmp [foo] [bar] Print location of the first differencediff [foo] [bar] Print all of the differences

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 9 / 17

Page 18: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Editors

viVery minimal, installed on nearly every Unix/Linux machine by default

vimVi IMproved. Much easier to use, also installed on nearly everyUnix/Linux machine by default. Extensible and very customizable.

emacsMore feature-packed than vi/vim.Extensible, very customizable, and very powerful.

nanoInstalled on most Unix/Linux machines by default.Very simple to use, but at the cost of extensibility and features.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 10 / 17

Page 19: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Editors

viVery minimal, installed on nearly every Unix/Linux machine by default

vimVi IMproved. Much easier to use, also installed on nearly everyUnix/Linux machine by default. Extensible and very customizable.

emacsMore feature-packed than vi/vim.Extensible, very customizable, and very powerful.

nanoInstalled on most Unix/Linux machines by default.Very simple to use, but at the cost of extensibility and features.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 10 / 17

Page 20: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Editors

viVery minimal, installed on nearly every Unix/Linux machine by default

vimVi IMproved. Much easier to use, also installed on nearly everyUnix/Linux machine by default. Extensible and very customizable.

emacsMore feature-packed than vi/vim.Extensible, very customizable, and very powerful.

nanoInstalled on most Unix/Linux machines by default.Very simple to use, but at the cost of extensibility and features.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 10 / 17

Page 21: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Editors

viVery minimal, installed on nearly every Unix/Linux machine by default

vimVi IMproved. Much easier to use, also installed on nearly everyUnix/Linux machine by default. Extensible and very customizable.

emacsMore feature-packed than vi/vim.Extensible, very customizable, and very powerful.

nanoInstalled on most Unix/Linux machines by default.Very simple to use, but at the cost of extensibility and features.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 10 / 17

Page 22: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Reserved/Special Characters

bar > foo Directs the standard output (stdout) of bar to foobar >> foo Append stdout of bar to foobar < foo Executes bar with foo as the inputbar | foo Executes foo with it’s input as the output of barbar ; foo Runs bar. When it finishes, runs foobar & foo Run bar, but don’t wait for it to finish to run foobar & Run bar in the background, freeing the terminal(foo ; bar) Run foo and bar as a sub-shell, treating their output as one.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 11 / 17

Page 23: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Arguments and Macros

Consider the command: mkdir alice bob chuck

Macro Meaning Value$0 Command name mkdir$1 Argument 1 alice$2 Argument 2 bob$* All arguments alice bob chuck$# Number of arguments 3# Rest of the line is a comment [none]

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 12 / 17

Page 24: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Arguments and Macros

Consider the command: mkdir alice bob chuck

Macro Meaning Value$0 Command name mkdir$1 Argument 1 alice$2 Argument 2 bob$* All arguments alice bob chuck$# Number of arguments 3# Rest of the line is a comment [none]

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 12 / 17

Page 25: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Key Combos

c Stop the running process, returning to shell (process can prevent this)z Pause the running process, returning to shell (resume with “fg”)\ Kill whatever process is running (process can’t stop this)r Reverse command search through your historyl Clears the terminal window (same as the “clear” command)!! Macro for the previously run command!$ Macro for the previous argument

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 13 / 17

Page 26: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Permissions – What can’t you do?

Permissions described two ways: letter combo or octal string for each typeof user

OwnerOther users in the owner’s groupUsers not in the owner’s group

Letter combo: rwx (Read, Write, Execute)Read (r) - can read the fileWrite (w) - can make changes or remove the fileExecute (x) - Can execute the program contained in the file or executethe script

rwxr-xr-x = Owner can do all, everyone else can read and execute

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 14 / 17

Page 27: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Permissions – What can’t you do?

Permissions described two ways: letter combo or octal string for each typeof user

OwnerOther users in the owner’s groupUsers not in the owner’s group

Letter combo: rwx (Read, Write, Execute)Read (r) - can read the fileWrite (w) - can make changes or remove the fileExecute (x) - Can execute the program contained in the file or executethe script

rwxr-xr-x = Owner can do all, everyone else can read and execute

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 14 / 17

Page 28: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Permissions – What can’t you do?

Permissions described two ways: letter combo or octal string for each typeof user

OwnerOther users in the owner’s groupUsers not in the owner’s group

Letter combo: rwx (Read, Write, Execute)Read (r) - can read the fileWrite (w) - can make changes or remove the fileExecute (x) - Can execute the program contained in the file or executethe script

rwxr-xr-x = Owner can do all, everyone else can read and execute

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 14 / 17

Page 29: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Permissions – What can’t you do? (cont’d)

Octal string: rwx represented in binary

Value r w x Permission0 0 0 0 No permissions (Most restrictive)1 0 0 1 Only execute permissions2 0 1 0 Only write permissions3 0 1 1 Write and execute permissions4 1 0 0 Read-only permissions...

......

......

7 1 1 1 All permissions (Most permissive)

One octal value for each group.755 = Owner can do all, everyone else can read and execute

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 15 / 17

Page 30: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Permissions – What can’t you do? (cont’d)

Octal string: rwx represented in binary

Value r w x Permission0 0 0 0 No permissions (Most restrictive)1 0 0 1 Only execute permissions2 0 1 0 Only write permissions3 0 1 1 Write and execute permissions4 1 0 0 Read-only permissions...

......

......

7 1 1 1 All permissions (Most permissive)

One octal value for each group.755 = Owner can do all, everyone else can read and execute

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 15 / 17

Page 31: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Changing Permissions

chmod – change the file mode bitsSimplest method is octal string:chmod 755 foo.pdfMust be the file’s owner to do this, unless you are the root user (su).Still restrictions with su.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 16 / 17

Page 32: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Changing Permissions

chmod – change the file mode bitsSimplest method is octal string:chmod 755 foo.pdfMust be the file’s owner to do this, unless you are the root user (su).Still restrictions with su.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 16 / 17

Page 33: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Changing Permissions

chmod – change the file mode bitsSimplest method is octal string:chmod 755 foo.pdfMust be the file’s owner to do this, unless you are the root user (su).Still restrictions with su.

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 16 / 17

Page 34: A Brief Introduction to Unixknowak/cs265_spring_2011_e/... · 2011-04-01 · ABriefIntroductiontoUnix SeanBarag  Drexel University March30,2011 Sean Barag

Customization

~/.bashrc (launch of terminal) and ~/.bash_profile (login)

AliasingSettingsColorsAnything you find useful

Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 17 / 17