
Working
with GPU Files and Directories
Marianne Aldridge (marianne.aldridge@ualberta.ca)
CNS Help Desk (492-9400)
Unix Command Syntax
The Unix command format specifies that the command -- the action you
want to perform -- must be the first item typed on the command line. In some cases the
command by itself is all that is required to perform a certain task, but in many other
cases we want to use conditions (operands) to customize the command.
If you want to specify a command operand, type in the command, then hit the spacebar to
put a space between the command and whatever else is to follow. Next type a hyphen (-),
which tells Unix that there are command operands coming. Then type the operand(s) you want
to use and press Enter or Return. For example, to list all files in a directory, including
hidden files, you would type the ls command and the a
operand, then press Enter or Return:
ls -a
It is important to remember that Unix commands and operands are case-sensitive. If you
don't type the commands or operands in the correct case, you'll get error messages like
"command not found."
Directory Navigation
The files you store in your GPU account are organized in a
hierarchical tree system of directories and subdirectories. Your "home"
directory is where all the files and directories in your account space are stored. Some
programs like Pine (electronic mail) and Tin (newsreader) create their own directories in
which they store files. Keeping files organized and grouped into directories and
subdirectories makes it much easier to find them later when you want to use them again.
To see where you currently are in your account's directory structure, use the
"print working directory" command, as shown below, and then press Enter or
Return:
pwd
When you first log in to your GPU account, by default your working directory is your home
directory. In the University of Alberta environment, a pwd command should return something
like this for a home directory path:
/afs/ualberta.ca/home/j/d/jdoe
If your GPU computing account (i.e., your CNS Computing ID) appears at the end of the
string returned by pwd, you are in your home directory.
To change your current working directory, use the cd command followed
by the name or pathname of the directory you want to change to, as in:
cd directory-name
For instance, to change to a subdirectory named Mail, you would use the command:
cd Mail
To move back one directory level, use the command:
cd ..
To return to your home directory from anywhere on the GPU system, type the cd command
without operands:
cd
Listing Files and
Directories
To list the names of directories, files and other file information on
your screen, use the list commands and operands shown below.
To see a simple list of the files in your current working directory, enter:
ls
To see a list of all files, including hidden files, in your current working directory,
type:
ls -a
To see a list of all files in the current working directory, including detailed file
information (permissions, owner, time and date stamps, etc.), enter:
ls -la
Note the following command, which is useful for displaying long lists more than one screen
long:
ls la | more
This will cause your command output to be displayed one screen at a time. Hitting the
spacebar will advance the display.
Renaming and Copying Files
In Unix, renaming a file is accomplished with the move command (mv),
which moves the file contents from one filename to another. The old filename ceases to
exist, and the file contents are found in the new filename. Here's an example:
mv old-filename new-filename
When you copy a file in Unix, the two files will have identical contents, but different
names. Then you can modify the contents of the new file, without disturbing the contents
of the original. To make a copy of a file, use:
cp filename1 filename2
Removing Files and
Directories
To remove a file, use the remove command (rm), as in:
rm filename
When typing filenames, it's important to remember that you must type the filename exactly
as it appears in the file list. If there are spaces or special characters in the name of
the file that you want to remove, you will need to enclose the filename within double
quotation marks. For instance, suppose you have a file named Everything I Need.
To remove this file, the command would be:
rm "Everything I Need"
Sometimes you might want to remove an entire directory. To remove an empty directory, use
the remove directory (rmdir) command, as in:
rmdir directory-name
If there are files in a directory that you want to remove, you must first change to the
directory where the files reside. You can then remove all files in the directory one at a
time using the rm command or you can remove the files all at once using the rm command and
an asterisk wildcard character (more on wildcards below), as in:
rm *
Both of the rm commands above will stop and prompt you for each filename, asking if you
really want to delete that file. You'll have to type y to respond yes to
each query, so if there are a large number of files in the directory, removing them can be
tedious and time-consuming. There's a much easier way to remove all the files in a
directory, but it's also much more dangerous. The following command will remove not only
the directory you specify, but also all files and subdirectories in that directory:
rm -rf directory-name
To use the command above, you must be one directory level above the directory that you
want to delete -- that is, you must be able to see the directory name in the file list
when you do a list command. Before using this command, be certain that you know exactly
what directory you are in, and make sure that you can see the name of the directory you
want to remove in the file list.
Warning: Never use wildcards with the rm -rf command!
Wildcard Characters
Unix allows the use of wildcard characters in its command strings. You
can use a wildcard to represent a single character or a string of characters.
The most commonly used wildcard character is the asterisk (*). For
example, suppose you use the ls command to list the files in your current working
directory, and get a list like the following:
pic1.gif pic5.gif
pic2.gif puppy.gif
pic3.gif hill.jpg
pic4.gif dart.gif
Now suppose you want to delete all the files that begin with "pic." You can
either use the rm (remove) command multiple times to remove the files one at a time, or
you can use the rm command once by including an asterisk wildcard character so that the
command is applied to all of the files at the same time, as in:
rm pic*.gif
The above command will remove all five of the files you want to delete, and leave the rest
of your directory intact.
Note that the command below will remove the files also, but will do it by removing all
files in the directory which have the suffix .gif. Since you have other files with that
suffix, this probably isn't something you want to do:
rm *.gif
Whenever you use wildcards in Unix commands, it's a good idea to specify as much of the
filename as possible, to better tailor the number of files the command will be applied to.
Warning: Never use the command rm *.* unless your
intent is to remove every file in the current working directory!
Viewing File Contents
The Unix more command will display the contents of
the specified filename, as in:
more filename
Now press Enter or Return to advance the display of the file by one line. Press the
Spacebar to advance the display by one screen at a time. Press the forward slash (/)
followed by a pattern to search for a matching pattern. Press b to
display the previous screen. Press h to display a help screen and press q
to quit the display.
Another neat trick to know about is Tab completion of filenames. Most Unix systems
support this feature, and it's really helpful when you have to type long filenames or
filenames with spaces or special characters. To use this feature with the more command,
type part of the filename and then press the Tab key to complete the filename. For
example, suppose you want to list the contents of a file named This is a test.
There are spaces in the filename, so you know you must enclose the filename in double
quotes. Type:
more "Th
Press the Tab key, and the following will display:
more "This is a test"
To display the contents of This is a test, just hit Enter or Return.
One function of the Unix cat command is to display the contents of a
file. To show the file contents one screen at a time, use the cat command with the
| more suffix, as in:
cat filename | more
Now press the Spacebar to advance the display by one screen at a time. Press Enter or
Return to advance the display of the file by one line at a time. Press b
to display the previous screen and press q to quit the display.
By default, the Unix head command displays the first 10 lines of the
specified file(s), as in:
head filename filename2
The head command can also display a different number of initial lines of a file (such as
25 lines), as in:
head 25 filename filename2
By default, the Unix tail command displays the last 10 lines of the
specified file(s), as in:
tail filename filename2
The tail command can also display a different number of ending lines of a file (such as 25
lines), as in:
tail 25 filename filename2
The Unix page (pg) command displays file contents one
screen at a time, and allows you to go forward and backwards in the file. Here's an
example:
pg filename filename2
Now, at the colon prompt (:), use any of the following:
- Press Enter or Return to display the next screen.
- Type +n and press Enter or Return to move forward n screens.
- Type n and press Enter or Return to move backward n screens.
- Type h and press Enter or Return to display a help screen.
Editing a File
The following command opens the specified file in a Pico editor
window:
pico filename
Pico commands are executed by holding down the CTRL key and pressing the letter key
associated with the function you want to perform. The table below shows a number of Pico's
keystroke combination commands.
| CTRL-f |
moves the cursor forward a character |
| CTRL-b |
moves the cursor backward a character |
| CTRL-p |
moves the cursor to the previous line |
| CTRL-n |
moves the cursor to the next line |
| CTRL-a |
moves the cursor to the beginning of the current line |
| CTRL-e |
moves the cursor to the end of the current line |
| CTRL-v |
moves the cursor forward one page |
| CTRL-y |
moves the cursor backward one page |
| CTRL-w |
searches for a string of text |
| CTRL-l |
refreshes the screen display |
| CTRL-d |
deletes the character at the cursor position |
| CTRL-k |
cuts selected text |
| CTRL-u |
pastes the text, which was last cut, at the cursor position |
| CTRL-i |
inserts a tab at the current cursor position |
| CTRL-j |
justifies the current paragraph |
| CTRL-t |
invokes the spelling checker |
| CTRL-r |
inserts an external file at the cursor position |
| CTRL-o |
saves the current file |
| CTRL-x |
exits the Pico editor |
Command History File
On GPU, a hidden file named .history is automatically
created and stored in your home directory. Its function is to hold your command history --
the last 40 commands you entered at the GPU system prompt. The contents of the .history
file are saved from one GPU session to the next.
If you know that a command you want to use is in your command history file, you can
save yourself typing it out (useful for long commands). Just go to the GPU Unix prompt and
use your up and down arrow keys to navigate through the commands in the .history file.
Each time you press an arrow key another command from the history list will appear at the
GPU prompt. When you see the command you want, simply press Enter or Return to execute it.
You can also edit a command retrieved from the history list before executing it.
Online Help with Unix
Commands
All Unix systems have online manual pages, known to Unix geeks as
"man pages." To find the associated man page information for any given command,
use the man command as shown below:
man command-name
|