how to use cp command featured image

How to use cp Command?

The “Copy” also known as cp command is used to move files and directories around the file system. A file or directory can be copied with the command, then moved to a new location.

Syntax Example

cp [source] [destination]
Using cp command sample syntax
This is a simple use of “cp file4.txt cp-command/” which creates a copy of the “file4.txt” file inside the folder “cp-command”.

Where source denotes the location of the file or directory you wish to copy, and destination denotes the place to which you wish to transfer the file or directory.

Common Options Used with cp command

  • -r: Recursively copying directories and their contents is done with the -r option. A directory, together with all of its files and subdirectories, will be copied.
how to use cp command with -r flag
In the example we have three files inside the “howto” folder. Whereas “cp-command” is an empty directory. In order to copy the entire content of the “howto” directory to “cp-command”, “-r” options can be useful.
  • -i: The -i option asks the user for approval before overwriting a file that already exists.
how to use cp command with -i flag
As seen in the example we have a file named “file4.txt” in the root directory. Similarly “file4.txt” also exists inside “cp-command” directory with a different content. When we use “cp -i file4.txt cp-command/ “ , a prompt asking for permission to overwrite the content will be presented. Upon agreeing to overwrite the content of the existing file is changed.
  • -u: Only when the destination file is older than the source file should you use the -u option to copy files.
How to use cp command with -u flag
The image above shows the files with the time the time they were created. So when we use “cp -u sample.txt cp-command/”, if the existing file inside “cp-command” directory is older than the source file it will be overwritten. Look at the example below to know what happens if the existing file is newer.

Special Note

In this article we see different variations of “ls” command. To know more about the use of the ls command look at the article below

how  not to use cp command with -u flag
If the existing file is newer than the source file as seen in the example, the file will be skipped and the content will not be overwritten.
  • -v: Use the -v option to show verbose output. The command’s status and the name of the file or directory that is being copied will be displayed.
how to use cp command with -v flag
The use of “-v” option prints the job done on to the terminal. Using this option does not effect the task. In the example “cp -v sample.txt cp-command/”, we are creating a copy of “sample.txt” inside the directory “cp-command”. The process completes and a message also appears in the terminal informing us what was done.

Uses of cp command and flag

  • to duplicate a file or directory and put it in another location, you frequently use this command.
  • for copying a directory and all of its contents, including subdirectories and files, the -r option comes in handy
  • copy a file or directory and there is a potential that it might overwrite an existing file, the -i option can be helpful
  • to only transfer the most recent version of the file while maintaining a backup, the -u option can be helpful.

It’s essential to remember that when you use the command to copy a file or directory, the original file or directory will remain in place. This means that the original file or directory will not be deleted from the source location.

If you want to delete the original file while placing it into another location. Have a look at the article below

1 thought on “How to use cp Command?”

  1. israelnightclub

    May I simply say what a comfort to discover somebody who genuinely knows what they are talking about over the internet. You actually understand how to bring a problem to light and make it important. More people ought to check this out and understand this side of the story. I cant believe you arent more popular because you surely possess the gift.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top