10.5 Copy and Move

10.5.1 cp

cp(1) copies files. DOS users will notice its similarity to the copy command. There are many options for cp , so you should have a look at the man page before using it.

A common use is to use cp to copy a file from one location to another. For example:

% cp hejaz /tmp

This copies the hejaz file from the current directory to the /tmp directory.

Many users prefer to keep the timestamps preserved, as in this example:

% cp -a hejaz /tmp

This ensures that the timestamps are not modified in the copy.

To recursively copy the contents of a directory to another directory, you would issue this command:

% cp -R mydir /tmp

That will copy the mydir directory to the /tmp directory.

Also if you wish to copy a directory or a file and keep all it's old permissions and time stamps and keep it exactly the same use cp -p.

% ls -l file
-rw-r--r--    1 root     vlad            4 Jan  1 15:27 file
% cp -p file /tmp
% ls -l /tmp/file
-rw-r--r--    1 root     vlad            4 Jan  1 15:27 file

cp has many more options that are discussed in detail in the online manual page.

10.5.2 mv

mv(1) moves files from one place to another. Sounds simple enough doesn't it?

% mv oldfile /tmp/newfile

mv has a few useful command line options that are detailed in the man page. In practice, mv is almost never used with commandline options.