cp - Linux


Overview

The cp command in Linux is used to copy files and directories from one location to another. This command is essential for managing file systems, backing up data, and transferring information within the file system hierarchy. It is most effective in scripting, batch jobs, and manual file management tasks.

Syntax

The basic syntax of the cp command is as follows:

cp [OPTION]... SOURCE... DESTINATION
  • SOURCE: The source file or directory to be copied.
  • DESTINATION: The destination path where the files or directories should be copied.
  • Multiple SOURCE files can be specified for copying to a directory specified as DESTINATION.

Options/Flags

The cp command includes several options that alter its behavior:

  • -a, –archive: Copy files and directories and all of their attributes, including symbolic links.
  • -R, -r, –recursive: Recursively copy directories, including all subdirectories and files.
  • -i, –interactive: Prompt before overwrite (overrides any prior -n option).
  • -n, –no-clobber: Do not overwrite an existing file.
  • -u, –update: Only copy files that are newer than the existing file in the destination or missing from it.
  • -v, –verbose: Show what is being done, by outputting the name of each file before copying.
  • -l, –link: Instead of copying, link the files using hard links.
  • -s, –symbolic-link: Instead of copying, make symbolic links to files.
  • -t, –target-directory=DIRECTORY: Copy all SOURCE arguments into DIRECTORY.
  • -T, –no-target-directory: Treat DESTINATION as a normal file and not a directory.

Examples

  1. Basic Copying
    Copy a file to another directory:

    cp myfile.txt /tmp
    
  2. Recursive Copy
    Copy a directory and all its contents:

    cp -R /myfolder /backups/myfolder_backup
    
  3. Verbose Output
    Copy with detailed logs:

    cp -v myfile.txt /tmp
    
  4. Preventing Overwrite
    Copy without overwriting existing files:

    cp -n source_file.txt destination_file.txt
    
  5. Copying Multiple Files
    Copy multiple files to a directory:

    cp file1.txt file2.txt file3.txt /mydirectory/
    

Common Issues

  • Permission Denied: Ensure you have the required permissions to read the source and write to the destination.
  • Target is a Directory: Use the -T flag to treat the destination as a regular file when it is mistakenly treated as a directory.
  • Overwriting Files Unintentionally: Always use the -i flag to prompt before overwriting files.

Integration

The cp command can be combined with other UNIX/Linux commands for powerful scripting and batch processing. For example, to find all .txt files in a directory and copy them to another directory:

find /source/directory -name "*.txt" -exec cp {} /destination/directory \;
  • mv: Command to move files or directories.
  • rm: Command to remove files or directories.
  • rsync: Tool for syncing files and directories across systems efficiently.

For further reading and detailed documentation, visit the GNU core utilities page or use man cp to access the manual directly in the terminal.