Introduction to Unix CSC 135 Professor John Carelli

  • Slides: 17
Download presentation
Introduction to Unix CSC 135 Professor John Carelli Kutztown University Computer Science Department

Introduction to Unix CSC 135 Professor John Carelli Kutztown University Computer Science Department

Unix basics What is Unix? • Unix is an Operating System (Linux is an

Unix basics What is Unix? • Unix is an Operating System (Linux is an open-source variant) • Developed at Bell Laboratories (late 1960’s) • written in “C” • Command based • A user types commands at a “command prompt” • Commands are interpreted and executed by a shell • Various shells are available • bash is used in this course • Others are: csh, tcsh, ksh, zsh, … Professor John Carelli Kutztown University bash$ pwd /export/home/public bash$ cd carelli bash$ ls file 1 file 2 file 3 Computer Science Department

Directory structure • Unix is organized in a hierarchy of file directories • Like

Directory structure • Unix is organized in a hierarchy of file directories • Like folders in MS Windows/DOS • Directories can contain files or other directories • Users can navigate up/down the directory structure • Using cd command (“change directory”) • Files/dirs are directly referenced by name in the current directory • … or via a “path”, if located elsewhere • /export/home/public/carelli • Slashes “/” separate subdirectory names • Special directory names • Dot, or period, (. ) refers to the current directory • Double dot (. . ) refers to the parent directory of the current directory • Up one level of hierarchy Professor John Carelli Kutztown University Computer Science Department

Unix Command Syntax $ cmd [options] [arguments] • At the command prompt ($) •

Unix Command Syntax $ cmd [options] [arguments] • At the command prompt ($) • Type the command name (cmd) • Add command options, if any (not required) • Generally begin with a “-” (ex: “ls –l”) • Add command arguments, if any (not required) • Type Enter (Return) • Unix executes the command, then prompts for another command Professor John Carelli Kutztown University Computer Science Department

Common Unix commands • pwd – “print working directory” • ls – list the

Common Unix commands • pwd – “print working directory” • ls – list the files/directories in the current directory • cd – change directory • mkdir – make, or create, a new directory • cat – print out the contents of a given file or files (also, less and more) • cp – copy a file/directory • mv – move a file/directory from one name or location to another • rm – remove a file (delete it) • rmdir - remove a directory (delete it) • man – output a manual (usage) page for a command Professor John Carelli Kutztown University Computer Science Department

Unix Commands: pwd • Print working directory • Prints out the current directory location

Unix Commands: pwd • Print working directory • Prints out the current directory location • Usage: pwd • Generally is not used with any options – just report the current directory Professor John Carelli Kutztown University Computer Science Department

Unix Commands: ls • List directory • Outputs a listing of the contents of

Unix Commands: ls • List directory • Outputs a listing of the contents of a directory • Usage: ls [options] [dirname …] • Print out the contents of the named directory (dirname) or directories • If no directory name is provided, output the contents of the current location • Common options: –l : output a listing in “long format” (lots of additional file information) -a : output all files, including “hidden”, “dot”, files (files whose names begin with a “. ”) -C : append special characters to file names to indicate file type -1 : output in a single column Professor John Carelli Kutztown University Computer Science Department

Unix Commands: cd • Change directory • Allows the user to navigate from one

Unix Commands: cd • Change directory • Allows the user to navigate from one directory to another • Usage: cd newdir • Will change the current location to the named directory (newdir) cd • Will change the current location back to the last directory visited Professor John Carelli Kutztown University Computer Science Department

Unix Commands: mkdir • Create a new directory • Usage: mkdir dirname • Create

Unix Commands: mkdir • Create a new directory • Usage: mkdir dirname • Create a new directory in the current directory mkdir path • Create a new directory in the specified location • Ex: mkdir /home/STUDENTS/abc 123/newdir mkdir –p path • Create a new directory in the specified location • Also create any missing intermediate directories in the path Professor John Carelli Kutztown University Computer Science Department

Unix Commands: cat (less, more) • Output (to the terminal) the contents of a

Unix Commands: cat (less, more) • Output (to the terminal) the contents of a file or files • Short for “catenate” (like “concatenate”) • Usage: cat file 1 [file 2 …] • Print out the listed files in the order given • The result is run together – nothing identifying which file the output came from • Would only want to do this with text files (otherwise get gibberish) • less and more also output the contents of a file • But they stop when the screen is full (useful for big files) • Press the spacebar to get the next screenfull • Type “q” to quit, or stop the output Professor John Carelli Kutztown University Computer Science Department

Unix Commands: cp • Copy a file or directory to a new file or

Unix Commands: cp • Copy a file or directory to a new file or location • Behavior depends on argument types • Common usage: cp file 1 file 2 • Make a copy of file 1 and name it file 2 cp file 1 dir • Place a copy of file 1 (with the same name) in directory dir cp file 1 file 2 [file 3… ] dir • Copy all listed files (with the same names) into directory dir cp –r dir 1 dir 2 • Recursively copy dir 1 (entire directory and all of it contents) into dir 2 Professor John Carelli Kutztown University Computer Science Department

Unix Commands: mv • Move a file or directory to a new name or

Unix Commands: mv • Move a file or directory to a new name or location • Similar in operation to “cp” • Common usage: mv file 1 file 2 • Move file 1 to file 2 (basically, rename it) mv file 1 dir • Move file 1 into the directory dir mv file 1 file 2 [file 3… ] dir • Move all listed files (with the same names) into directory dir mv dir 1 dir 2 • Move entire directory dir 1 to dir 2 Professor John Carelli Kutztown University Computer Science Department

Unix Commands: rm • Remove (delete) files or directories • Common usage: rm file

Unix Commands: rm • Remove (delete) files or directories • Common usage: rm file 1 [file 2 file 3…] • Delete all listed files rm –r dir 1 [dir 2. . ] • Delete all listed directories • Be careful with this one, it can do a lot of damage! • In all cases, a full file path can be used instead of a file name • If a file name is given without a path, it is assumed to be in the current directory • Ex: rm /home/STUDENTS/abc 123/file 1 Professor John Carelli Kutztown University Computer Science Department

Unix Commands: rmdir • Remove (delete) directory • But only if it is empty!

Unix Commands: rmdir • Remove (delete) directory • But only if it is empty! • Like “rm –r”, but safer, because the directory must be emptied first • Usage: rmdir 1 [dir 2 …] Professor John Carelli Kutztown University Computer Science Department

Unix Commands: man • Output (to the screen) a manual page for a unix

Unix Commands: man • Output (to the screen) a manual page for a unix command • help utility • All commands described here have many more options! • Note: the output is passed through the more command • So, must use the spacebar to continue seeing the “man page” • Usage: man cmdname Ex: man ls Professor John Carelli Kutztown University Computer Science Department

Additional topics (directories) • “Dot” directories • A dot (. ) can be used

Additional topics (directories) • “Dot” directories • A dot (. ) can be used to represent the current directory • A double dot (. . ) is one directory “up” (the parent of the current directory) • Paths to a file or directory • In any unix command, a full file or directory path can be used • If a file/directory name is given without a path, it is assumed to be in the current directory mv /home/STUDENTS/abc 123/file 1. cp /home/STUDENTS/abc 123/file 1. Professor John Carelli Kutztown University Computer Science Department

Additional topics (wildcarding) • Asterisk “*” is a wildcard character • It can be

Additional topics (wildcarding) • Asterisk “*” is a wildcard character • It can be used in unix commands to do file/directory name “matching” • Examples: ls *. cpp • Will list all files that end with “. cpp” cp /home/STUDENTS/abc 123/a*. • Copies all files in the specified directory whose names begin with the letter “a” into the current directory rm * • Remove all files in the current directory (yikes!) Professor John Carelli Kutztown University Computer Science Department