1 / 86

Day 1 Introduction to Unix

Day 1 Introduction to Unix. What is Unix?. Unix is a computer operating system What does an O/S do? Manage hardware and software resources, e.g. processor, memory, disk space. Provide a consistent way for applications and users to interact with hardware without having to know all its details.

taran
Download Presentation

Day 1 Introduction to Unix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 1Introduction to Unix

  2. What is Unix? • Unix is a computer operating system • What does an O/S do? • Manage hardware and software resources, e.g. processor, memory, disk space. • Provide a consistent way for applications and users to interact with hardware without having to know all its details.

  3. Why does a computer need an O/S? • Not all computers have an O/S. E.g., a microwave computer does not have an O/S. • Microwave computer: • Has one set of simple tasks to perform, • Very simple input and output (a keypad and an LCD screen), • Simple, never-changing hardware to control.

  4. Brief History of Unix • Developed at Bell Labs in 1969 by Ken Thompson, Dennis Ritchie and others. • First developed for a small computer with limited resources (small memory and disk). - Thus, Unix programs had to be small. • Many small programs, each did one small thing. • But, programs can be put together in various combinations to get desired functionality.

  5. Comparisons • Unix vs. Linux • Linux is a Unix-like O/S. • “The goal of Linux was to develop a free Unix system that could be run on both a personal level and on large network servers.” • Most popular vendor of Linux is RedHat. • Many kinds of Unix, e.g. Solaris from Sun, HP-UX from Hewlett-Packard, AIX from IBM

  6. Comparisons (cont.) • Unix vs. Windows (Traditionally) • Windows designed by Microsoft to be “user-friendly.” • Programs are easy to learn and use; each program (e.g. word processor) combines many tasks. • Unix designed for researchers and developers. Programs can be easily combined to perform a multitude of various tasks.

  7. Files and directories • All information on Unix is stored in files that are organized into directories. • A file is simply data associated with a name (the filename): • e.g., text file (.txt extension) contains plain text, • Directories provide a means of organizing files into meaningful groups: • When you log in, you are in your home directory, • Can create a directory for your term papers, a directory for your digital images, a directory for your web pages, etc.

  8. Command Shell • The shell is a “command interpreter”. - It allows a user to interact with Unix O/S. • User enters a command using the shell, O/S executes the command, shell shows output. - Shell Prompt: where you type-in a command. - term: window where prompt appears. • Various kinds of shells, e.g., csh, tcsh, bash.

  9. Command • A command consists of a command-name, followed by 0 or more options (flags), followed by 0 or more operands, e.g., % ls -l *.txt. • Command-name is a name of a Unix program, e.g., ls. • Options/Flags modify the way the command works, e.g., -l. • Operands are names of a file the command will operate on, e.g., *.txt.

  10. Listing files in a directory (ls) • % ls • Lists files in a directory (except “dotfiles”). • % ls -a • Lists all files in the directory, including “dotfiles”. • % ls -l • shows more information about files (e.g. owner, file size, file modify-time). • % ls -F • shows type of files (such as whether it is a regular file or directory). • Can combine flags, e.g., % ls -alF

  11. Working with Directories • pwd • print working directory • shows where you are; your current directory. • mkdirdirectory • Create a new directory, e.g., • % mkdir foome • cd directory • Change working directory, e.g., • % cd foome % pwd % mkdir fooyou % cd fooyou % pwd

  12. Directory Specifications • The current working directory, “.”. • The parent directory, “..”. • Home directory, “~”. • % cd ~ % cd foome/fooyou % pwd % cd ..

  13. Relative vs. Full pathnames • Full name implies a full path to the file/directory on the file system. • Relative name implies name of file/directory relative to current directory. % cd /tmp/foome/fooyou/ or % cd /tmp % cd foome % cd fooyou

  14. Editing files: Emacs text editor • emacs filename & • Start emacs editor, • “&” runs emacs in the background (we discuss this later). • % cd ~/foome % emacs paper.txt & • Used to create, view, and edit files. • If no file exists, a file is created with the name entered • Important Command: C-x C-x • Save changes

  15. Displaying files (cat, more) • cat file • displays contents of a file on the screen, e.g., • % cat ~/foome/paper.txt • morefile • Displays contents of a file on the screen once screen at a time, e.g., • % more ~/foome/paper.txt • Use spacebar to step forward through screens. • Use b to step back through screens.

  16. Copying and Moving files • cpsourceFile copyFile • % cd ~ % cd foome % cp paper.txt bar.txt % ls • mvfromFile toFile • % mv bar.txt bar2.txt • % ls

  17. Removing files or directories • rmfile • % cd ~ % cd foome % rm bar.txt • rmdirdirectory • % rmdir fooyou

  18. Documentation • There are hundreds of Unix commands, and most commands have several options. • Be able to find and use commands out as you need them rather than memorizing all of them (learn how to learn). - Online resources are readily available. - Unix manual (man) pages is an excellent reference manual. - mancommand - Used to search man-pages.

  19. Example: % man ls • % man ls • Describes what ls does and the options it takes, • Describes each of the options, • Gives examples of usages of ls, • Also gives other commands similar to ls, e.g., (chmod, cp, setfacl, etc. …). • Best way to learn is by doing and exploring!

  20. whatis command • whatiscommand • Summarizes what command does, • It actually gives the NAME field of the relevant man page. • Example: • % whatis ls • % whatis cd • % whatis whatis

  21. Having fun with commands • % date • % time • % cal • % echo • % wc • % history • % man man • % whoami • % who am i • % touch

  22. Day 2Combining commandsEnvironment variablesCustomizing Unix

  23. Combining commands -Command Redirection • Heart of UNIX is being able to combine commands to create new commands. • Before, input to command was specified with the operand, and the output is printed to the screen. • Pipe ( | ) operation redirects the output of one command to the input of another command.

  24. Examples • % ls –alF | more • Shows output of ls a page at a time • % ls –alF | sort –r | more • Shows output of ls, in reverse alphabetical order, a page at a time • % ls | wc –l • Gives number of files in the current directory • % ls –s | sort –nr • Lists files by the largest first • % ls –s | sort –nr | head -5 • List top five largest files

  25. Examples contd. • Create the file, stuff.txt, and add the lines One Two One One Two Two Two Three Three One • Try • % cat stuff.txt • % cat stuff.txt | uniq • % cat stuff.txt | uniq | wc -l

  26. File redirection • > • Write output of command to a file, • E.g., ls > myListFile.txt • < • Read input from a file instead of an operand, • E.g., sort < stuff.txt • >> • Append output of command to a file, • E.g., echo Four >> stuff.txt • <<marker • Allows you to input a number of lines to a command until you reach marker

  27. More Examples • % cat > numbers.txt 1 2 3 ^D • % cat >> numbers.txt 4 5 ^D • % cat numbers.txt > numbers2.txt • % cat numbers.txt >> numbers2.txt

  28. More examples • % ls –s | sort –nr > moreStuff.txt • % cat < moreStuff.txt > moreStuff2.txt • % history | sort > mycommands.txt • % cat mycommand.txt | wc –c • % cat mycommand.txt | sort | uniq • % history | sort | uniq | wc –l > mc2.txt • % echo simple test > mycommand.txt • % echo simple test >> mycommand.txt

  29. Wildcard (*) • “*” matches all files except those whose names begin with . (period), or any number of characters within a filename. • Examples: • *.f • Matches all filenames with a .f extension e.g. h.f and verylongfilename.f • a* • Matches all filenames that begin with “a”, e.g., a and anotherfilename.c

  30. Wildcard(?) • “?” matches a single character. • Examples: • ?.f • Matches all one-character filenames with a “.f” e.g., a.f would match, but ab.f would not match. • ?his?ile.txt • Matches both ThisFile.txt and thisfile.txt, but does not match ThisWillNotMatch.txt or hisFile.txt. • A “?” requires that a character be present in the space reserved by the “?”. A * does not have this restriction.

  31. Examples • % touch a.txt % touch b.txt % touch c.txt % touch k.txt % touch owl.a % touch owl.d % touch owl.abc • % ls *.txt • % ls -l a* b* k* • % ls owl.?

  32. grep • greppattern filenames • Seaches filenames for pattern • grep, without flags, finds patterns in a case-sensitive manner • Most helpful grep flags • -c • List a count of matching lines only • -i • Ignore the case of letters in the pattern • -l • List only the names of files that contain pattern • -n • Include line numbers

  33. Examples • % cat > grepExample.txt alpha beta gamma delta ^D • % grep beta grepExample.txt • % grep Beta grepExample.txt • % grep –i Beta grepExample.txt • % grep –i –n Beta grepExample.txt • % grep –l beta grepExample.txt

  34. More Examples • % grep ‘b’ grepExample.txt • % grep ‘e’ grepExample.txt | sort • % grep –l ‘e’ *.txt | sort • % grep –n ‘e’ *.txt | sort > grepResult.txt

  35. find • findpath pattern • Finds files containing pattern in the directory hierarchy beginning with path. • Like grep –l pattern, but more powerful • In general, find is used to search for files, and grep is used to search within files. • Most helpful find flags • -atime n • True if file was accessed n days ago (use –n, +n, and n to indicate n days or less, n days or more, or exactly n days) • -type t • True if file is of type t (t=d  directory, t=f  file) • -name pattern • True if filename that matches pattern • -print • Print names of the files that matched

  36. Examples • % find . -print • List of all files and directories in and below the current directory • % find . -name “*.txt” –print • List all .txt files in and below the current directory • % find . –atime -7 –name “*.txt” –print • List all .txt files in and below the current directory that have been accessed in the last 7 days. • % find ~ –type d –print | more • List of directories in the home directory

  37. Customizing your Unix account: alias • aliasnamedefinition • Creates a shortcut (name) for a command or series of commands (definition) • E.g., % alias ls ‘ls –alF’ % ls • alias name • Gives the definition for the shortcut, if there is one. • E.g., % alias ls % alias mv • unaliasname • Removes an alias • E.g., % unalias ls % ls

  38. Customizing your prompt • set prompt = ‘something’ • sets your prompt to something • E.g., set prompt = ‘my new prompt> ’

  39. Customizing your prompt • Useful codes you can use in your prompt • %c • Current working directory • %m • Machine name • %t • Current time • %d • Current day • \n • Prints a new line • E.g., set prompt = ‘\n[%d] on [%m::%c] at [%t]\n->’

  40. Customizing your Unix account: Dotfiles • Files that begin with “.” (period) • Use ls -a to list them • Special files • The current working directory, “.” • The parent directory,“..” • Other dotfiles describe your session initialization procedures and store user customizations.

  41. The .cshrc file • File does two main things • Environment Setup • Sets up UNIX environment variables, command search path, etc. • Shell Setup • Sets up aliases, command prompt, etc. • The file is executed when the user logs in (global effect), or starts an xterm (local effect), or performs the command source ~/.cshrc (local effect)

  42. Creating a file of personal customizations • Add the following line to ~/.cshrc, so that your customizations are setup when you log in: source ~/.myAliases • Add your customizations to ~/.myAliases. • If you make changes to ~/.myAliases, to get the changes to take effect you can either log out and log back in, or just do source ~/.myAliases in an xterm window.

  43. Useful Customizations for ~/.myAliases • alias rm ‘rm –i’ • alias mv ‘mv –i’ • alias cp ‘cp –i’ (prevents accidentally removing a file, moving a file onto another file, or copying a file onto another file). • set prompt = ‘\n[%m::%c] at [%t]\n->’ • Remember, you need to: source ~/.myAliases for the changes to take effect (or log out and log back in again).

  44. Unix environment • The working environment is a list of variables that are accessible to Unix programs. • The O/S sets up the environment, with the aid of the “dotfiles”, when a user logs in. • Programs can reference variables in the environment to help them function. • % env or % printenv • Prints out a user’s environment.

  45. Descriptions of some environment variables • HOME: user’s home directory • USER: user’s username • PATH: defines which directories for Unix to search to find a command the user has specified at the prompt • SHELL: gives the type of shell (command interpreter) • To print a particular environment variable, you can also printenv • E.g., printenv HOME

  46. Day 3Ownership and PermissionsJob Control

  47. Why does UNIX need file permissions? • UNIX is a multi-user operating system, thus, it allows many different users to take advantage of the computer’s resources simultaneously. • Question: Because UNIX was designed to support many users, how to determine who sees what file? • Answer: use file permissions.

  48. Users and Groups • Unix uses a three-part system to determine file access: there’s what the file owner can do, what a group is allowed to do, and what everyone else is allowed to do. • Every user on the system has a username. • Unix also supports creating groups of users. A user can belong to any number of groups.

  49. ls -a • ls -a returns, for each file: • A ten-character-long string of letters and hyphens (permissions), • The number of hard links to the directory (do not worry about this for now), • The username of the file owner, • The name of the group of users that can access the file, • The size of the file, in bytes, • Date and time of the last modification for the file, • And finally, the filename.

  50. Reading Permissions • First letter (d or -): • Indicates this is a directory • Next three letters: • Indicates what the user who owns the file can do • Read: ability to see the contents of the file • Write: ability to change the contents of the file or delete the file • Execute: indicates whether the file is also a command that can be run on the system (later, we will show how all commands in Unix are stored in files that can be created, modified, and deleted like any other file) • Second set of three letters: • Indicate permissions for group • Third set of three letters: • Indicate permissions for everyone else • Directories treat the execute permission differently. If a directory does not have execute permissions, that user (or group, or other users) cannot cd into the directory.

More Related