• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

cp: Copy files and directories

by admin

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.

Filed Under: Linux

Some more articles you might also be interested in …

  1. mkfs Command Examples in Linux
  2. “docker exec” Command Examples
  3. Difference between absolute and relative paths in Linux
  4. How to Setup SSH keys for “passwordless” SSH Login on CentOS/RHEL
  5. script: command not found
  6. CentOS/RHEL – How to Remove stale ISCSI Target Node Information from ISCSI Initiator Server
  7. ffsend: Easily and securely share files from command-line
  8. gdu: Disk usage analyzer with console interface
  9. sed: command not found
  10. readpe Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • “glab repo” Command Examples
  • “glab release” Command Examples
  • “glab pipeline” Command Examples
  • “glab mr” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright