cp: Copy files and directories

The “cp” command is a commonly used command-line utility in Unix-like operating systems that allows you to copy files and directories from one location to another. Its primary purpose is to duplicate files and directories, preserving their content and metadata in the process.

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

# cp [OPTIONS] SOURCE DESTINATION

Here, “SOURCE” refers to the file or directory you want to copy, and “DESTINATION” specifies the location where you want to create the copy. The “SOURCE” can be a single file or a directory containing multiple files and subdirectories.

The “cp” command offers various options to customize the copying process. Some commonly used options include preserving symbolic links (-L or -P), preserving the original ownership and group (-a), and displaying verbose output (-v) for tracking the progress of the copy operation.

cp Command Examples

1. Copy a file to another location:

# cp /path/to/source_file.ext /path/to/target_file.ext

2. Copy a file into another directory, keeping the filename:

# cp /path/to/source_file.ext /path/to/target_parent_directory

3. Recursively copy a directory’s contents to another location (if the destination exists, the directory is copied inside it):

# cp -R /path/to/source_directory /path/to/target_directory

4. Copy a directory recursively, in verbose mode (shows files as they are copied):

# cp -vR /path/to/source_directory /path/to/target_directory

5. Copy text files to another location, in interactive mode (prompts user before overwriting):

# cp -i *.txt /path/to/target_directory

6. Follow symbolic links before copying:

# cp -L link /path/to/target_directory

Summary

In summary, the “cp” command is a versatile tool for copying files and directories in Unix-like operating systems. It provides a straightforward way to duplicate files while preserving their content, attributes, and directory structure. By using various options, you can customize the copying process according to your needs.

Related Post