Master the Linux CP Command with Examples
Linux cp Command with Examples
Linux cp command is a command line utility for copying files and directories. It supports several options which allow you to copy files more effectively. In this article, we will learn the basics of cp command and its usage through some examples.
Syntax
cp [OPTIONS] SOURCE DESTINATION
Options
- -i: Prompts before overwriting
- -R: Copies data recursively (copies all files and subdirectories, including their contents)
- -v: Verbose mode
- -p: Preserves modification times, access times, and modes from the original file
- -f: Silently overwrites existing files and does not prompt for confirmation
Examples
Copying a Single File
# cp file1.txt /tmp/
Copying Multiple Files
# cp file1.txt file2.txt file3.txt /tmp/
Copy a File to a Different Name (or Location)
# cp file1.txt file1_new.txt
Prompt Before Overwriting
# cp -i file1.txt /tmp/file1.txt
Copy Directories Recursively
# cp -R source_dir /tmp/
Verbose Mode
# cp -v file1.txt /tmp/
Preserve File Attributes
# cp -p file1.txt /tmp/
Silently Overwrite Files
# cp -f file1.txt /tmp/
We hope you got a good understanding of linux cp command and its usage with some examples. To know more about cp command you can use 'man' or 'info' command.