Make your own free website on
In Association with

Copying, Moving, Renaming, and Deleting Files

By now, you've learned a little about the structure of the filesystem and you've learned how to create files and directories.

But just because you know how to create files and directories doesn't mean that you have to keep the changes you've made. This section explains how to copy, move, rename, and delete files and directories.

Copying Files

Like so many Linux features, you have a variety of options from which to choose when you want to manipulate files and directories. You can also use wildcards when you're copying, moving, or deleting files and directories.

To copy a file, type the following command:

cp <source> <destination>

So, to copy the file sneakers.txt to the directory tigger in your login directory, move to your login directory and type:

cp sneakers.txt tigger

Notice that you also used relative pathnames to copy the file. You can use both relative and absolute pathnames with cp. Our login directory is the parent of the directory tigger; tigger is one directory down from our login directory.

Read the cp man page (man cp) for a full list of the options available with cp. Among the options you can use with cp are the following:

If you use cp with no options, you won't see much when the command is executed. Using an option, such as -i, can make the process a little more useful. If you want to copy a file to a location that already has a file with the same name, you'll be asked first if you really want to overwrite (or replace) the file that's already there.

Now that you have the file sneakers.txt in the tigger directory, use cp -i to copy the file again to the same location.

[newuser@localhost newuser]$ 
cp -i sneakers.txt tigger
cp: overwrite 'tigger/sneakers.txt'?

To overwrite the file that's already there, press [Y] and then [Enter]. Don't want to overwrite the file? Press [N] and [Enter].

Moving Files

To move files, use the mv command. It is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp. For more about mv, see the mv man page (type man mv).

Common options for mv include the following:

If you want to move a file out of your home directory and into another directory, type the following (you'll need to be in your home directory):

mv sneakers.txt tigger

Alternatively, the same command using absolute pathnames looks like mv sneakers.txt /home/newuser /home/newuser/tigger.

Renaming Files

Actually, we've already covered half of renaming, because when you copy or move files, you can also rename.

To copy the file sneakers.txt from your login directory to the tigger subdirectory, just type:

cp sneakers.txt tigger

To copy and rename that file from sneakers.txt to piglet.txt, type:

cp sneakers.txt tigger/piglet.txt

To move and rename the file, just substitute mv for cp in the above example.

If you cd to tigger and then type ls, you'll see the file piglet.txt.

If you just want to rename the file and keep its location, just mv in your current directory:

mv sneakers.txt piglet.txt

Deleting Files and Directories

You learned about creating files with the touch command and by using redirection in Chapter 10 . And you created the directory tigger using mkdir.

Now you need to learn how to delete files and directories. Deleting files and directories with the rm command is a straightforward process. See the rm man page for more information. Options for removing files and directories include:

To delete the file piglet.txt from the tigger directory with the rm command:

rm piglet.txt

What happens if you didn't really want to get rid of it? Too late! That's where the -i (interactive) option is helpful, because it gives you a second chance to think about whether or not you really want to delete the file.

[newuser@localhost newuser]$ 
rm -i piglet.txt
rm: remove 'piglet.txt'?

You can also delete files using the wildcard *, but be careful, because you can easily delete files you didn't intend to throw away.

To remove a file using a wildcard, you would type:

rm pig*

The above command will remove all files in the directory which start with the letters "pig."

You can also remove more than one file using one command:

rm piglet.txt sneakers.txt

Options for removing files and directories include the following:

You can use rmdir to remove a directory (rmdir foo, for example), but only if the directory is empty. To remove directories with rm, you must specify the -r option.

For example, if you want to recursively remove the directory tigger you would type:

rm -r tigger

If you want to combine options, such as forcing a recursive deletion, you can type:

rm -rf tigger


Be Careful When Using rm!


The rm command can delete your entire filesystem! If you're logged in as root and you type the simple command rm -rf /, you're in trouble; this command will recursively remove everything on your system.

A safer alternative to using rm for removing directories is the rmdir command. With this command, you won't be allowed to use recursive deletions, so a directory which has files in it won't be deleted.

Read the rmdir man page (man rmdir) to find out more about this command.

Book Site Map ElectronicVideo International Sites :

USA, United Kingdom, Germany, France