Moving and Copying Files

Copying Files

The cp command takes two parameters:

cp source destination

Where source is the file being copied and destination is the name of the

file into which the source is copied. In the following example, a file

called project is copied to a new file called oldproject:

$cp project oldproject

Using cp as in the example above is dangerous because it might overwrite

an existing file. To avoid this, use the -i option:

$cp -i project oldproject

In this case, cp first checks if a file called oldproject already

exists. If it does, cp will ask you if you want to overwrite the

existing file:

Overwrite oldporject?

If you enter 'y', the original file will be destroyed and a new copy of

project called oldproject will be created.

To copy a file to another directory, supply the target directory as the

second parameter:

$cp -i project team

In this example, cp creates a new file called project under the

directory team.

cp can copy several files at once. When there are more than 2

parameters, the last parameter is the target directory. In the following

example, the user copies the files intro, chapter1 and appendix into the

directory primer:

$cp intro chapter1 appendix primer

Similarly, you can use wildcards to copy multiples files. In the

following example, cp copies all the files in the src directory that

have a .c extension to the srcbackup directory:

$cp src/*.c srcbackup

To copy a directory and its subdirectories, use the -r (r is for

"recursive") option. The -r option ensures that the entire directory

subtree will be copied to the destination, as the following example


$cp -r usr/projects oldproejcts

$ls -F usr


$ls -F oldprojects


Moving Files

The mv command moves a file to a different location or changes its name.

mv takes two parameters:

mv originalname target

When both parameters refer to the same directory, mv renames the file.

In the following example, the file menu.c is renamed menu.cpp:

$mv menu.c menu.cpp

As with the cp command, use the -i option to avoid accidental file


To move a file from the current directory to another, use the

destination directory as the second parameter:

$mv menu.h headers

If you want to move and rename the file, provide a new name after the

destination directory:

$mv driver.h headers/test.h

To move all the files of a given directory to another directory, use


$mv *.* newsrc

ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon