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(s)
mv
oldFile
newFile
Warning! Pay heed before you proceed!
To clobber a file means to unlink it from your file system. When you clobber a file it is lost and there is virtually no chance you will recover its contents. There is no undelete facility as you might find on other computing systems you have used.
If you remove a file it is clobbered, and there is no way to get it
back without an infinitude of horrid hassle. If you copy or rename onto
an existing file, that file is clobbered, and it is gone forever. Always
check to see if the file name you are copying or moving to is
unoccupied! When in doubt, do an ls
to look before you
leap. All three of these commands have an option -i
, which
warns you before clobbering a file. Using this is a smart
precaution.
The cp
command copies oldFile to
newFile. If newFile does not exist, it creates
newFile; otherwise it will overwrite any existing
newFile.
Try this at your UNIX prompt.
cp .bash_profile quack
Notice that the command cp
has two arguments: the donor
file and the recipient file. If you executed the last command
successfully, you made a copy of your .bash_profile
file to
a file called quack
.
Next, let’s get rid of all the animals in the zoo we had created
before. The command rm
will accept one or more arguments
and remove the named files. We can accomplish this in one blow with
unix> rm aardvark buffalo cougar dingo elephant
Now enter
unix> 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.
The file shown here has size 191. The size is the number of bytes
contained in the file; yours may be larger or smaller. You will also see
that the menagerie has been sent packing.
-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
the -i
option. Enter this at the UNIX prompt.
unix> rm -i 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
. Be reminded that
the -i
option is also available with cp
and
mv
. You should 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
, proceed as follows.
unix> 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
.