Mastering the cp Command in Linux/Unix with Examples

Mastering the cp Command in Linux/Unix with Examples


In the Linux/Unix operating system, the “cp” command stands for “copy.” It is a fundamental and versatile command used to create duplicates of files and directories, either within the same filesystem or across different filesystems. Understanding the ins and outs of the “cp” command is essential for efficient file management and data backup. In this blog post, we will explore the various options and examples of using the “cp” command in Linux/Unix.

Basic Syntax

The basic syntax of the “cp” command is as follows:

cp [options] source_file destination_file


  • -r or -R: Recursively copy directories (needed when copying directories).
  • -i: Prompt before overwriting files.
  • -v: Verbose mode, display detailed output of each file copied.
  • -p: Preserve file attributes, including permissions and timestamps.
  • -u: Update, copy only when the source file is newer than the destination file.
  • -l: Create hard links instead of copying files (for multiple hard links to the same file).
  • -s: Create symbolic links (symlinks) instead of copying files.

Streamlining Data Transfer: Uploading Multiple Files from Amazon S3 to Salesforce


Copy a File to a Different Directory

cp file.txt /path/to/destination/

Copy Multiple Files to a Directory

cp file1.txt file2.txt /path/to/destination/

Copy All Files and Subdirectories in a Directory to Another Directory

cp -r /path/to/source/ /path/to/destination/

Copy a Directory with Preserving File Attributes

cp -rp /path/to/source_directory/ /path/to/destination/

Prompt Before Overwriting Files

cp -i file.txt /path/to/destination/

Copy Only Newer Files

cp -u /path/to/source/file.txt /path/to/destination/

Create a Hard Link to a File

cp -l file.txt /path/to/hard_link/

Create a Symbolic Link to a File

cp -s file.txt /path/to/symlink/

Verbose Mode – Display Detailed Output

cp -v file1.txt file2.txt /path/to/destination/

Copying Files to a Remote Server (using SSH)

The “cp” command can also be used to copy files to a remote server using the Secure Shell (SSH) protocol. This is particularly useful when you need to transfer files securely between local and remote machines.

cp file.txt user@remote_server:/path/to/destination/

Copying Files with Verbose Mode and Prompt Before Overwriting

By combining the options, you can have more control over the copy process. The “-v” option enables verbose mode, displaying detailed output of each file copied, and the “-i” option prompts for confirmation before overwriting existing files.

cp -vi file1.txt file2.txt /path/to/destination/

Copying Files with a Specific File Extension

You can use wildcards to copy files with a specific file extension. In the following example, all files with the “.txt” extension will be copied to the destination directory.

cp *.txt /path/to/destination/

Copying Files and Directories Excluding Some Files

You can exclude specific files from being copied using the “!” symbol within parentheses. In this example, all files and directories in the source_directory will be copied to the destination directory, except “file1.txt” and “file2.txt.”

cp -r /path/to/source_directory/!(file1.txt|file2.txt) /path/to/destination/

Copying and Merging Directories (Preserving Subdirectories)

The “-u” option can be used to copy and merge directories while preserving subdirectories. This is useful when you want to update an existing directory with new files.

cp -ur /path/to/source_directory/* /path/to/destination/

Copying Files Using Wildcards (for batch copying)

Wildcards can also be used for batch copying files that match a certain pattern. In this example, all files with names starting with “file” and ending with “.txt” will be copied to the destination directory.

cp file*.txt /path/to/destination/

The “cp” command is a powerful tool in Linux/Unix for copying files and directories. With its various options, it allows users to perform simple file copying tasks, create backups, and preserve file attributes during the copy process. By mastering the “cp” command and understanding its options, users can efficiently manage their files and directories, ensuring smooth data organization and backup procedures.

Remember to exercise caution when using the “cp” command, especially with options like -i, which prompts for overwriting files. Incorrect usage of this command can lead to data loss or accidental file overwrites. Always double-check your commands before executing them, and regularly make backups of critical data to prevent any unintended data loss.

Leave a Reply

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

Top 10 Mobile Phone Brands in the World Top 10 cartoons in the world Top 10 hollywood movies 2023 Top 10 Cars in The World 10 best social media platforms 10 Best Small Business Tools for Beginners Top 10 universities in the world Top 10 scenic drives in the world Top 10 Tourist Destinations in world Top 10 Best Airlines in the World Top 10 Crytocurrencies Top 10 Most Beautiful Beaches in the World Top 10 Fastest Growing Economies in the World 2023 Top 10 Websites To Learn Skills For Free Top 10 AI Websites 10 Top Most Popular Databases in the World Top 10 Best Image Viewers 10 Best Collage Maker Apps 10 Ringtone Apps for Android & iPhone Top Android Games That Support Controllers