UNIX Commands
If you have not already done so, log into your UNIX account. You will see the greeting message and then your UNIX prompt. My prompt looks like this
morrison@cs ~]$
A prompt is so-named because it is asking you to enter a command. Before we do this, let's cover some basic facts. Every UNIX command has the following anatomy
commandName -option(s) argument(s)
I will assume you have used a Mac or a DSM before; all of the familiar features of a graphics-based machine are here, you will just invoke them with a text command instead of a mouse click. We will go through some examples so you get familiar with all the parts of a UNIX command.
Let us first use the UNIX command touch
. This command
creates a file. At your UNIX prompt, enter
$ touch stuff
This creates an empty file named stuff
in your account. Let us
talk about the anatomy of this command. The name of the command is
touch
; its purpose is to create an empty file. Since you do not see a
- sign, there are no options being used. The argument is stuff
. This
is the name of the file you created. Create a few more empty files. Enter
these commands
$ touch foo $ touch bar
Now you have three files in your account. Now we will see how to list the files
Enter this command at the UNIX prompt
$ ls
The command ls
lists your files. Notice we had neither
options nor arguments. If you created the files using touch
as instructed, they should appear on your screen like this
$ ls bar foo stuff
The command ls
has several options. One option is the
l
option; it list the files in long format. To invoke it,
type
$ ls -l
You will see a listing like this
$ ls -l -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 bar -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:49 foo -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:49 stuff
The first column reflects the permissions for the files. The sequence
-rw-rw-r--
indicates that you and your group have read/write permission and that others ("the world") has read permission. We will discuss permissions in more detail on the directories page.
You can see your name is listed in two columns. The zero indicates
the size of the file; it is empty. Then there is a date, a time and
the file name. This is the long format for file listing; it is seen by
using the -l
option in the ls
command.
Another option is the -a
option. This lists all files,
including "hidden" files. Hidden files have a dot (.) preceding their
name. To see them, enter
$ ls -a
at the UNIX command line. You will see the files you saw using
ls
and several hidden files with mysterious names like
.bash_profile
. Do not delete these; they provide the
configuration for your account. You can use more than one option at
once. For example, entering
$ ls -al
shows all of your files and hidden files in long format. Try this now.
Now we will show how to display a file to the screen. Let us look
inside your .bash_profile
file. Enter the command
$ cat .bash_profile
The command name is cat
, short for catalog (the file to the
screen). We are using no options, but the filename is an argument to
cat
. If a file is long and you want to see it one screenful
at a time, use the command more
. The command more
takes a filename as an argument and shows it one screenful at a time.
You can hit the spacebar to see the next screenful or hit the enter
key to scroll down one line at a time. To exit more
at any
time, type a q
and more
quits.
Copying, Renaming and Deleting Files
Three commands every beginner should know are: cp
,
rm
and mv
. These are, respectively, copy, remove and
move(rename). Here are their usages
cp <oldFile> <newFile> rm <garbageFile> mv <oldFile> <newFile>
The first command copies oldFile
to newFile
. If
newFile
does not exist, it creates newFile
;
otherwise it will overwrite newFile
.
Try this at your unix prompt.
$ cp .bash_profile quack
Notice that the command cp
has two arguments: the source file and
the recipient file. If you executed the last command successfully, you make a
copy of your .bash_profile
file to a file called quack
.
Now enter ls -l
You will see that quack's size is nonzero because it has a copy of the contents of your .bash_profile file in it. Mine has size 191.
$ ls -l -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 bar -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:49 foo -rw-r--r-- 1 morrison morrison 191 Jun 9 11:25 quack -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:49 stuff
Let us now remove the file stuff
. We are going to use an
option, however. Enter this: rm -i stuff
. This command has
an option (see the - sign??) and the argument stuff
. The
system will then ask you if you are sure you want to remove the file.
Tell it yes by typing the letter y
.
The -i
option is also available with cp. I recommend you use
it to avoid costly mistakes.
Finally, we shall use mv
This "moves" a file to have a new
name. Let's change the name of quack
to honk
and
back again. To change quack
to honk
, enter
mv quack honk
. Once you do this, list the files in long
format. Then change it back.
Now you know how to copy, move, and create files. You can show them to
the screen and you can list all the files you have. So far, we can
create files two ways, we can create an empty file with touch
or copy an existing file to a new file with cp
Your account has been configured to automatically do this whenever
you use the rm
, mv
or cp
commands.
Next you will want to know how to edit files.