More on Unix
Assignment :
Read the second chapter in your text book.
.Before proceeding to more Unix commands, it is important to review the basic commands for working with
the file/directory structure. Make some test directories, and run ls to confirm that they are present.
More Useful Unix File System Commands
cp
- make a copy of a file
- Options
- -i : inquire before copying a file over an existing file (existing file is lost)
- Examples
- "cp ~jhm/201/tutor.vi . " : copy the file "tutor.vi" from a subdirectory "201"
located under jhm's home directory (jhm is a user login identifier), to the current directory
("." is a Unix abbreviation for current directory)
- "cp ~jhm/201/tutor.vi ~ " : copy the file "tutor.vi" from a subdirectory "201"
located under jhm's home directory (jhm is a user login identifier), to your home directory
("~" taken alone is a Unix abbreviation for your home directory)
- "cp ~jhm/201/tutor.vi ~/my.vi " : copy the file "tutor.vi" from a subdirectory
"201" located under jhm's home directory (jhm is a user login identifier), to your home
directory giving it a new name of "my.vi"
- "cp tutor.vi my.vi " : copy the file tutor.vi to a file in the same directory with
the name "my.vi". This is a typical way to keep an untouched copy of a file around
when you are about to make some changes.
- "cp tutor.vi My-Stuff " : here I am assuming that a subdirectory called
"My-Stuff" exists under the current directory. A copy of the file "tutor.vi" will by
created in the subdirectory "My-Stuff".
- "cp ../tutor.vi ." : you might use this command if you were in the subdirectory
"My-Stuff and wanted to copy "tutor.vi" from the next directory level up (perhaps your
home directory) into "My-Stuff".
mv
- move a file to another location (rename a file)
- Options
- -i : inquire before moving a file over an existing file (existing file is lost)
- Examples
- "mv tutor.vi my.vi " : the file tutor.vi is given the new name "my.vi".
- "mv tutor.vi My-Stuff " : here I am assuming that a subdirectory called "My-
Stuff" exists under the current directory. The file "tutor.vi" will disappear from the current
directory, but you will find it in the subdirectory "My-Stuff".
- "mv tutor.vi My-Stuff/my.vi " : here I am assuming that a subdirectory called
"My-Stuff" exists under the current directory. The file "tutor.vi" will disappear from the
current directory, but you will find it in the subdirectory "My-Stuff" with the new name of
"my.vi".
- "mv ../tutor.vi ." : you might use this command if you were in the subdirectory
"My-Stuff and wanted to move "tutor.vi" from the next directory level up (perhaps your
home directory) into "My-Stuff", as part of a general reorganization of your files.
rm
- remove (delete) a file that you no longer need. Be careful with this one. Unlike, Macs or
MSDOS, there is no way to change your mind once you have told the system to remove the file.
- Options
- -r : stands for "recursive" and is only used if the name that follows is actually a
subdirectory. This will remove the subdirectory including everything under it (files,
lower levels of subdirectories and all of their contents. Use with extreme caution.
- Examples
- "rm tutor.vi " : remove the file "tutor.vi" from the current directory. .
- "rm My-Stuff/my.vi ": remove the file "my.vi" from the a subdirectory called "My-
Stuff"
.
rmdir
- remove (delete) a subdirecory that you no longer need. Only works if you have emptied the
subdirectory of all contents.
- Examples
- "rmdir My-Stuff ": remove the subdirectory named "My-Stuff"
Creating your own text files with the vi editor
This is how you will create your Fortran programs for this course. The most important thing you need to
know about vi is that it operates in two modes, command and insert. When it first starts, it is in command
mode. Any keys that you hit will be interpreted as commands to vi, not text to be inserted into the file. If
one of the commands that you use is "I" or "a", you are placed into insert mode and subsequent characters
are inserted into the current file. To return to command mode, you must hit the escape key (Esc). If you
remember how to use "I" and "a" plus "x" (delete the character under the cursor) and "dd" (delete the line
at the cursor), you will be able to survive with vi.
Knowing the basics of this editor is important, because it available on any Unix system that you use. You
can walk into a new job, and start doing useful things immediately. I will teach the vi commands only by
example in this course. You should rely on the homework and tutorials to get you started and polish your
skills. If you have problems come to class help sessions.
Some Special Files in Unix
Any file name beginning with a "." is not displayed by "ls" unless you use the "-a" option. From your
home directory (just type "cd" to get there), type "ls -a". You should see several files used by Unix
utilities to store special default information. Two of these are:
- A file of commands that are executed every time you log into the machine. Often your path is set
here.
- A file of commands that are executed every time you start csh or tsch. This happens at login, or
any time you open a new window in an Xwindows session. Your path might also be set or
modified here. You might also see lines starting with "source" to run other files containing
commands, or lines beginning "set" or "setenv" to set shell or environment variables serving as
information sources to various utilities.
If you see a file ".alias" or ".aliases", look in ".cshrc" for "source .alias" or "source .aliases". If the
appropriate "source" command is present, this alias file is run whenever ".cshrc" is, and by convention
contains only "alias" commands. "alias" is the way you can make your own custom command definitions.
To minimize my confusion switching between MSDOS and Unix, I keep some "alias" commands that
translate DOS commands to the closest Unix command, and others that force the use of my favorite
options. For example, my ".aliases" file contains the line:
alias dir 'ls -alF'
to mimic the look of the DOS "dir" command. I also include:
alias ls 'ls -F'
to distinguish between file names and subdirectory names, and between executable and non-executable
files.
I'm nervous about inadvertently overwriting existing files so I also include
alias cp 'cp -i'
alias mv 'mv -i'
If the alias for "cp becomes an nuisance during a major housekeeping operation, I can always type
unalias cp
to remove my forced option.
File Redirection
Those of you familiar with recent versions of MSDOS may have used the concept of file
redirection. There are times when you would prefer that a program's output to your screen be
stored away in a file, either for future processing or so you can ignore it. This task is accomplished
with combinations of the symbols ">", "!" and "&". For example the command
man ls > ls.doc
puts the manual pages for "ls" into a file ls.doc. If "ls.doc" already exists, this command will fail.
However, if you want to overwrite any previous "ls.doc", you can type
man ls >! ls.doc
If you are building up a file with documentation from several commands, you can add more
information onto the end of an existing file "command.doc" with
man ls >> command.doc
You may find these capabilities especially useful when compiling large programs with many
errors. The command
f77 test.f >& compile.out
will capture all normal output plus special system error output (why "&" is used) in the file
"compile.out".
Redirection can also be used for input. If I create a program called "myprog" that normally takes
input from the keyboard, I can replace the keyboard input by lines from a file using "<"
myprog < input.file
One common use of this capability is to mail files to someone. If you want to mail the file "test.f"
to me, just type
mail jhm@cac.psu.edu < test.f
Stopping programs
If a program appears not to be responding and you want to shut it down, you have several options. The
first is to hold the control key and type "c" (usually denoted "^c" in documentation). This generally
completely shuts down a job. The next option is to type "Control z". This suspends execution of the job.
You can then completely eliminate it with the "kill" command. A third option is to use another window or
login session to find the offending program with the "ps" command (generally you would use the -u option
followed by your user ID). Once its job number is known, you can wipe it out with the "kill" command.
Check man and/or Web tutorials for details on use of "ps" and "kill".
Suspending a program with "^z", also opens the option of continuing execution of the job in the
"background" while you go on to run other commands. Right after typing "^z", type "bg", but be please
note that if the program is scheduled to send output to your screen, this will continue, making whatever
else you are trying to do confusing at best. You can also make a decision to run a program in the
background when you first execute it. If I have a program called "hydro" that takes a long time to run and
dumps all of its output directly to files, I might choose to type
hydro &
The ampersand at the end of the line sends execution of the job into the background while I am freed to
type other commands. However, if I log off of the computer before "hydro" has finished running, Unix will
normally kill the program prematurely.
Ending Your Interactive Computer Session
Recall that if you are working directly at a Unix Workstation in an Xwindows session you will need to
position the cursor on the screen background, push the right mouse button, and follow the menu chain to
log off of the machine. If you are connected to the Workstation via a telnet session, just type "exit".
Watch for messages that indicate you are really disconnected from the machine. Sometimes you have
forgotten that you are in some other relative of a shell session, and need to type "exit" again to really get
off of the computer.
Review Questions
Test your knowledge with some review questions on this lecture.
Up one level / Home
Maintained by John Mahaffy : jhm@cac.psu.edu