cp

cp linux command cheatsheet by Thamizhiniyan C S

Introduction

The cp command is used to copy files and directories.


Syntax

cp [OPTION]... [-T] SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...


Important Flags

FlagDescription

-a, --archive

Preserve the source's metadata, such as creation date, permissions, and extended attributes.

-b, --backup

Create backups for destination files. The backup file has the (~) suffix unless --suffix is used.

-f, --force

If the destination file/directory already exists, replace it with the source.

-i, --interactive

Ask before overwriting the destination with the source.

-l, --link

Create hard links instead of new files. The destination file will have the same inode attribute as the source.

-n, --no-clobber

Do not overwrite the destination file if it exists.

-R, -r, --recursive

Copy the source directory recursively.

-S, --suffix=

Provide a custom suffix for the backup file.

-t, --target-directory=

Specify a directory for the source files.

-u, --update

Replace files only if they satisfy the update condition. The short option replaces files if the destination is older than the source. The long option lets the user specify the condition (all, none, or older).

-v, --verbose

Output information about the copying process.

-p

Preserves file characteristics (modification time, access time, ownership, permission-bits).

*

Uses the * wildcard to represent all files and directories matching a pattern.

--help

Show help.

--version

Show version information.


Examples

CommandDescription
cp file-1.txt file-2.txt file-3.txt test-dir

Copy file-1.txt, file-2.txt, and file-3.txt to the test-dir directory.

cp file-* test-dir

Copy all files starting with file- to test-dir using the * wildcard.

cp -v -i file-1.txt test-dir

Copy file-1.txt to test-dir with verbose output. Prompt to overwrite if the file exists.

cp -v -b -S .bak file-1.txt test-dir

Copy file-1.txt to test-dir, creating a backup file with .bak extension. Show verbose output.

cp Src_file Dest_file

Copy contents of Src_file to Dest_file. Overwrites Dest_file if it exists.

cp a.txt b.txt

Copy contents of a.txt to b.txt. Overwrites b.txt if it exists.

cp a.txt c.txt

Copy contents of a.txt to c.txt. Overwrites c.txt if it exists.

cp Src_file1 Src_file2 Src_file3 Dest_directory

Copy Src_file1, Src_file2, and Src_file3 to Dest_directory. Overwrites existing files in Dest_directory.

cp a.txt b.txt c.txt new/

Copy a.txt, b.txt, and c.txt to new/ directory. Overwrites existing files in new/.

cp -R Src_directory Dest_directory

Recursively copy all files from Src_directory to Dest_directory.

cp -i [Source_file] [Destination_file]

Interactive copying: prompts before overwriting Destination_file. Press 'y' to confirm overwrite.

cp -i a.txt b.txt

Interactive copy of a.txt to b.txt. Prompts to confirm overwrite of b.txt.

cp -f [Source_file] [Destination_file]

Force copying: deletes Destination_file if unable to write, then copies Source_file.

cp -f a.txt b.txt

Forceful copy of a.txt to b.txt. Overwrites b.txt without prompting.

cp -r [Directory_name1] [Directory_name2]

Recursive copy of Directory_name1 contents to Directory_name2.

cp -r source_directory dest_directory

Recursively copy contents of source_directory to dest_directory.

cp -p [Source_file] [Destination_file]

Preserve copying: retains original file characteristics (timestamps, ownership, permissions) in Destination_file.

cp -p a.txt c.txt

Preserves characteristics of a.txt when copying to c.txt.

cp *.txt [Destination Directory or file]

Copy all .txt files to Destination Directory or file using * wildcard.

cp *.txt Folder1

Copy all .txt files to Folder1 using * wildcard.

Last updated