UNIX - The basics and beyond
Compiled by Terralyn Vandetta


Getting Around
 
cd
Takes you to your home directory. Your home directory is the place where files like .cshrc and .login are located. This is the directory you start in when you login.
cd ..
Move up one directory to the parent directory.
cd <directory>
Go directly to the directory you are looking for.
cd ~<user>
Go to <user>'s home directory


Looking Around
 
ls
List current directory contents
ls –l
Provide a long listing with information like permissions, owner, date last modified. Will also give information on links.
ls –a
Show hidden files.
ls –R
Recursively list all files, directories, and subdirectories.
ls -lt
List (long) in order with most recent files first.
ls *.dat
List all files with a .dat extension.
ls -lg
Show the group ownership, as well as the user ownership.
ls -ld
Show only the directory, and none of the files. 


Looking for something
 
find . -name <string> -print
Find all files by the name of <string> from the current directory down through all subdirectories and print it out. If you wish to use wildcards (like *), put "" around the <string>.
find . -user <name> -print
Find all files owned by <name> from the current directory down through all subdirectories and print it out.
find . -name <string> -ls
Print out a long listing of the file, instead of just the name. 
grep <string> <file>
Search for <string> in <file>.
pwd Print the current working directory. Many folks have the current working directory as their prompt.  This is the "Where am I?" command.


How much room do I have and how much am I using?
 
df .
Show how much space is available in the current directory. The available amount is given in the fourth column. These numbers are in kilobytes. Another interesting tidbit of information that shows up in the Filesystem column is where the directory is nfs mounted from. In this case it is larix, and the directory on larix is /export/home/terralyn. The directory that you have accessed is actually /users/terralyn. 
df
Shows all of the filesystems currently mounted on your machine.
df -kl
(Solaris)
df -t 4.2
(SunOS) Shows only the local filesystems, and not those that are nfs mounted.
du -s .
Shows, in kilobytes how much room the current directory is using. You can give specific directories, or a * will sum each file and directory separately.


Printing a file
 
lp <file>
(Solaris)
lpr <file>
(SunOS) Prints the <file> to the default printer defined either at the system level, or with the $PRINTER environment variable.
lp -d <printer> <file>
(Solar
lpr -P<printer> <file>
(SunOS)Prints the <file> to the printer specified in <printer>
lpstat -o <printer>
(solaris)
lpq -P<printer>
(SunOS) Shows the files currently in the queue for specified printer.
cancel <jobname>
(Solaris)
lprm -P<printer> <job>
(SunOS) Remove the print job specified by <job> or <jobname> from the queue. In SunOS you have to specify the printer as well, if not the default printer. In Solaris, the name of the printer is included in the <jobname>.


Moving files around
 
cp <file> <destination> 
Copy the file <file> to new location <destination>. <destination> can be the name of a new file, or a directory. If it is a directory, the file will be copied with the same name.
cp -R <directory> <destination>
Copy the directory <directory> and all of its contentst to the new directory, <destination>.
mv <file> <destination>
Rename the file <file> to <destination>. If <destination> is a directory, the file keeps the same name. <file> no longer exists after the mv.
rm <file> 
Remove a file.
rm -r <directory>
Remove a directory and all of its contents.
rmdir <directory>
Remove an empty directory.
rm -i <file>
Ask before removing the file.


Archiving files

Currently there is one 8mm tape drive connected to myrica.fsl.orst.edu located in FSL 345. The commands here will be specific to that drive. You will need to be logged into myrica with a tape in the drive. It is not necessary to be sitting at the console.

With tar you do not want to use absolute paths (that is, paths that begin with a /). The reason is that if you attempt to recover the data from somewhere other than the machine you tarred from, you may not have permissions to create the upper level directories. This is particularly important if you are sending the data off somewhere. For that reason, you want to be in the parent directory of the directory you are trying to archive. So, if you want to archive /data/pspace2/jdoe, first "cd /data/pspace2" and archive "jdoe".
 
/dev/rst1
Rewinding tape device. The tape rewinds after any command given.
/dev/nrst1
Nonrewinding tape device. The tape remains where it is after each command
tar cvf /dev/rst1 <directory>
Archive the contents of <directory> to device /dev/rst1. This will rewind the tape when the tar is done. This will also show on the screen what is being written.
tar cvf /dev/nrst1 <directory>
Archive the contents of <directory> to device /dev/nrst1, showing the contents on the screen. This one will NOT rewind the tape when completed. This is useful if you have multiple directories in separate places to archive.
tar tvf /dev/rst1
Show the contents of the tar file without extracting the files.
tar xvf /dev/rst1
Extract, showing the contents to screen, all of the files in the tar file on the tape device /dev/rst1. Rewind when complete.
tar xvf /dev/nrst1
Extract, showing the contents to screen, all of the files in the tar file on the tape device /dev/nrst1. Do not rewind when complete.
mt -f /dev/rst1 rew
Rewind the tape to the beginning.
mt -f /dev/nrst1 fsf 1
Move the tape forward to the next beginning of file marker. Do not rewind the tape.
mt -f /dev/nrst1 bsf 1
Rewind the tape to the next beginning of file marker. Do not rewind the tape.
mt -f /dev/nrst1 eom
Move to the end of the tape marker and don't rewind.
mt -f /dev/rst1 offline
Rewind the tape and eject from tape drive.


Examples:

Archive /data/pspace/jdoe

Archive /data/pspace/jdoe/dir1 and /data/pspace/jdoe/dir2

Archive /data/pspace/jdoe/dir1 and /data/pspace2/jdoe/dir1 on the same tape

Extract multiple tar files from a tape to /data/pspace/jdoe

Tar can also be used to archive directories to files so that they can be compressed. Simply replace the device, /dev/nrst1, with a file name. For instance:

To extract the information again, there are two methods:


Job Control and Process Management

It is sometimes useful to start a program in the "background". One reason may be that you aren't running in a windows environment, and you need the terminal back to do other work. Or, you may have a process that is going to take a long time, but you need to log off of the machine. To put a process in the background, use the "&" at the end of the command. You can also use it to free up the window you are working in if you don't want to open another one.
 
netscape &
Will start netscape and give you your prompt back. If you start netscape without the "&" and still want to put it in the background, you can: Ctrl-z in the window netscape was started.
bg
puts the stopped process in the background. 

If you stop a process, but don't put it in the background (bg), you may not be able to exit the window. You will get a message "there are stopped jobs". Use "fg" to bring stopped jobs back to the foreground.

If you have a big program you need to run you can also put the job in the background, log out and have it continue to run: nohup bigjob >& bigjob.out &

You want to make sure that you redirect both standard error and standard out to a file. If the program attempts to write to either of these and you don't have a file redirection, the process will stall, and you will have to kill it. The nohup guarantees that it will continue to run after you log out. This is by default in the Cshell, but doesn't hurt either way.

You can set the priority on jobs so that they don't consume all of the CPU. This is a courtesy if you have big jobs running for instance on a machine with lots of users. The program is "nice" and can be used to start a program at a given priority. "renice" will let you change the priority of a currently running program.

nice +20 bigjob
Gives the bigjob the lowest priority.
renice 20 <pid>
Gives the process <pid> lowest priority.


There are several ways to monitor processes that you are running.
 
jobs
Will show you jobs running in the background started in the current session. If you log out, "jobs" no longer works.
ps
Will show you your current process. This is the more common method and has many options:
ps
Shows only the "parent" processes.
ps -x
Shows ALL processes owned by you.
ps -ux
Gives additional information on the processes, like CPU and memory usage.
ps -auxw
Show ALL processes on the system (ax), complete with information on CPU and memory usage (u), and the name of the program with arguments (w)
Sometimes if you are not logged out of a machine gracefully, you will leave processes running. Or, maybe the machine you are working on has locked up. If you can log into another machine, and then rlogin back to the original machine, you can kill the processes.
ps -uxw
Shows you all of your processes.
kill <pid>
Kill the process with process id <pid> (<pid> is the second column)
kill -9 <pid>
If the process didn't die, try kill -9. Multiple <pid>'s can be used with one kill command


Creating Files

If you are using one of the windowing systems (OpenWindows or CDE) you may want to use the GUI text editors available. Otherwise, here is a short primer on "vi"

vi has two modes: command and insert. When you first start vi with "vi <file>" you are placed in command mode. Here are just a few useful commands to be used in command mode. One you are in insert mode, and <ESC> will get you back to command mode.

i
Starts insert mode in front of the cursor position.
a
Starts insert mode after the cursor position.
A
Starts insert mode at the end of the current line.
o
Opens a new line below current line.
O
Opens a new line above current line.
h,j,k,l
Move the cursor left,down,up,right. (Sometimes the arrow keys will work)
w
Move forward one word.
b
Move back one word.
^
Go to the beginning of the line.
$
Got to the end of the line.
yy
Make a copy of the line and place in the cut buffer. (3yy will make a copy of the next 3 lines.)
dd
Delete the current line in the cut buffer. (3dd will delete the next 3 lines)
D
Delete to the end of the line from the point of the cursor.
dw
Delete the current word. (Generally defined as characters to the next whitespace, or special character)
p
Put back what is in the cut buffer after the cursor if it is a word, or on the next line if it is a whole line.
P
Same as "p", only before the cursor.
u
Undo last command.
.
Repeat last command.
G
Go to end of file.
nG
Got to line n. (1G will get you to the beginning of the file)
/string
Search for string, and place the cursor at the beginning of string
n
Go to next occurrence of search string.
cw
Change word. A $ will appear at the end of the string vi thinks is a word. You are in overwrite mode and need to press <ESC> to get back to command mode.
r
Replace a single character.
R
Change to overwrite mode until <ESC> is pressed.
:1,$s/string1/string2/ 
Start at the beginning of the file, to the end and replace the First occurrence of string1 on each line with string2.
:1,$s/string1/string2/g
Same as above, except ALL occurrences are changed.
:.,$s/string1/string2/
Same as above, but start at current location.
:1,.s/string1/string2/
Same as above, but start at beginning and end at current location.
:5,9s/string1/string2/g
Only change occurrences of string1 on lines 5 through 9 with string2.
:r <file>
Read in <file> at current cursor location.
:w <file>
Write current contents to <file>
:wq
Write current contents and quit.
:q!
Quit without saving changes.


Network Commands


Running graphic programs on another machine

In order to run a graphical program on another machine, you must use a couple of Xwindows commands.

Example: Display a clock (xclock) from ilex on trillium.


Redirecting input/output

Sometimes you will have programs that produce a lot of output to the screen that you want to capture. You can "redirect" that output to a file in several ways:

You can use the "<" to redirect standard input (input from the keyboard) to a command. One of the more useful methods is mailing a file to someone from the command line:

mail jdoe < file1

Sometimes you want to take the output from one program and use it as input to another program. The | is called "pipe", and does just that. For instance:

ps -auxw | grep terralyn

takes the output from "ps -auxw" and gives it to the command "grep" to search for the string "terralyn". The output will only be those processes containing the string "terralyn".


File Permissions

There are three sets of permissions associated with a file: user, group, other (ugo). Each group is individually assigned read, write, and execute permissions (rwx). When you do an ls -l of a directory, the first string of letters shows the permissions.

The first letter indicates filetype. It can be "d" for directory, "-" for file, or "l" for symbolic link. Each of the next set of three characters are the permissions for user, group, and other respectively. In the above examples, terralyn, the user (or owner, but UNIX uses "o" for other) owns the directory, Mail, and has read, write, and execute permissions. The group "staff" also has read, write, and execute permissions. The rest of the world (other), only has read and execute permissions. For a directory, you must have execute permissions to be able to "cd" into the directory. In the case of "Terralyn's_Xterm", the group now only has read and execute permissions. If you can read a file, you may copy the file somewhere else. If you have right permissions, you may modify a file. With write permissions, you may also delete the file IF you also have write permissions in the current directory. A deletion of a file requires that the directory table be updated, which requires write permissions.

Useful commands for file permissions:

chmod <options> <file>
Change the protections of the file. You may do this in two forms (check "man chmod" for more details)
chown <user> <file>
Change the ownership of the file. This can only be done as root, or if you are the owner of the file. Being the owner in SunOS still does not allow you to change the ownership.
chgrp <group> <file> 
Change the group ownership of the file.

NOTE: In the above three commands you can add a "-R" for recursive changes, so that all files and directories below your current directory are changed.
 
groups <user>
Tells you what groups <user> is assigned.
newgrp <group>
Start a new subshell with the default group as <group>. All files created during the session will have group ownership of <group>, instead of your default <group>


Miscellaneous

Now all of the good stuff, that makes your life a little easier.

File Completion In the Cshell, use the <ESC> key to complete the name of a file that you are typing. It will be completed if what you have typed is unique. If it isn't, you will get a beep and nothing else. A <ctrl>-d will show you what the possibilities are. If you are in a Cshell and <ESC> doesn't work, try "set filec"

History The Cshell keeps track of previous commands if "set history=n" has been set.

Aliases Setting up short cuts for stuff you do all the time, but don't like to type. These should be added to your .cshrc file located in your home directory


  Links  Create symbolic links in your home directory with so you don't have to type the full pathnames of data directories. 

Creates a "link" in the current directory called "mydata" that points to /data/mydatadisk/mydata. Now you can "cd mydata" and get to /data/mydatadisk/mydata.

Quotations ",',` all have different meanings.

Repitious commands (foreach statement)

Other commands of interest, but too complicated for the last page: awk, sed


How to get additional help - Try the man pages!

"man" is the command for accessing the online manual pages. The first thing to do is "man man", which will explain how to use man.  Examples:

man ls  Shows all of the options for the command ls.
man -k font   Shows all of the commands that deal with the subject, font.
man is probably the signle best source of information available for those seeking help within a UNIX enviroment, USEIT!



FSRN Home Page / GIS/RS Helpdesk Page / GIS/RS Intro Page