The cp command is used to create an exact copy of a file or directory and
place it in a specified location, either under a new name or in a different directory. This command
is fundamental when you need to back up files, duplicate files for editing, or move files into
organized directories without losing the original.
>Synopsis
cp [option] source_file
target_file/directory
>Options
-r
To copy a directory and its contents, you must use the -r (or --recursive)
option. For example, cp -r dir1/ dir2/ copies the entire dir1
directory into dir2. You can verify that the directory has been copied by
listing the contents of destination_folder using the lscommand.
Example:
-zsh
> cp -r source_folder destination_folder
> ls destination_folder
> source_folder
If you try to copy a non-existent directory, you'll see an error like this:
Example:
-zsh
> cp -r source_folder destination_folder
cp: source_folder/: No such file or directory
-i
The -i option in the cp command stands for
interactive mode, it's specifically used to prompt you before
overwriting each individual file during the copy operation.
When you use -i, the command will ask whether you want to overwrite a
file in the destination if a file with the same name already exists.
Your Options:
y: Overwrite the file with the one from the
source. n: Do not overwrite the file, and the command will skip
that file.
If you don't enter anything and just press Return, the default behavior is not to
overwrite (n is assumed). The same goes for copying multiple files and
one of them conflicts.
The -f option in the cp command stands for
force and is used to forcefully overwrite files in the destination
directory without any prompts, even if the file already exists. This means that if
you're copying files and there's a conflict (i.e., a file with the same name already
exists in the destination), the -f option will automatically overwrite
the existing file without asking for confirmation.
Example:
-zsh
> cp -f source_folder destination_folder
> ls destination_folder
> source_folder
-u
The -u option in the cp command copies only when the source
file is newer than the destination file or when the destination file is missing. When you
use
cp -u,
only newer files will be copied, and if the destination file is already up-to-date, it will
not
be overwritten; nothing will happen, and there will be no output (unless there's an error).
Example:
-zsh
> cp -u source_folder destination_folder
-v
The -v option in the cpVerbose mode, which shows
the files being copied, indicating that the copy operation has taken place.
Example:
-zsh
> cp -v folder1 folder2
folder1 -> folder2
-p
The -p option in the cp option can be used to preserve file
attributes
such as timestamps and permissions during the copy process. E.g
cp -p file1.txt file2.txt
This command copies file1.txt to file2.txt while preserving the original file's permissions,
timestamps,
and ownership.
Example: Check the file attributes before copying
-zsh
> ls -l source_folder/example.txt
> -rw-r--r-- 1 user1 staff 1234 Oct 1 12:34
source_folder/example.txt