Linux cp Command: Copy Files and Directories

By 

Updated on

6 min read

Using the Linux cp command to copy files and directories

cp is a command-line utility for copying files and directories on Linux and Unix systems. It is one of the most frequently used commands when working in the terminal.

This guide explains how to use the cp command with practical examples. For an overview of all tools available for copying files, see How to Copy Files and Directories in Linux .

Syntax

The general syntax for the cp command is:

txt
cp [OPTIONS] SOURCE... DESTINATION

SOURCE can be one or more files or directories. DESTINATION is a single file or directory. The behavior depends on what you pass:

  • When SOURCE and DESTINATION are both file paths, cp copies the source file to the destination. If the destination file does not exist, it is created.
  • When SOURCE contains multiple arguments, DESTINATION must be a directory. Each source file or directory is copied into it.
  • When SOURCE is a directory, use the -R option to copy it recursively.

To copy files and directories you must have at least read permission on the source and write permission on the destination directory. To learn more about file permissions, see Understanding Linux File Permissions .

Copy a File

To copy a file in the current working directory , specify the source file and the destination file name:

Terminal
cp file.txt file_backup.txt

Bash brace expansion offers a shorter form for the same operation:

Terminal
cp file.txt{,_backup}

Both commands produce file_backup.txt as a copy of file.txt.

To copy a file to another directory, provide the destination directory path. When only a directory is given, the copied file keeps its original name:

Terminal
cp file.txt /backup

To copy the file to a different directory under a new name, include the file name in the destination path:

Terminal
cp file.txt /backup/new_file.txt

Overwrite Behavior

By default, cp overwrites the destination file if it already exists. Use the -n (--no-clobber) option to skip existing files:

Terminal
cp -n file.txt file_backup.txt

To prompt for confirmation before overwriting, use the -i (--interactive) option:

Terminal
cp -i file.txt file_backup.txt
output
cp: overwrite 'file_backup.txt'?

To copy only when the source is newer than the destination, use -u (--update):

Terminal
cp -u file.txt file_backup.txt

Preserve File Attributes

When cp creates a new file, it is owned by the user running the command with a timestamp of the current time. Use the -p (--preserve) option to preserve the file mode, ownership , and timestamps from the source:

Terminal
cp -p file.txt file_backup.txt

Verbose Output

Use the -v (--verbose) option to print each file as it is copied:

Terminal
cp -v file.txt file_backup.txt
output
'file.txt' -> 'file_backup.txt'

Copy a Directory

To copy a directory and all its contents recursively, use the -R or -r option:

Terminal
cp -R Pictures Pictures_backup

If Pictures_backup does not exist, cp creates it and copies the contents of Pictures into it. If Pictures_backup already exists, Pictures itself is copied inside it, resulting in Pictures_backup/Pictures/.

To copy only the contents of a directory without the directory itself, use the -T option. This treats the destination as a regular target rather than a container:

Terminal
cp -RT Pictures Pictures_backup

You can also use a wildcard to copy directory contents, though note that this approach does not copy hidden files and directories (those starting with .):

Terminal
cp -R Pictures/* Pictures_backup/

All file-copy options such as -p, -i, -u, and -v work the same way when copying directories. The -R flag is always required for directory copies.

Copy Multiple Files

To copy multiple files into a directory, list the sources followed by the destination directory as the last argument:

Terminal
cp file.txt file1.txt dir/ /backup

When copying multiple sources, the destination must be a directory.

Quick Reference

TaskCommand
Copy a filecp source.txt dest.txt
Copy to a directorycp file.txt /backup/
Copy without overwritingcp -n source.txt dest.txt
Prompt before overwritingcp -i source.txt dest.txt
Copy only if source is newercp -u source.txt dest.txt
Preserve permissions and timestampscp -p source.txt dest.txt
Verbose outputcp -v source.txt dest.txt
Copy directory recursivelycp -R sourcedir/ destdir/
Copy directory contents onlycp -RT sourcedir/ destdir/
Copy multiple files to directorycp file1 file2 dir/ /dest/

Troubleshooting

cp: cannot stat 'source': No such file or directory
The source file or directory does not exist. Check the path for typos and confirm the file is present with ls -l source.

cp: omitting directory 'dirname'
You tried to copy a directory without the -R option. Add -R to copy recursively: cp -R sourcedir/ destdir/.

cp: cannot create regular file 'dest': Permission denied
You do not have write permission on the destination directory. Check permissions with ls -l and use sudo if required, or change ownership with chown .

cp: cannot open 'source' for reading: Permission denied
You do not have read permission on the source file. See Understanding Linux File Permissions for how to inspect and adjust permissions.

FAQ

How do I copy a directory and all its contents?
Use the -R (recursive) option: cp -R sourcedir/ destdir/. Without -R, cp will refuse to copy a directory and print a warning.

How do I copy files without overwriting existing ones?
Use the -n (--no-clobber) option: cp -n source.txt dest.txt. Files that already exist at the destination are silently skipped.

How do I preserve file permissions and timestamps when copying?
Use the -p option: cp -p source.txt dest.txt. This preserves the file mode, ownership, and timestamps from the original file.

What is the difference between cp and rsync?
cp is the standard tool for local copies. rsync is better for large transfers or keeping directories in sync — it skips files that have not changed, supports remote transfers over SSH, and can resume interrupted operations.

How do I copy hidden files with a wildcard?
The glob * does not match files starting with .. To include hidden files, use cp -R sourcedir/. destdir/ (note the trailing /.), or list them explicitly.

What is the difference between cp and mv?
cp creates a copy while leaving the original in place. mv moves the file, removing it from the source location.

Conclusion

The cp command is the standard way to copy files and directories in Linux. Use -R for directories, -p to preserve attributes, and -n or -i to control overwrite behavior.

For copying files over a network, use the rsync or scp utilities.

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page