cp PDF Print E-mail

cp

The copy command for Linux is cp. This command is a heavily used command that is simple but allows you to use options that created a very powerful tool.

 

Example:

cp /home/fred/text /home/jim/

This command will copy the file text to Jim's home directory.

 

options

-a retain archival attributes

-b create backup of file instead of overwriting

-d maintain symbolic links

-f force copy

-i interactive mode, prompts before files are overwritten

-l creates links between files copied to directories instead of actually copying the file

-p preserve existing permissions

-r copy entire directory and subdirectories

-R copy entire directory and subdirectories

-s create symbolic links between file copied to directories instead of actual copy

-S set a suffix to all new files, default is ~

-u does not copy to newer files

-v verbose mode

-V version-control numbering

-x ignores subdirectories on remote file systems

 

An often used option with cp is the -R or -r option that allows you to copy not only directories but all of the sub-directories that are contained inside a directory. For example suppose you are in an office and the office maintains a directory system that contains policies and procedures as well as all client files which relate to the running of the business. All of these documents are located in one directory on the file system called /home/office. The directory system has been expanded and a new partition has been created called /off. The /off partition will contain all of the office material that is located in /home/office. Therefore, /home/office needs to be copied to /off. Here is the directory system that must be copied.

 

/home/office

/procedures

/policies

/activities

/clients

 

cp -R /home/office /off

 

The command recursively (that means folders and sub-folders) copies all of the material in /home/office to /off.

 

Now an additional problem may arise. In a Linux file system you have the ability to set permissions of read/write/execute on each file individually. Suppose the file system that was created in /home/office has individual permissions set on each file in terms of who may modify each file within the office. You need to then maintain those file permissions when you copy. That may easily be accomplished by using the -p option that will maintain permissions originally set. In this example, the same recursive action is taken by copying all of the files and folders in addition the permissions are maintained.

 

cp -Rp /home/office /off

 

The p option is added with the -R.