A Brief Introduction to Unix Sean Barag <sjb89@drexel.edu> Drexel University March 30, 2011 Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 1 / 17
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
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
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
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
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
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
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 Unix C:\Documents and Settings\[user]\Desktop\Presentation.pdf /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
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 Unix C:\Documents and Settings\[user]\Desktop\Presentation.pdf /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
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
How to Get Help man an interface to the on-line reference manuals Nearly every command has an entry (if not, try -h or --help) Typically: Synopsis Description Options Defaults Examples References Related Commands Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 5 / 17
How to Get Help man an interface to the on-line reference manuals Nearly every command has an entry (if not, try -h or --help) Typically: Synopsis Description Options Defaults Examples References Related Commands Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 5 / 17
Working with Directories Command pwd cd [path] mkdir [path] rmdir [path] Action Prints present working directory (where you are) Change directory Makes directory Removes an empty directory Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 6 / 17
Working with Files Command ls cp [foo] [bar] mv [foo] [bar] rm [foo] cat [foo] wc [foo] Action List names of all files in the current directory Copy foo to bar, overwrites bar if it exists Moves foo to bar, overwrites bar if it exists Removes named files Prints contents of a file to the command line 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
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 Result ls foo Show the file foo ls * Show all the files ls *.pdf Show all files who s filename ends with.pdf ls b[aeiou]n Show any file with name ban, ben, bin, bon, or bun ls?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.mp5 Many 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
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 Result ls foo Show the file foo ls * Show all the files ls *.pdf Show all files who s filename ends with.pdf ls b[aeiou]n Show any file with name ban, ben, bin, bon, or bun ls?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.mp5 Many 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
Working with Files (cont d) Command grep [pattern] [foo] grep -v [pattern] [foo] tail [foo] tail -n [foo] tail +n [foo] cmp [foo] [bar] diff [foo] [bar] Result Print lines in foo matching a pattern Print lines in foo that don t match the pattern Print last 10 lines of foo Print last n lines of foo Start printing foo at line n Print location of the first difference Print all of the differences Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 9 / 17
Editors vi vim emacs nano Very minimal, installed on nearly every Unix/Linux machine by default Vi IMproved. Much easier to use, also installed on nearly every Unix/Linux machine by default. Extensible and very customizable. More feature-packed than vi/vim. Extensible, very customizable, and very powerful. Installed 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
Editors vi vim emacs nano Very minimal, installed on nearly every Unix/Linux machine by default Vi IMproved. Much easier to use, also installed on nearly every Unix/Linux machine by default. Extensible and very customizable. More feature-packed than vi/vim. Extensible, very customizable, and very powerful. Installed 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
Editors vi vim emacs nano Very minimal, installed on nearly every Unix/Linux machine by default Vi IMproved. Much easier to use, also installed on nearly every Unix/Linux machine by default. Extensible and very customizable. More feature-packed than vi/vim. Extensible, very customizable, and very powerful. Installed 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
Editors vi vim emacs nano Very minimal, installed on nearly every Unix/Linux machine by default Vi IMproved. Much easier to use, also installed on nearly every Unix/Linux machine by default. Extensible and very customizable. More feature-packed than vi/vim. Extensible, very customizable, and very powerful. Installed 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
Reserved/Special Characters bar > foo bar >> foo bar < foo bar foo bar ; foo bar & foo bar & (foo ; bar) Directs the standard output (stdout) of bar to foo Append stdout of bar to foo Executes bar with foo as the input Executes foo with it s input as the output of bar Runs bar. When it finishes, runs foo Run bar, but don t wait for it to finish to run foo Run bar in the background, freeing the terminal 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
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
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
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 history ˆl 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
Permissions What can t you do? Permissions described two ways: letter combo or octal string for each type of user Owner Other users in the owner s group Users not in the owner s group Letter combo: rwx (Read, Write, Execute) Read (r) - can read the file Write (w) - can make changes or remove the file Execute (x) - Can execute the program contained in the file or execute the 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
Permissions What can t you do? Permissions described two ways: letter combo or octal string for each type of user Owner Other users in the owner s group Users not in the owner s group Letter combo: rwx (Read, Write, Execute) Read (r) - can read the file Write (w) - can make changes or remove the file Execute (x) - Can execute the program contained in the file or execute the 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
Permissions What can t you do? Permissions described two ways: letter combo or octal string for each type of user Owner Other users in the owner s group Users not in the owner s group Letter combo: rwx (Read, Write, Execute) Read (r) - can read the file Write (w) - can make changes or remove the file Execute (x) - Can execute the program contained in the file or execute the 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
Permissions What can t you do? (cont d) Octal string: rwx represented in binary Value r w x Permission 0 0 0 0 No permissions (Most restrictive) 1 0 0 1 Only execute permissions 2 0 1 0 Only write permissions 3 0 1 1 Write and execute permissions 4 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
Permissions What can t you do? (cont d) Octal string: rwx represented in binary Value r w x Permission 0 0 0 0 No permissions (Most restrictive) 1 0 0 1 Only execute permissions 2 0 1 0 Only write permissions 3 0 1 1 Write and execute permissions 4 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
Changing Permissions chmod change the file mode bits Simplest method is octal string: chmod 755 foo.pdf Must 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
Changing Permissions chmod change the file mode bits Simplest method is octal string: chmod 755 foo.pdf Must 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
Changing Permissions chmod change the file mode bits Simplest method is octal string: chmod 755 foo.pdf Must 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
Customization ~/.bashrc (launch of terminal) and ~/.bash_profile (login) Aliasing Settings Colors Anything you find useful Sean Barag (Drexel University) CS 265 - A Brief Introduction to Unix March 30, 2011 17 / 17