

In the previous segment, you learned how data can be stored in files, and how to list the files that you have access to. In this segment we will learn some more basic file-handling skills.
The filenames we used in the previous section were very simple: 'diary' and 'expenses'. Before we move on, let's just see what a filename can contain.
A filename can contain most keyboard characters. However, there are one or two restrictions, and a few things that should be avoided to make life easier:
As a simple rule, it is best to use a mixture of letters and numbers, with spaces replaced by hyphens ( - ).
Also, the full-stop ( . ) is often used, and the part of the filename after the full stop (sometimes called the extension) is sometimes used to indicate the type of data. For example, a text file may have an extension of .text or .txt - however, don't worry too much about extensions for now. Look at the following, which are 'good' filenames, which don't use any illegal or difficult characters:
letter-to-John
report-14thMarch-2001.text
Website.html
Contacts
Note that filenames are case sensitive. This means that the upper case and lower case versions of any letter are considered to be different. So, you could have two files, one named 'contacts', and another named 'Contacts'. As far as the system is concerned, they are two different files. Of course, you should usually try not to have two files with such similar names, to avoid confusion!
Sometimes, it is useful to change the name of a file. For instance, if you have a file named 'diary', which was your schedule for last week, you may now want to rename it to 'old-diary' or 'diary-09-April-2001'.
Any file can be renamed by using the command 'mv' (short for 'move'). This command takes two parameters: the old (current) name of the file you wish to move, and its new name. For example:
$ mv diary old-diary
Exercise: Use the command 'ls', and make sure you can see your file 'diary' from the previous segment. Then use the command 'mv' to change the filename to 'old-diary' (without the apostrophes!). Then use the command 'ls' again, to make sure that the name has changed as expected. Use the 'cat' command to look at the file with the new name: The contents of the file should be completely un-changed, as the filename is just a label for the data - the data itself does not change. Then use the 'mv' command to rename the file back to its original name: 'diary'. Use 'ls' once more to make sure the file is back to its original name.
You have just renamed a file to a new name. You saw how the data itself stayed the same, only the filename changed. You then renamed the file again, to get it back to its original name.
When we renamed the file in the previous section, there was still only one copy of the file. Its name changed twice, but the data stayed the same. In this section, we'll find out how to take one file, and make a copy of it, which will be stored under a different filename.
The command to do this is 'cp' (short for 'copy'). This takes two parameters: The name of the file to copy, and the name of the new copy of the file. For example:
$ cp diary new-diary
This command would take the file 'diary', and create a new copy of the data in the file, this time stored with the name 'new-diary'.
Exercise: Use the command shown above to copy the file 'diary' to a new file named 'new-diary'. Use the 'ls' command to list the files in your directory. You should see the file 'new-diary' and the original file, 'diary'.
The difference between 'mv' and 'cp' is that 'mv' just renamed an existing file. In the end, there was still only one file, with a different name. In the case of 'cp', a new copy of the file was created, so that after the command, there are two files. The new file is an exact copy of the old file.
Exercise: Using vi, make some changes to the file 'new-diary'. Then save the file, and exit vi. Use the 'cat' command to view first the file 'new-diary', then the file 'diary'.
Note how you can now make changes to one file independantly of the other. In other words, although the 'cp' command created an exact copy of the original, the new file is then a separate file like any other.
As an exercise, create an electronic diary, that you can use to store your schedule, one week at a time. This will use many of the skills you have learned so far, and will let you see how you already know enough to use UNIX to help with every day tasks. Follow these steps:
1. Use vi to create a file named 'weekly-diary-template'. The file should look something like this:
My Schedule for Week Commencing: Day AM PM Evening --------------------------------------------------------------------------------- Mon Tue Wed Thur Fri Sat Sun
2. Use the 'cp' command to make a new copy of the file, which should be named after the date of the Monday in the current week, for example '09-April-2001'. The command may be something like:
$ cp weekly-diary-template diary-09-April-2001
You should now have two files: the template, and the new copy of the template, which is named after the current week.
3. Use vi to fill in your schedule. Only make changes to the version of the file including the date. Leave the template as it is, ready to create next week's diary when you need it.
4. Whenever you need to start a new week's diary, use a similar 'cp' command. Again, copy the template file to a new file containing the date of the Monday in that week. For example, the following week, you may use:
$ cp weekly-diary-template diary-16-April-2001
Start using the electronic diary on a regular basis. This will give you good practice in vi, and basic file management commands.
We are developing the next part of the tutorial - please bear with us.
If you would like to be notified when it is ready, please simply email us
and say so!
If this site has helped you, please consider sending us a small donation if you can afford it - thanks!