# Using files and directories

## Creating files

touch filename

If the filename doesn’t exist it gets created (0 Bytes).
If the filename already exists, touch alters its timestamp to the current time.

In UNIX, filenames should NOT contain spaces, and so underscores or a capital letter should be used instead to separate words. E.g. % touch my file will not work, therefore you will need to type in either of the following: % touch myFile or %touch my_file. This also applies to whenever directories are created.

Instructions on creating directories can be found by visiting the following link:
Navigating the Unix file system > Making Directories

## Copying files

cp (copy)

cp file1 file2 is the command that makes a copy of file1 in the current working directory and calls it file2.

What needs to be done next is to take a file stored in your home area, and use the cp command to copy it to your unixstuff directory.
This directory was created in ‘Making Directories’. Please view this section by visiting the following link if you haven’t already done so:
Navigating the Unix file system > Making Directories

1. Add cd to your unixstuff directory:
% cd ~/unixstuff
2. At the UNIX prompt type:
% cp ~/myFile.txt .
Don’t forget to include the full stop ‘.’ at the end. Remember, in UNIX, the full stop means the current directory.
The above command means copy the file myFile.txt to the current directory, keeping the name the same.

## Moving files

mv (move)

mv file1 file2 moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.

It can also be used to rename a file, by ‘moving’ the file to the same directory, but giving it a different name.

What needs to be done next is to move the file myFile.bak to your backup directory (created in ‘cd’, which can be found by visting the link below):
Navigating the Unix file system > Changing directories

1. Change the directories to your unixstuff directory.
2. Inside the unixstuff directory, type the following:
% mv myFile.bak backups/.
3. Type ls and ls backups to see if it has worked.

## Removing files and directories

rm (remove)
rmdir (remove directory)

To delete (remove) a file, use the rm command. As an example, the following code shows how to create a copy of the myFile.txt file then delete it.

Inside your unixstuff directory, type the following:
% cp myFile.txt tempfile.txt
% ls (to check if it has created the file)
% rm tempfile.txt
% ls (to check if it has deleted the file)

You can use the rmdir command to remove a directory (make sure it is empty first). However, if you try to remove the backups directory, you will notice that this is not possible because Unix will not let you remove a non-empty directory.

## Removing a directory that contains files

To remove a directory which contains files you must use the regular rm command and not the predefined alias rm -i

In order to do this you will need to start the command with a backslash

\

The following code will remove directory without prompting to delete each file within it.

\rm -rf directory

WARNING: Never use wildcard matches i.e. * ? . (asterisks, question marks or full stops) and always check twice before hitting the enter or return key.

## Summary

cp file1 file2 – copy file1 and call it file2

mv file1 file2 – move or rename file1 to file2

rm file – remove a file

rmdir directory – remove a directory

\rm -rf directory – removes a directory that contains files