31
CS 197U: Introduction to Unix Lecture 2: Directory Structure, File System, Basic Commands, Setting Up Linux Instructor: Arun Dunna Lectures: Monday/Wednesday, 4pm - 5:15pm, LGRC A301 CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 1

C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

CS 197U: Introduction to Unix

Lecture 2: Directory Structure, File System,Basic Commands, Setting Up Linux

Instructor: Arun Dunna

Lectures: Monday/Wednesday, 4pm - 5:15pm, LGRC A301

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 1

Page 2: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

File Systems

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 2

Page 3: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

File SystemUNIX consists of files and processes

Files: Either a destination for, or a source of, a data stream"Normal" Files: documents, images, programs, etc.

Directories: equivalent to folders; used to store more files

Processes: A currently running program, referred to by aprocess identifier (PID) - a unique number

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 3

Page 4: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Directory StructureThe UNIX file system is a hierarchy, and can be representedby a tree

The top of the hierarchy (root of the tree) is aptly called rootIt is represented by a single forward slash: /

Forward slashes represent "going a directory deeper"ex: /home contains user-specific directories, such as/home/adunna if my username is adunna

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 4

Page 5: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Common Directories 1/3 (important)Important to know to easily find files!

/bin : Common programs sharedbetween system, administrators, andusers ex: /bin/sh and /bin/bash

/boot : Startup files

/dev : References to hardware ex: /dev/input/mouse0 is mouse

/etc : System configuration files

/home : Home directories for users

/lib : Contains library files - resources needed for programs,such as other smaller programs

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 5

Page 6: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Common Directories 2/3 (important)/lost+found : Files are

saved here duringfailures (like interruptinga process by interruptingsystem power)

/mnt : Mounting forexternal file systems, such as flash drives or DVDs

/opt : Sometimes, software installs here - usually largerprograms, such as Google Chrome

/proc : Contains information about system resources andprocesses ex: /proc/cpuinfo has information about your CPU

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 6

Page 7: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Common Directories 3/3 (important)/root : Home

directory for rootuser (not in/home , unlike

other users!)

/sbin : System-related programs

/tmp : Temporary file storage - it is cleared at reboot!

/usr : Programs, libraries, documentation, and other filestorage for user programs

/var : Storage for "variable" files, which means files that areregularly written to (for example, logs!)

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 7

Page 8: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Some Questions1. If your username were student , where would you put your

user files?i. Where would you put your documents?

ii. Where would you put your music?

2. If I'm a developer, where should I write my log files to?

3. Where is Google Chrome likely to install to?

4. Where should I look for some corrupted files if I unplugged thepower from my system?

5. As a developer, where should I store my programconfiguration?

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 8

Page 9: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Notes About NamesNote: Unix has case-sensitivity, file extensions aren't required, andfiles starting with . are hidden, such as .config

\ : used to escape characters, so \\ matches "\", \&matches "&", etc.

The Tonight Show.mp4 → The Tonight Show.mp4 The\ Tonight\ Show.mp4 → The Tonight Show.mp4

"" : can be used to specify filenames with special charactersinstead of using escapes

"The Tonight Show.mp4" → The Tonight Show.mp4

"The Tonight Show.mp4" = The\ Tonight\ Show.mp4 "Tom&Jerry.jpg" = Tom&Jerry.jpg

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 9

Page 10: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Standard Input/Output (I/O)Two files you'll see/use a lot are stdin and stdout

These are the default sources of ( stdin ) anddestinations for ( stdout ) data Ex: The default output for stdout is your terminal, andthe default input for stdin is your keyboard!

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 10

Page 11: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Standard I/OProcesses inherit their stdin and stdout from their parent,so if you run a program in your terminal, by default it will outputto that terminal

There is also stderr for error output, which is usuallydirected to the same place as stdout

Sometimes you may redirect stderr to elsewhere if youdon't want to show error messages

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 11

Page 12: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Directory Shortcuts. : Current directory .. : Parent directory ~ : Current user's home directory (ex: ~ for me is /home/adunna ) / : Root directory

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 12

Page 13: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Some Basic File System Commands

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 13

Page 14: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

pwd, ls, cdNote: Options can be specified by two hyphens and then a word ifthe program supports it, so for example ls -a = ls --all

pwd : Prints current directory

ls : Lists files in current directory

-a , --all : Includes files starting with . (hidden files)

-l : Uses long listing, showing extra details

cd : Changes directory, can be relative or absolute

cd → goes to home directory

cd dir → goes to dir

cd .. → goes up a directory

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 14

Page 15: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

mkdir, rmdir, rmmkdir dir : Makes directory (or directories) if they don't exist rmdir dir : Removes directory (or directories) if they are empty

rm : Removes file(s) or directory(ies).

-f , --force : Forcibly removes (no prompt if don't exist)

-i : Prompt before removing each file.

-r , -R , --recursive : Remove directories and contentsrecursively.

Doesn't remove directories unless this option is used.

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 15

Page 16: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

cp, mvcp file1 file2 : Copies file1 to destination file2 .

cp file1 dir/ : Copy file1 into dir and names it file1 .

-r , -R , --recursive : Copies directories and contentsrecursively.

Doesn't copy directories unless this option is used.

mv file1 file2 : Moves file1 to destination file2 .

mv file1 dir/ : Moves file1 into dir and names itfile1 .

Unlike rm and cp , this will affect directories andsubdirectories, as it will completely move the specified sourceto the specified destination.

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 16

Page 17: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Interacting With Files Commands

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 17

Page 18: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

touch, cat, more/lesstouch file : Creates file if it doesn't exist. If it does exist, it

updates the recorded access time of the file.

cat ... : Concatenates file(s) to standard output ( stdout ).

cat file1 : Sends file1 contents to standard output.

cat file1 file2 ... : Sends contents of all specified files tostandard output.

-n , --number : Number the output lines.

more and less : Read files interactively.

--help : Use this to get navigational commands for thecommands.

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 18

Page 19: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

head, tailhead file1 file2 ... : Prints first 10 lines of file(s) to stdout .

-n , --lines : Changes 10 to n lines.

tail file1 file2 ... : Prints last 10 lines of file(s) to stdout .

-n , --lines : Changes 10 to n lines.

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 19

Page 20: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

vi and vimNote: vim = vi improved; commands in vi are the same in vim ,but vim has some extra stuff. If vim is installed, it usuallyreplaces vi so if you start vi , it will start vim .

vim is a fully featured text editor, and is very commonly used. Itcan be a bit complex at first, but after a few times using it, it willbecome like second nature.

Open vim: vim file

vim features its own "internal commands", usually just called vimcommands.

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 20

Page 21: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Within vimcommand : Does command in vim. ex: :q then ENTER quits vim.

Some Commands: (https://www.fprintf.net/vimCheatSheet.html)

:q : Quits vim. Fails if changes have been made.

:q! : Quits vim without writing changes to file.

:w : Writes changes to file.

i : Moves from visual mode to insert mode, to write text intothe file. ESC or CTRL+C exits the mode.

d[n]d : Deletes n lines. dd just deletes one. d9d deletesnine lines.

u : Undoes most recent change.

. : Repeats latest change.CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 21

Page 22: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Let's Look At Some LinuxDistributions!

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 22

Page 23: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Simplified Tree of Linux Distributions

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 23

Page 24: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Elementary OS

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 24

Page 25: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Ubuntu

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 25

Page 26: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Linux Mint

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 26

Page 27: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Arch

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 27

Page 28: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Debian

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 28

Page 29: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

My RecommendationsPersonal Preference:

1. Ubuntu

2. Debian

3. Linux Mint

4. Arch

5. Elementary OS

Easiest To Use (Least Amount of Setup Time):1. Ubuntu

2. Elementary OS / Linux Mint

3. Debian

4. ArchCS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 29

Page 30: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Setting Up LinuxRecommended 25 - 30 GB, but 15 - 20 GB will work.

Virtual Machine: Run within your own operating system, suchas Windows or Mac OS. Short setup time.

Easy to do with any OS / Laptop. (VirtualBox)

Dual Boot: Run alongside another OS. Longer to setup, bestfor immersing yourself in Linux, and best for budget laptops.

Easiest to do with Windows. Can't help with Mac.

Alternative for Mac: Since Mac is Unix-based, Mac users canchoose to just use Mac OS. There are benefits from using aLinux distribution, as even if you work on Mac you'll still useservers running Linux, but it's your call.

CS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 30

Page 31: C S 1 9 7 U : I n tr o d u c ti o n to U n i x · C S 1 9 7 U : I n tr o d u c ti o n to U n i x L ectu r e 2: Di r ecto r y S tr u ctu r e, F i l e S ystem, Basi c Co mman d s, S

Wrap UpNow to setup Linux! You can leave if you want, but this is valuablein-class time to setup Linux if you've never done so before.

Pick a distribution and pick an install method. The easiest route is aUbuntu Virtual Machine with VirtualBox. I'll demonstrate setting thisup in-class.

Remember: Lab 1 and Quiz 1 are due on Sunday, 11:59pm

Next Time

System Commands

Redirects, Pipes, and Links

Key Shortcuts

More Text ManipulationCS 197U: Introduction to Unix | Lecture 2 | 1/30/19 | Arun Dunna 31