The primary usage of cp command is to “copy” files or directory from one location to another.
On Linux and UNIX flavor machines cp command is one of the most used commands in the day to day activities.
copying the file1 information to file2 within the same directory.
cp file1 file2
More about cp command and its options, please checks its manuals and help content.
Copy content of a file to another file in the same directory:
This is the most practical use case of cp command.
Sometimes, we might want to do some changes to any application configuration file, then it’s always a best practice to take a copy of that file content. This will act as a backup file.
In the below given example, we want to copy “abc_lab1.txt” file content to “abc_lab1_copy.txt” .
Copy the file content but don’t override the existing one:
We can use cp command in combination with “-n” option.
This file will copy the source file to destination file without disturbing or overwriting the original content of destination file.
Therefore the final content of the destination file is source file content plus destination file content.
Let’s observe a practical example.
There is a file and it is having all ftp user details. We have a copy of ftp user details of another system as well. Now, my task is to combine all the users details in the single file.
In this scenario, we will use this option "-n".
Copy the file from one directory to another directory:
Let us take a practical case,
As part of version upgrade for an application software, “app1_config.txt” and other configuration files in installation directory are going to be updated to new format.
So we want to take a copy of these files, in another directory “/opt/bin/App1_Config_backup/” for any roll back activity.
Changing to target directory,
Also note that, to copy a file or create a new file user should be having proper credentials to that parent directory.
Copy entire directory to another directory:
We can use the cp command with option “-r” recursive and copy entire directories and its sub-directories to another directory.
Recursive copy means, first sub directories and their files are copied and in the last parent directory is copied.
Forcing the copy operation:
While copying the file from one location to the another location it will ask for the confirmation by using the cp command with '-f' option won’t prompt the confirmation.
[rreddy@abclearnabclearn_dir1]$cp -f abc_lab1.txt /tmp
[rreddy@abclearn~]$ cd /tmp
Prompting when overwriting a file:
Whenever we are copying the content of one file to the another file there is a chance of losing the important data in the destination file.
By using the cp command with "-i" option we get confirmation alert while overwriting the destination file with the source file.
[rreddy@abclearnabclearn_dir1]$cp -i abc_lab1.txt abc_lab1_copy.txt
cp: overwrite `abc_lab1_copy.txt'? y
Copying the file to the directory without changing the attributes of the file:
While copying the file from one location to the another location the attributes like timestamp of the file in the target location will change.
Without disturbing the attributes of the file we can copy the file from source to target using "-p" option.
Follow the following scenario to understand more.
More to practice:
-u, --update copy only when the SOURCE file is newer
then the destination file or when the
destination file is missing
-s, --symbolic-link make symbolic links instead of copying
-H follow command-line symbolic links in SOURCE
-l, --link hard link files instead of copying
-L, --dereference always follow symbolic links in SOURCE
-a, --archive same as -dR --preserve=all
--attributes-only don't copy the file data, just the attributes
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument