Linux and Unix CP Command linux-unix-cp-command.pdf

Linux CP Command Overview

Linux "cp" command is used to copy file(s) or directory(s) from one directory to another. So Linux "cp" command comes handy when you are restructuring your directory.

Linux CP Command Syntax

cp [--options] Source Destination-Directory

Linux CP Command Examples

Consider file sample_file.txt and directory sample_dir and destination directory as /var/www/dest_dir

Linux CP Command Example Case 1:

Let us copy file sample_file.txt in dest_dir. Linux CP Command for that will be

cp sample_file ./dest_dir

Linux CP Command Example Case 2:

Let us copy directory sample_dir in dest_dir. Linux CP Command for that will be

cp ./sample_dir ./dest_dir

Linux CP Command Example Case 3:

If the source file or directory already exists in dest_dir then by default Linux CP Command will ovewrite the exisitng file without prompting. If you want to get a prompt before overwriting you can use "-i" parameter. Linux CP Command for that will be

cp -i source_file.txt ./dest_dir
cp -i source_dir ./dest_dir

"-i" parameter will give a prompt if dest_dir already contains source_file.txt or source_dir.

Linux CP Command Example Case 4:

Suppose you want to copy all the content of source_dir to dest_dir. The Linux CP command for that will be

cp -R ./source_dest ./dest_dir

"-R" parameter will recursively copy all the content of source_dir to dest_dir

Linux CP Command Example Case 5:

Suppose you want to create a backup file of sample_file.txt with the name of sample_file_backup.txt. Linux CP Command for that will be

cp sample_file.txt sample_file_backup.txt

Similarly if you want to create a backup directory of source_dir as source_dir_backup. Linux CP Command for that will beĀ 

cp source_dir source_dir_backup




This article is contributed by Ankur Rastogi on Sun Apr 17 2016 10:14:08 GMT+0530 (IST)

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.