How to copy a file, group of files, or directory in Linux?

The cp command is used to copy files or directories in Linux/Unix systems. It allows you to duplicate files from a source location to a destination directory. By default, cp works with files only − to copy directories, you must use the -R (recursive) option.

Syntax

The general syntax of the cp command is as follows −

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

Common Options

Option Description
-R, -r, --recursive Copy directories recursively
-i, --interactive Prompt before overwriting files
-f, --force Copy forcefully without prompting
-v, --verbose Show files being copied
-u, --update Copy only when source is newer than destination
-l, --link Create hard links instead of copying
--backup Create backup of destination file
-p Preserve file attributes (timestamps, permissions)

Examples

Copy a Single File

Copy a file from current directory to another directory −

cp file.txt /home/user/documents/

Copy a file with a new name −

cp source.txt destination.txt

Copy Multiple Files

Copy all .txt files to a destination directory −

cp *.txt /home/user/backup/

Copy specific files to a directory −

cp file1.txt file2.txt file3.txt /home/user/documents/

Copy Directories

Copy a directory and its contents recursively −

cp -R source_directory/ destination_directory/

Copy directory with verbose output −

cp -Rv projects/ /home/user/backup/

Interactive and Safe Copying

Prompt before overwriting existing files −

cp -i file.txt /home/user/documents/
cp: overwrite '/home/user/documents/file.txt'? y

Preserve file attributes while copying −

cp -p important.txt /home/user/backup/

Advanced Usage

Copy only if source is newer than destination −

cp -u *.txt /home/user/documents/

Create backup of existing files before overwriting −

cp --backup=numbered file.txt /home/user/documents/

Common Use Cases

  • Backup files − Create copies of important documents in backup directories

  • Template creation − Copy configuration files as templates for new setups

  • Project duplication − Copy entire directory structures for new projects

  • File distribution − Copy files to multiple locations using wildcards

Conclusion

The cp command is essential for file management in Linux systems. It provides flexible options for copying single files, multiple files, or entire directories. Remember to use -R for directories and -i for safe interactive copying to avoid accidental overwrites.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements