cp

cp is a file management tool used to copy files and directories from one location to another. It can be used to create backups, duplicate files, move files between directories, and more.

Basic Syntax

cp [options] source destination

Here, source specifies the file or directory to be copied, and destination specifies the name and location of the copied file or directory. If the destination file already exists, it will be overwritten by default.

Commonly Used Options

  • -r: This recursively copies directories and their contents.
  • -i: This prompts the user before overwriting an existing file.
  • -u: This copies only files that are newer than the destination file.
  • -p: This preserves the file attributes and permissions, such as the owner, group, timestamp, and mode.
  • -v: This displays verbose output, showing the files being copied and their progress.
  • -n: This does not overwrite an existing file, but skips it instead.
  • -l: This creates a hard link to the source file instead of copying it.

Useful Examples

  • cp file1.txt file2.txt: This copies the contents of file1.txt to file2.txt.
  • cp -r dir1 dir2: This recursively copies the contents of dir1 and its subdirectories to dir2.
  • cp -i file1.txt file2.txt: This prompts the user before overwriting file2.txt with the contents of file1.txt.
  • cp -u file1.txt dir1: This copies file1.txt to dir1 only if it is newer than the existing file.
  • cp -p file1.txt dir1: This copies file1.txt to dir1 while preserving its attributes and permissions.
  • cp -v file1.txt dir1: This copies file1.txt to dir1 and displays verbose output.
  • cp -n file1.txt dir1: This copies file1.txt to dir1 only if there is no existing file with the same name.
  • cp -l file1.txt link1.txt: This creates a hard link to file1.txt named link1.txt.

Leave a Reply

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

Comments

  1. […] Once installed, let’s first allow TCP traffic over your SSH port so that you don’t get locked out of…

  2. […] have the option to choose between Password and SSH key selection. Learn more about SSH keys here. It’s recommended…