Making a Difference

Commands covered in this section: touch mkdir mv cp rm rmdir

A Word of Caution

The previous section showed you how to move around the filesystem and examine text files. We will now show you how you can change things - the names of files, their location, and how to remove them. You can NOT do any damage to the system through use of the commands covered here, but you could lose your own files by accident if you are not careful. So if you have anything important, keep a copy of it - either somewhere else in your home directory, or (even better) on a secondary storage device, such as a floppy disk. Remember: there is NO undelete command under UNIX, although the system administrator should make regular backups, so it may be possible to retrieve lost files from these.

Creating a File

You may already have some files in your home directory to play around with, but don't worry if you haven't. The easiest way to create some is with the touch command. Make sure you are in your home directory (type cd to get there, and then pwd to check). Now type touch rents which will create an empty file called rents in the current directory. Use ls to verify its existence, then use the -l option to see that its size is 0 bytes. Look at the time and date column as well - this shows when the file was last modified, or in this case created. The touch command has another use, when given a filename; it changes the modification time to the current time. Do touch rents again, then ls -l or ls -l rents to see the new modification time.

There are obviously many ways to create files, such as with text editors. These will be covered in a later section.

Creating a Directory

Directories are very useful for collecting related files together. The typical UNIX system contains many thousands of files. Imagine the mess if every one was contained in the same directory. You can too can use directories to organize your files. The mkdir command is used to create new directories. Create one now, with mkdir leith and use ls to see it.

Moving and Renaming

This is all achieved with the mv (short for move) command, which operates differently, depending on the arguments given to it. Many of these operations are illustrated in the examples below, which can be carried out in order using the files and directories created earlier. As before, use ls to examine the effects of each command.

Rename a file:
mv rents forry

Rename a directory:
mv leith hibs

Move a file into a directory:
mv forry hibs

Move it back into the current directory:
mv hibs/forry .

Making a Copy

The copy command is cp. Notice how most of the command UNIX commands are only two letters, to save some typing. Later you'll see that there are many other ways in which the amount of typing you have to do can be reduced.

The copy command has two modes of operation; it can make a copy of a file under a new name, and it can make copies of a list of files and place the copies in a given directory. To illustrate this, we'll need to create some more files, using touch again.

Type this:
touch tommy franco matty

This creates three new files, all in one go. You don't need to use three separate touch commands. This sort of thing can be done with most UNIX commands, and is another example of making things quicker to do.

Now we can make a copy of one of the files:
cp franco begbie

Let's make a copy of two of the files and place them in the hibs directory:
cp tommy matty hibs

Use ls to verify that the files you have created are arranged like this:

directory structure

Removing things

The remove, or delete, command is rm. Related to this is the rmdir command for deleting directories, but only if they are empty.

We'll finish this session by removing all the files we created. It is important to know how to clean up your directories by removing unwanted files, otherwise they will become cluttered and you won't be able to find things easily. Also, your home directory may be limited in the number and total size of files it can hold.

Remove two files:
rm forry tommy

Attempt to remove the hibs directory:
rmdir hibs

You'll get a Directory not empty error. You could delete all the files in it with rm, and then use rmdir, or you can use the -r option to rm, which recursively deletes all files and sub-directories in the given directory, and then the directory itself. Be careful with this command.

Delete the hibs directory and its contents:
rm -r hibs

Finally, remove the remaining files:
rm matty begbie


< Previous: Exploration ^ Next: Editing and Printing Files >
Index

Matt Chapman
Last modified: Tue Aug 26 21:25:17 BST 1997