Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The cp
command in Linux (short for "copy") is a utility used to copy files and directories. In this tutorial, we will discuss how to use the cp
command effectively, along with various options and examples.
Basic usage of the cp
command:
To copy a file, use the cp
command followed by the source file and the destination file or directory:
cp source.txt destination.txt
This command will create a copy of source.txt
and name it destination.txt
. If destination.txt
already exists, its contents will be overwritten. If the destination is a directory, the file will be copied into that directory with the same name.
Copying directories:
To copy a directory, use the -R
or --recursive
option followed by the source directory and the destination directory:
cp -R source_directory destination_directory
This command will create a copy of source_directory
and all its contents (including subdirectories) inside destination_directory
.
Preserving file attributes:
When copying files or directories, you might want to preserve the original file attributes such as timestamps, permissions, and ownership. To do so, use the -p
or --preserve
option:
cp -p source.txt destination.txt
cp -Rp source_directory destination_directory
These commands will preserve the attributes when copying files or directories.
Prevent overwriting existing files:
To avoid overwriting existing files, use the -n
or --no-clobber
option:
cp -n source.txt destination.txt
This command will only copy the file if destination.txt
does not already exist.
Prompt before overwriting files:
If you want to be prompted before overwriting a file, use the -i
or --interactive
option:
cp -i source.txt destination.txt
This command will ask for confirmation before overwriting destination.txt
.
Creating symbolic links and hard links:
To create a symbolic link instead of copying the file, use the -s
or --symbolic-link
option:
cp -s source.txt symlink.txt
This command will create a symbolic link symlink.txt
pointing to source.txt
.
To create a hard link instead of copying the file, use the -l
or --link
option:
cp -l source.txt hardlink.txt
This command will create a hard link hardlink.txt
pointing to the same inode as source.txt
.
By following this tutorial, you should now have a good understanding of how to use the cp
command in Linux to copy files and directories. The cp
command is an essential tool for managing files and allows you to efficiently duplicate, preserve, and organize your data.
Copying files in Linux with cp:
cp source_file destination
Copying directories and subdirectories with cp:
-r
(or -R
) option:cp -r source_directory destination
Preserving file attributes during copy with cp:
-a
option:cp -a source_file destination
Recursive copy with cp in Linux:
-r
option:cp -r source_directory destination
Overwriting files with cp command:
-f
option:cp -f source_file destination
Copying files with specific permissions in cp:
--preserve
option:cp --preserve=mode,ownership source_file destination
Copying files to a different directory in Linux:
cp source_file /path/to/destination_directory/
Using wildcards with cp for bulk file copy:
cp *.txt destination_directory/