Master the Linux ‘cp’ Command: A Comprehensive Guide

Peter Hou
3 min readApr 28, 2023

--

This article provides a comprehensive guide for beginners on using the Linux cp command. It covers the command's history, usage, commonly used parameters, advanced techniques, and precautions. By studying this article, beginners will be able to effectively use the cp command for daily file operations. Be cautious when using certain options (such as -f) to avoid overwriting important files. Practice using different parameter combinations to better grasp the functionalities of the cp command.

Instructions

This article provides a detailed introduction to the Linux cp command for beginners, covering its history, usage, common parameters, and advanced techniques.

History

The cp command is a part of the core GNU utilities and has been a part of the UNIX operating system since its early days. It has been carried over to Linux and other Unix-like systems due to its simplicity and usefulness.

When and why to use it

The cp command is used to copy files and directories within the file system. It is a fundamental command in Linux and other Unix-like systems, allowing users to duplicate files and directories for backup, sharing, or modification purposes.

How to use it

The basic syntax for the cp command is cp [options] SOURCE DEST, where SOURCE is the file or directory to be copied, and DEST is the destination file or directory.

cp file.txt copy_of_file.txt

The commonly used parameters

  • -r or --recursive: Copy directories recursively, preserving their structure.
cp -r source_directory destination_directory
  • -p: Preserve the file's mode, ownership, and timestamps.
cp -p file.txt copy_of_file.txt
  • -i: Prompt before overwriting an existing file in the destination.
cp -i file.txt existing_file.txt
  • -u: Update, only copy if the source file is newer than the destination file or if the destination file does not exist.
cp -u file.txt destination_directory
  • -v: Verbose mode, display files as they are being copied.
cp -v file.txt copy_of_file.txt

Other supported parameters

  • -b or --backup: Create a backup of each existing destination file.
  • -f or --remove-destination: Force, and remove an existing destination file if necessary.
  • -l: Create hard links instead of copying the files.
  • -s: Create symbolic links instead of copying the files.

Most common use cases

  • Copying a single file to another location:
cp file.txt destination_directory
  • Copying a directory and its contents to another location:
cp -r source_directory destination_directory
  • Updating files in a backup directory:
cp -u source_directory/* backup_directory

The tricky skills

  • Using cp to create a backup with a different file extension:
cp file.txt{,.bak}

What needs to be noted

  • Be cautious when using the -f option, as it may overwrite important files.
  • The cp command does not preserve symbolic links by default. Use the -d option to preserve symbolic links.

Conclusion

The Linux cp command is a fundamental tool for copying files and directories. By understanding its various parameters and use cases, you can effectively manage your file system and ensure the safety of your data.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet