Academic Computing Services * Simon Fraser University

HOW TO

Create, Copy, Rename and Remove Unix Files and Directories

© August 2, 1996 B-4



What are files and directories?

A file is a "container" for data. Unix makes no distinction among file types -- a file may contain the text of a document, data for a program, or the program itself.

Directories provide a way to organize files, allowing you to group related files together. Directories may contain files and/or other directories. Unix directories are analogous to Macintosh folders and DOS/Windows directories.

Naming files and directories

Each file and directory has a name. Within a directory, each item (i.e., each file or directory) must have a unique name, but items with the same name may exist in more than one directory. A directory may have the same name as one of the items it contains.

File and directory names may be up to 256 characters long. Names may use almost any character except a space. You can divide a multi-word file name using either an underscore or a period, e.g.

chapter_one      chapter.two

Some characters have special meanings to Unix. It is best to avoid using these characters in file names:

/ \ " ' * | ! ? ~ $ < >

Unix is case-sensitive. Each of these is a unique file:

myfile     Myfile     myFile     MYFILE

Creating a file

Many people create files using a text editor (see the E-series of how-tos for information on text editors), but you can use the command cat to create files without learning a text editor. To create a practice file and enter one line in it, type the following at the % prompt:

cat > firstfile (press RETURN)
This is just a test.
(press RETURN)

Stop the file entry by typing Control-d on a line by itself. (Hold down the Control key and type d).

On your screen you will see:

1: jsmythe_fraser% cat > firstfile
This is just a test.
^D

To examine the contents of a file, see how-to B-7, Examine Unix Files and Redirect Output.

You can also use the command touch to create empty files. To create an empty file called test, type

touch test

Copying a file

To make a duplicate copy of a file, use the command cp. For example, to create an exact copy of the file called firstfile, you would type

cp firstfile secondfile

The result is two files with different names, each containing the same information. The cp command works by overwriting information. If you create a different file called thirdfile and then type the following command

cp thirdfile firstfile

you will find that the original contents of firstfile are gone, replaced by the contents of thirdfile. (For information on ways to avoid this, see how-to B-14, Customize Your Unix Environment.)

Renaming a file

Unix does not have a command specifically for renaming files. Instead, the mv command is used both to change the name of a file or to move a file into a different directory.

To change the name of a file, use the following command

mv thirdfile file3

The result of this command is that there is no longer a file called thirdfile, but a new file called file3 contains what was previously in thirdfile.

Like cp, the mv command also overwrites existing files. For example, if you have two files, fourthfile and secondfile, and you type the command

mv fourthfile secondfile

mv will remove the original contents of secondfile and replace them with the contents of fourthfile. The effect is that fourthfile is renamed secondfile, but in the process secondfile is deleted.

Removing a file

The rm command is used to remove a file. For example

rm file3

deletes file3 and its contents. You may remove more than one file at a time by giving a list of files to be deleted, e.g.

rm firstfile secondfile

The rm command removes files silently and permanently. If you wish to be prompted when removing files, use the -i option with the rm command. Using the -i option with the previous example would produce

rm -i firstfile secondfile
File firstfile. Remove ? (yes/no)[no]

Type y or yes to remove the file, type n or no or press Return to leave it. The next file in the list will then be displayed:

File secondfile. Remove ? (yes/no)[no]

Type y or yes to remove the file, type n or no or press Return to leave it. How-to B-14, Customize Your Unix Environment, explains how to turn on the -i option automatically.

Creating a directory

Creating directories permits you to organize your files. The command

mkdir project1

creates a directory called project1, where you might store files related to a particular project. The directory that you create will be a subdirectory within your current directory. For details on how to move around in directories and how to show the files and directories they contain, see how-to B-5, List Contents and Navigate Unix Directories.

Moving & copying files into a directory

The mv and cp commands can be used to put files into a directory. Assume that we want to put some files from our current directory into the newly-created directory called project1. The command

mv bibliography project1

will move the file bibliography into the directory project1. The command

cp chapter1 project1

will put a copy of the file chapter1 into the directory project1, but leave chapter1 still in the current directory. There will now be two copies of chapter1, one in the current directory and one in project1.

Renaming a directory

The mv command is also used to rename and to move directories. When you type the command

mv project1 project2

the directory called project1 will be given the new name project2 as long as a directory called project2 did not previously exist. If project2 already existed before the mv command was issued, the result of

mv project1 project2

would be to put the directory project1 and its files into the directory project2.

Copying a directory

The cp command is used to make a duplicate copy of a directory and its contents. To copy directory project1 to a new directory proj1copy, you would type

cp -r project1 proj1copy

If directory proj1copy already exists, this command will put a duplicate copy of directory project1 into directory proj1copy.

Removing a directory

Use the command rmdir to remove an empty directory. Multiple empty directories may be removed by listing them after the command, e.g.

rmdir testdir1 testdir2

If you try to remove a directory which is not empty, you will see

testdir3: Directory not empty

If you are sure that you want to remove the directory and all the files it contains, use the command

rm -r testdir3

Summary of Commands

Working with files
touch file1 Creates an empty file called file1
mv file1 file2 Renames file1 to file2 (overwrites original contents of file2, if file2 existed previously)
cp file1 file2 Copies file1 as file2 (overwrites original contents of file2, if file2 existed previously)
rm file3 Removes file3, with no request for confirmation
rm -i file3 file4 Removes file3 and file4, requesting confirmation for each removal
Working with directories
mkdir dir1 Creates a new directory called dir1
mv dir1 dir2 If dir2 does not exist, renames dir1 to dir2
If dir2 does exist, moves dir1 inside dir2
cp -r dir1 dir2 If dir2 does not exist, copies dir1 as dir2
If dir2 does exist, copies dir1 inside dir2
rmdir dir1 Removes dir1, if dir1 contains no files
rm -r dir1 Removes dir1 and any files it contains
(use with caution)
Working with files and directories
cp file1 dir1 Copies file file1 into existing directory dir1
mv file2 dir2 Moves file file2 into existing directory dir2


* * * * * * * * * * * * * * *

This page written and maintained by Academic Computing Services, Simon Fraser University.
Please e-mail questions or comments to help@sfu.ca.