Main index

Introducing UNIX and Linux


Files

Overview
The UNIX directory hierarchy
Filesystems
Manipulating files
      Creating directories
      Creating files
      links
      'Dot' files
Protecting files
      Groups
      File access control
      Changing privileges
File contents
      Text files
      Comparing files
      Filtering files
      Non-text files
Printing files
File archives and file compression
Other relevant commands
Summary
Exercises

Creating files

The first thing that must happen to a file before anything else is that it must come into existence. This may happen by design of another command (when you create it with an editor, for instance) or as a side-effect. You will frequently find that the current directory contains files you don't remember anything about! When the data held in a file is no longer required, you may wish to delete the file. Just as for file creation, this may be explicit or implicit (some automatically created files may disappear spontaneously). Suppose you have created a file called myfile. You can create another file with a copy of the contents of myfile using the command cp ('copy'). If you decide that the name of a file needs to be changed, the command mv ('move') will do exactly that. For instance:

ls
myfile
cp myfile foo
ls
foo myfile
mv myfile bar
ls
bar foo

When the command cp is called, a completely new file is created, with a new inode. The directory in which the new file is to be located is amended with the information about the new filename and the new inode. The data in the file being copied is not changed at all.

Sometimes it is useful to give a file several different names. This can often happen if you have data that needs to be accessed in several different directories, and any changes to it need to be made consistently. If you created several copies of the file, you would have to perform updates several times, once on each copy. This is inefficient, and errors might creep in. Also, storage space would be wasted - and if the files concerned are large data files this might well be an important consideration. We can use the concept of an inode to good effect here. Since a directory associates names with their storage locations via inodes, there is no reason why a particular file should not have several names, perhaps in different directories.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck