A free Basic UNIX-training with muLinux
To save this exercise on a floppy disk, you insert a disk into the drive,
mount the drive on /a and change to that directory.
/# mount /dev/fd0 /a
/# cd /a
/a#
Copy, Move, Rename, Link
Copying files and directories
The command to copy files and directories is called cp
(copy) and can be used in many ways.
Copying a file
The command cp sourcefile targetfile will produce a back up copy of the source file. If you are going to make changes to a configuration file in /etc it is wise to make a copy of the original first in case of an error. You can then copy the original to restore things to a known good operating condition.
/# cd /etc
/etc# cp passwd passwd.original
In this example you have cloned the file passwd.
In case you made a mistake in editing passwd, you just need to
delete passwd and replace it with passwd.original
by copying the original file.
The copied file will have the same permissions and retains the last
change date. The user who wants to copy a file must have permission to read
the file and needs write permission in the directory where the
file will be created.
The user will immediately be "owner" of the new file.
If the "old" file wasn't his file he requires permission there too.
/etc# ls -l passwd*
1 -rw-r--r-- 1 craxi psi 708 Feb 27 1999 passwd
1 -rw-r--r-- 1 root root 708 Oct 31 13:18 passwd.original
You can also copy a file to the floppy.
For this the floppy needs to be mounted on /a.
/etc# cp passwd /a
Copying more files
With the cp command you can copy several files at once, by giving a directory as the target.
/etc# cp passwd passwd.original /a
The original file /a/passwd is overwritten.
You can test this with following command. Let's have a look at the
change date.
/etc# ls -l /a
If you want to be asked for confirmation, you need to add the option
-i (interactive).
/etc# cp -i passwd passwd.original /a
cp: overwrite `/a/passwd`? y
cp: overwrite `/a/passwd.original`? y
/etc#
Using the wildcard symbol * several files can be copied.
/etc# cp i* /a
Now, all file names starting with an i, are copied to the floppy.
Copy directories with contents
By using the option -r (recursively) you can copy whole
directories with files and subdirectories.
/etc# cp -r /etc/ppp /a
The option -r you also used with the command rm.
Now you will use it to remove the contents of the floppy.
/etc# rm -r /a/*
Move and rename files
Moving is the same as Copying, with the difference that the original file
is being deleted.
The command is called mv (move).
/etc# mv passwd /a
This puts the file passwd on the floppy, and removes it from the
directory /etc which would have fatal consequences in a
"real" Unix system. That's why you must move the file back.
Renaming files
To rename files you also use the command mv.
You might want to use this command to change the command ls into
dir.
Thus a DOS-user can use his familiar dir command to display the contents of a directory. This is not very practical and offends Unix guru's, but it's a good example of the rename procedure.
/etc# cd /bin
/bin# mv ls dir
/bin# ls
/bin/ls: not found
/bin# dir
directory-contents
Please rename the file dir back to ls.
You can also copy this file, to use it as dir,
but there is a more elegant way.
Links - More names for one file
You can give more names to a file.
The new file name appears as a file in the directory, but doesn't
occupy additional drivespace. The command for this is ln (link).
/bin# ln ls dir
Now the command ls works just like dir.
Another use of this command would be to use the floppy as our
home directory
/home/Robert.
Like this you can work in your directory while at the same time everything is
saved on the floppy disk. Please use your own name for your home directory.
/bin# ln /a /home/Robert
Please use "ln -s" to link directories.
/bin#
What now? It seems that by linking directories you need to add the option
-s (softlink):
/bin# ln -s /a /home/Robert
/bin# ls /home/Robert
contents of the floppy
/bin#
When you enter into the directory
/home/Robert you are in the
directory /a.
/bin# cd /a /home/Robert
/a#
To finish this exercise, unmount the floppy.
/a# cd /
/# umount /a
/#
Robert.Warnke@giso.de (copyleft) Robert Warnke, Berlin (Germany) - You can write me in English. | http://rowa.giso.de | translated by by mek, corrected by Bob Goodwin
|