Create, Copy, Rename, and Remove Unix Files and Directories
S4148 May 2009
This document lists commands for creating, copying, renaming, and removing Unix files and directories. It assumes you are using Unix on the ITS Login Service (login.itd.umich.edu). The instructions here apply to many other Unix machines; however, you may notice different behavior if you are not using the ITS Login Service. Table of Contents
What Are Unix Files and Directories?A file is a "container" for data. Unix makes no distinction among file typesa 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. Directories are analogous to Macintosh and Windows folders. Naming Unix Files and DirectoriesEach file and directory has a name. Within a directory, each item (that is, 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 (for example, chapter_one or 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, and MYFILE. Creating a FileMany people create files using a text editor, but you can use the command cat to create files without learning a text editor. To create a practice file (named firstfile) and enter one line of text in it, type the following at the % prompt:
cat > firstfile
(Press the Enter/Return key.) This is just a test. (Press the Enter/Return key.) Stop 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:
% cat > firstfile
This is just a test. ^D One way to examine the contents of the file you've just created is to enter this at the % prompt:
cat firstfile
Copying a FileTo 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. Renaming a FileUnix does not have a command specifically for renaming files. Instead, the mv command is used both to change the name of a file and to move a file into a different directory. To change the name of a file, use the following command format (where thirdfile and file3 are sample file names):
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 FileUse the rm command 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. For example,
rm firstfile secondfile
You will be prompted to confirm whether you really want to remove the files:
rm: remove firstfile (y/n)? y
rm: remove secondfile (y/n)? n Type y or yes to remove a file; type n or no to leave it. Creating a DirectoryCreating 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 List Contents and Navigate Unix Directories (S4149). Moving and Copying Files Into a DirectoryThe mv and cp commands can be used to put files into a directory. Assume that you want to put some files from your current directory into a 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 DirectoryYou can also use the mv command 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 directory 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 DirectoryYou can use the cp command to make a duplicate copy of a directory and its contents. To copy directory project1 to directory proj1copy, for example, 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 DirectoryUse the command rmdir to remove an empty directory. Multiple empty directories may be removed by listing them after the command:
rmdir testdir1 testdir2
If you try to remove a directory that is not empty, you will see rmdir: testdir3: Directory not emptyIf you are sure that you want to remove the directory and all the files it contains, use the command
rm -r testdir3
Summary of CommandsWorking With Files
Working With Directories
Working With Files and Directories
Additional ResourcesVisit ITS's Information System to obtain ITS computer documentation and other resources. A list of relevant documents follows:
The ITS Service Center provides a variety of computing help resources. For further help with this or any other topic, call 734-764-HELP [4357] or submit an online service request.
|