| PEOPLE | ||
whoami |
Learn about yourself. | |
| who | See who is logged on | |
| finger username | Find out about the person who has an account called username on this host - things like whether they are currently logged on to this host or not. Try subsituting username with your account name, or the user name of one of your classmates. | |
| hostname | Learn out about the host computer you're logged on to. | |
| grep pattern /etc/passwd | Search for pattern in the password file. Everyone with a user account on this host has a line in the /etc/passwd file. This command is useful if you know part of someone's real or account name and want to know more. Try substituting pattern with your account name. The colon-separated line includes the user name, placeholder for an encrypted password, unique user id, default group id, name, full pathname of home directory, default shell. |
|
| su - username | Don't bother to type this in, but we include it FYI. If you know the password for another user account, you can change identities to be that person by using the superuser command. Warning: don't tell anyone your password. This command is typically used by people that have a shared project account or by a system administrator when changing identities to become the superuser known as "root" (su - root), who has permission to do anything and everything. |
|
| THE UNIX SHELL | ||
| What is a "shell"? | A shell is a unix command line interpreter. It reads commands that a user types in at a terminal window and attempts to execute them. There are many different shell programs. You are given a default one with your user account, but you can change it. One of the first if not the first shell was called "sh". Others include csh (the C shell), ksh (the Korn shell), tcsh, zsh, and bash (the "born again" shell which is based on sh). |
|
| ps | ps means "process status". This command command reports what programs you are currently running. What shell are you running? | |
| THE UNIX MANUAL | ||
| man | Use the man (manual) command to look up a command's sytnax and usage.
Understanding man pages takes practice but it's an invaluable skill if you work on a unix platform.
Examples:
|
|
| MOVING AROUND A UNIX FILE SYSTEM | ||
| What is the "unix file system "? | It's an n-ary tree where all the files (including special files such as directories (folders), programs, and device drivers are kept). Here is a picture of a typical unix file system |
|
| pwd | pwd means "print working directory". It prints the full path name (starting at the root) of your current directory. | |
| cd full_or_relative_pathname | cd means "change directory"; it's used to change your location in the file system. A full path name is the complete path to a file, starting at the root directory, with forward slashes as separators. Sample full pathnames:
A relative pathname is a path to a file starting at the current directory, with forward slashes as separators. A relative pathname never begins with a forward slash "/". Shell metacharacters "." and ".." mean current directory and parent directory, respectively. Sample relative pathnames:
|
|
cd cd ~ |
With no argument, the cd command will "bring you home" - back to your login directory, no matter where you are currently located in the file system. Because the tilde "~" is a shell metacharacter that means "home directory", "cd ~" will also bring you home. |
|
| COPYING, MOVING, RENAMING, AND REMOVING FILES | ||
| cp file(s) path | cp means copy. The file(s) are copied to path. | |
| mv file(s) path | mv means move. The files(s) are moved to path. Also used to rename a file. | |
| rm file(s) | rm means remove. It removes file(s). To remove directories, use the -r option (for a recursive remove). | |
| rmdir file | rmdir means "remove directory". It will remove the directory file only if it is empty. | |
| SHELL METACHARACTERS FOR MATCHING FILE NAMES | ||
| * | Matches any number of characters. Examples:
|
|
| ? | Matches any one character. Examples:
|
|
| [characters] | Matches one character listed in square brackets. Examples:
|
|
| PIPES | ||
| program1 | program2 program1 | program2 | program3 |
The vertical bar "|" is called the pipe symbol. Here the output from program1 becomes the input to program2 and so on. | |
| ls -la | more | See the full contents of a large directory one screen full at a time. | |
| cat file.txt | java MyClass | Execute the main method of the java class MyClass, feeding it file.txt as standard input. | |
| cat file.txt | java MyClass | sort | more | Execute the main method of the java class MyClass, feeding it file.txt as standard input, sorting the output, and viewing the sorted output one screen full at a time. | |
| REDIRECTING INPUT AND OUTPUT WITH SHELL METACHARACTER >, >>, and < | ||
| program > file | The ">" redirects the command output to a file. It will create a new file or overwrite a file having the same name (if permissions/quota system permits). | |
| program >> file | The ">" appends/adds the command's output to a file. It will create a new file or add to an existing file with the same name as file (if permissions/quota system permits). | |
| program < file | The "<" redirects input. Here the command is run and its input is taken from file. | |
| redirecting "standard error" | It's also possible to redirect error messages from a command or program to a file. The mechanism varies from shell to shell. | |
| CREATING FILES AND DIRECTORIES | ||
| mkdir directory | mkdir means "make directory". It creates a new directory (if permissions and the host's quota system allows it). directory may be a full or relative pathname. | |
| emacs and vi | Text editors worshipped by rival cults. | |
| touch file | Updates the specified file's last modification time, or creates an empty file if the specified file doesn't already exist. It comes in handy sometimes, e.g. to quickly create some files to experiment with, or to ensure that a source file gets rebuilt when using make. | |
| FINDING FILES | ||
find . -print |
Starting with the current directory, recursively print the names of all files beneath it. |
|
| find ../src - print | Starting with the sibling directory src, recursively print the names of all files beneath it. | |
| find . - print | grep pattern | List all files below whose pathname contains pattern. | |
| find . -type d -exec chmod 775 {} \; | Change the mode/permissions of all directories under the current one to rwxrwxr-x. | |
| find . -type f -exec chmod 660 {} \; | Change the mode/permissions of all directories under the current one to rw-rw----. | |
| ARCHIVING COMPRESSING FILES | ||
| Create and compress and archive: | ||
| tar cvf somename.tar dir1 | tar means "tape archive". It was originally used to back up files/directories on to a magnetic tape. Although tapes aren't used much anymore, tar is still widely used both for creating an archive (which is a file with potentially lots of files contained inside it that keeps track of file hierarchies) and for simply compressing files to make the process of transporting/downloading them quicker. The options cvf mean create verbosely (print file names as they are added) a file. Here we're creating a new file called "somename.tar" which is our new archive. A copy of all the files in the directory called dir will be added to the archive. tar recursively visits every file under dir to do this. |
|
| tar cvf somename.tar file1 file2 dir2 | Here's another example showing that you can give a list of files/directories to put in the archive. In the case file1, file2, and all the files in dir2 are added. | |
| gzip somename.tar | Usually people compress the archive further by using gzip. This makes the archive smaller and easier to transprot. This will rename the file to somename.tar.gz. If gzip doesn't work, try zip. |
|
| Decompress and unbundle and archive: | ||
| gunzip somename.tar.gz | Unzips the file. Renames the file to somename.tar. If gunzip doesn't work, try unzip. | |
| tar xvf somename.tar | The x option means extract. This command copies all the files in the archive in to the current directory, recreating the exact file hierarchy of the files that were archived (e.g. file1, file2, dir2). | |
| ls -l | Check to see that the files were extracted. | |
| rm somename.tar | Usually you want to remove the archive to free up the space it's taking. | |
| NETWORK CONNECTIVITY | ping eniac.seas.upenn.edu ping www.upenn.edu |
Test network connectivity with another host. |
| COPYING FILES FROM ONE UNIX MACHINE TO ANOTHER | ||
| scp file login@remotehostname:dest | scp means "secure copy". Copies file on the current host to the destination path on remotehost. The example below copies ClassX.java (in the current directory on the unix machine I'm logged on to) in to my eniac account's cse100/hw8 directory. scp ClassX.java mylogin@eniac.seas.upenn.edu:cse100/hw08 |
|
| scp login@remotehostname:file dest | Copies file on remotehost to current host. scp mylogin@eniac.seas.upenn.edu:cse100/hw08/ClassX hw08 |
|
| REMOTE LOGIN | ||
| ssh remotehost | Use "secure shell" to log on to a remote host. You'll be prompted for a password; the same user name is assumed. Example: ssh blue.seas.upenn.edu |
|
| ssh login@remotehost | Use "secure shell" to log on to a remote host. You'll be prompted to enter the password for the "otherlogin" account. Example:
ssh otherlogin@blue.seas.upenn.edu |
|