| UNIX Basics |
- INTRODUCTION
- LOGGING IN TO THE SYSTEMS
- CHANGING YOUR PASSWORD
- UNIX COMMANDS
- REDIRECTING INPUT AND OUTPUT
- PIPES
- ONLINE HELP
- THE UNIX FILE SYSTEM
- THE C SHELL
- BATCH JOBS
- UTILITIES
- BASIC UNIX COMMANDS
Introduction
The UNIX operating system was developed at Bell Laboratories in the early 1970s. It has grown into one of the most widely used operating systems. UNIX runs on a wide variety of computers allowing applications and data to be shared by mainframes, minis, workstations, and high-end PCs. Solaris is the version of UNIX running on the Stern timesharing systems, the Sun SPARCservers sales, rnd, and db.
Logging in to the Systems
When you login, the system will prompts you for your username (NetID found on NYU ID card). Enter your username in lowercase and press <Enter>. Next enter your password and press <Enter>. For security reasons, your password will not be displayed as you enter it.
After you correctly type your username and password, the system will pause briefly and then display system notices.
Changing Your Password
All password changes should be done through SIMON.
Unix Commands
Most UNIX commands are short, usually an abbreviation of one or two words, or a mnemonic. For example, the command to delete a file is rm which is short for the word remove. Important: UNIX is case-sensitive; i.e., it distinguishes between lowercase and uppercase letters (recognizing commands in lowercase only). Most commands follow a common syntax:
command -options arguments
Here are some examples of basic UNIX commands and command syntax:
rnd% ls
lists all of the files in your current directory
rnd% ls -l
gives a long listing of the files in your current working directory including information on size, date of creation and protection level.
rnd% cp a b
copies file a to a new file named b. If b is the name of a subdirectory then file a will be copied into subdirectory b retaining the name a.
rnd% rm a
removes the file named a.
rnd% rm c*.dat
removes all files that have names beginning with a c and ending in .dat. An asterisk (*) represents any number of characters in a file's name.
rnd% mv my.dat new.dat
renames (moves) the file my.dat to new.dat.
rnd% more my.dat
displays the contents of the file my.dat, a screenful at a time. Press the space bar to move forward screen by screen until the end of the file is reached or press the letter q, for quit.
rnd% mkdir study1
makes a subdirectory named study1.
rnd% cd study1
(change directory) moves you into the subdirectory study1. Using cd with no directory name will return you to your home directory.
rnd% rmdir study1
removes the directory study1. A directory must be empty for rmdir to remove it.
rnd% lpr -Plc15 myfile.txt
prints a file called myfile.txt to the printer in LC-15 Tisch Hall.
rnd% lpr -Pl100 myfile.txt
prints a file called myfile.txt to the printer in L-100 Tisch Hall.
rnd% file *
identifies the formats of all files in your current directory, (i.e. ascii text, English text, FORTRAN, directory).
Redirecting Input and Output
Commands usually display their results to the screen. Also, commands normally operate on data as you type it in from the keyboard. A right angle-bracket > (called an "into") on the command line indicates that the next word is the name of a file or device in which to place, or redirect the output of a command:
rnd% ls > list
will place the output of the ls command in a file named list. If a file named list existed before you entered this command, any previous contents will be written over.
You can append to the end of a file using a double right angle-bracket >> (called an "onto"). For example, if the next command entered was:
rnd% date >> list
The output of the date command would be added to the bottom of the file list.
Pipes
The output of one command can be fed in directly as input to another. The symbol for the input/output (I/O) connection is a vertical bar | , called a pipe. (The vertical bar is usually located above the back-slash character, near the upper-right corner of the keyboard.)
For example, a large directory will scroll off the screen if just ls is entered. If a pipe is used to direct the output of ls to be the input of more, then the directory listing will not scroll off the screen:
rnd% ls | more
Online Help
To get on-line help in UNIX, use the man command. It provides access to a comprehensive online UNIX manual. Simply type man followed by a command or topic on which you want information:
rnd% man man
will display the pages of the online manual that explain the man command itself. To make the online manual more helpful, an index is provided. The index is accessed with the -k option of the man command. For example:
rnd% man -k directory
will display a one-line synopsis of all manual pages having to do with directories. To get information on using the UNIX systems at Stern, type man stern at the system prompt.
The Unix File System
UNIX uses a hierarchical file structure, similar to DOS, which is made up of files, directories and subdirectories. A file can hold text, data, or a program. Directories contain files and sometimes subdirectories. A subdirectory is a directory that has been created within another directory. To see a list of files and directories use the ls command:
rnd% ls
a.out* logon ls-list sas-one/ tex/
Permissions
Since UNIX is set up to let users share files, you have the option of allowing or denying access to others on the system. Permissions determine who may access your files and directories or what may be done with a file or a directory. Use ls -l to see what permissions your files and directories have.
rnd% ls -l
-rw------- 1 medved 1314 Aug 20 10:15 logon
-rwx------ 1 medved 916 Aug 19 15:23 a.out*
-r-xr-xr-x 1 medved 784 Aug 28 09:48 ls-list
drwx------ 1 medved 574 Aug 27 15:45 sas-one/
drw------- 1 medved 574 Aug 27 15:45 tex/The letters at the far left indicate the permissions of each file and directory.
d (directory)
If the first letter is a d, the file is a directory. If the first character is a hyphen (-), then it is a regular file.r (readable)
A file must be readable to be looked at or copied. A directory must be readable for you to list its contents.w (writable)
A file must be writable in order for you to modify it, remove it, or rename it. A directory must be writable in order for you to add or delete files in it.x (executable)
A file with executable permissions is one you can run, such as a program or a shell script. A directory must be executable in order for you to move into it (using the cd command), list its contents, or create or delete files there.The hyphen (-) appears when the permission is switched off. For instance, if a hyphen (-) appears in place of an r, then the file or directory is not readable.
Example Meaning
-rw-r--r-- file is read/write for owner, read only for others
-rwx------ file is read/write/execute for owner only
drwxr-x--x directory is read/write/search for owner, read/search for group, searchable only for others
Changing File and Directory Protection
The file and directory protection levels in UNIX can be changed to allow or deny access to other users. The chmod command is used to set the protection level. You can add or remove various protection levels by using either + (add) or - (remove) with chmod and a letter signifying a certain class of users:
u for the file owner
g for a system defined group
a for all usersand the desired access settings:
r for read access
w for write access
x for execute accessFor example, full access to all users for the file my.dat could be granted with the following command:
rnd% chmod a+rwx my.dat
You could fully protect the file my.dat so that no one, including the owner, could modify or delete it with:
rnd% chmod a-w my.dat
The C Shell
The C shell is a program (with a syntax reminiscent of the C programming language) that reads commands from a terminal or from a file (called a shell script) and makes calls to the operating system to execute them. The C shell provides a number of convenient features for interactive use, including command aliasing, history substitution, filename completion, and job control. By default, the C shell is invoked when you login to your UNIX account.
The .login and .cshrc Files
When first started, the C shell performs commands from a file named .cshrc in your home directory. Setting aliases (discussed below) and other C shell specific customizations can be done using the .cshrc file. After executing commands in the .cshrc file, the C shell executes commands from the .login file. Any customizations to your environment can be added here.
The alias Command
The alias command allows you to substitute a short command for a long one, or a single command for a series of commands. To create an alias, use the following syntax:
alias name-of-alias 'alias-commands'
One way to use an alias is to create alternate forms of existing commands. For example, the rm command normally removes files without asking for confirmation. With the -i option it queries the user before deleting. To create a new command named "del" that will query before removing a file use the following alias:
rnd% alias del 'rm -i'
Now del becomes a command alias that can be used like any other UNIX command:
rnd% del file1
Delete file1? [y/n]The new del command alias will function until you log out. If you want the alias to work each time you login, create a .login file in your top-level directory and put the alias in it.
History
The C shell allows you to store a history of previous commands. The number of commands stored can be set by the user (the system default is 50):
rnd% set history=75
Previous commands can then be listed with the history command and retrieved with the ! command.
rnd% history lists the commands stored
rnd% !! repeats the last command
rnd% !3 repeats the 3rd command
rnd% !cat repeats the last command starting with the string cat
On the Stern systems, previous commands can also be retrieved by typing <Ctrl>p (hold down the <Ctrl> key and press p). A retrieved command can be re-executed or edited. <Ctrl>a moves cursor to the beginning of the line, <Ctrl>e moves cursor to the end of the line, <Ctrl>f moves forward one character, <Ctrl>b moves back one character, <Ctrl>d deletes character to the right.
Job Control
UNIX is a multi-tasking operating system. You can suspend interactive programs and return to the C shell by typing <Ctrl>z (hold down the <Ctrl> key and press z). Afterwards you can enter other commands. You can resume your suspended program by entering fg % followed by the job identification number. You can find the job identification number with the jobs command (see below). If the program is non-interactive (i.e., it doesn't need input from, or write output to your terminal) you can let it run on its own in the background: use the bg % command followed by the job identification number. You can terminate any process that is suspended or running in the background with the kill command. Simply type kill % followed by the job identification number. Here are some examples of job control:
The jobs command displays the status of all your jobs:
rnd% jobs
[1] - Stopped pico typescript
[2] Stopped (signal) sas
[3] + Stopped vi fiTo resume the suspended Pico job above (pico typescript) you would enter
rnd% fg %1 (or fg %pico)
To kill the suspended SAS job listed above (job number [2]) you would enter
rnd% kill %2 (or kill %sas)
You can also run a job in background (i.e. you can type in other commands while it's running in the background). To do this end the command line with an ampersand (&):
rnd% tex big-book.tex&
[1] 40014001 is the PID (Process Identification Number), [1] is the job number.
After each command is interpreted by the C shell, an independent process, with a unique process identification number (PID), is created to execute it. The system uses the PID to track the current status of each process. To see the processes you have running, use the ps command.
rnd% ps
PID TT STAT TIME COMMAND
13244 p0 TW 0:01 mm
15169 p0 IW 0:00 -csh (csh)
15262 p2 TW 0:00 sas
15264 p2 IW 0:00 SPSS -i -z -qThe kill command provides you with a direct way to stop commands that are no longer needed, even from a shell running on another terminal. This is particularly useful when you make a mistake in a command or a program that takes a very long time to run. This can be a problem with the mm e-mail program. If you start mm and leave it without quitting (i.e. by entering <Ctrl>z, or turning off your terminal while still in mm), you will not be able to use it properly until your old process is killed. You can identify it by entering ps and looking in the command column.
To kill the mm process listed above enter kill -9 (the -9 ensures the process will be terminated) and the PID number of the mm command:
rnd% kill -9 13244
Be careful with the PID number: you can only kill your own processes, but you can kill the wrong ones and log yourself out accidentally.
Batch Jobs
To run large jobs after you have logged out, use the at or the batch command. First, create a file containing the command line(s) you wish to run later on. For example, a file called sas-later has the one line in it:
sas big-problem.sas
Type in at, followed by the time you wish to run the job, and the name of the file containing the command line(s):
rnd% at -f sas-later 2 am
The command tells the system to run the commands in the file sas-later at 2:00 a.m. Use the atq command to see what jobs are waiting to execute.
rnd% atq
Rank Execution Date Owner Job # Queue Job Name
1st Aug 20, 1991 02:00 ykawabat 57354 a sas-late
2nd Aug 21, 1991 01:00 ykawabat 57350 a logon
3rd Aug 21, 1991 04:00 ykawabat 57353 a a.out
To terminate a batch job, use the atrm command. Terminate individual jobs by entering atrm job#. To cancel logon in the above list, enter:
rnd% atrm 57350
To remove all of your jobs with a single command, enter atrm followed by your username. To cancel all jobs in the listing above, enter:
rnd% atrm ykawabat
Please be conscientious when entering batch jobs; remove jobs you decide not to run.
Utilities
The script Command
The script command records a session to a file. It creates a subprocess that terminates and closes the script file when exit is typed at the system prompt. For example:
rnd% script session1
will write the contents of your session to the file called session1 until you type exit.
The finger Command
The finger command displays the names of all users logged in. It can also be used to obtain information (such as date and time of last login) on one user at a time:
rnd% finger jdoe
Login name: jdoe In real life: John Doe
Directory: /files/students/j/jdoe Shell: /usr/local/csh
Last login Thu Aug 8 10:32 on ttyp8 from BOURSE.STERN.NYU
New mail received Tue Sep 3 15:00:02 1991;
unread since Thu Aug 8 10:42:22 1991
No Plan.
Basic Unix Commands
|
Basic Unix Commands |
|
|
cd |
moves you to another directory |
|
chmod |
sets permissions on a file |
|
cp |
copies a file |
|
file |
lists file type of given file |
|
head |
displays top lines of a file |
|
history |
displays recently entered commands |
|
jobs |
lists active jobs under job control |
|
kill |
kills jobs or processes |
|
lpr |
prints a file |
|
ls |
lists the contents of a directory |
|
man |
displays manual pages about UNIX commands |
|
mkdir |
creates a directory |
|
more |
displays the contents of a file |
|
mv |
moves or renames a file |
|
ps |
displays information about processes |
|
pwd |
tells you which directory you're in |
|
rm |
removes a file |
|
rmdir |
removes a directory |
|
tail |
displays the last lines of a file |


