
The cp
command in Bash is used to copy files and directories from one location to another.
Copy Command Syntax and Explanation
The basic syntax of the cp
command is:
cp [OPTION]... SOURCE... DESTINATION
- SOURCE: The file or directory you want to copy.
- DESTINATION: The location where you want to place the copied file or directory.
- OPTION: Optional flags that modify the behavior of the
cp
command.
Commonly used options include:
-r
or-R
: Recursively copy directories and their contents.-i
: Prompt before overwriting an existing file.-u
: Copy only when the SOURCE file is newer than the DESTINATION file or when the DESTINATION file is missing.-v
: Verbose mode, which shows the files being copied.-T
: Treat the destination as a normal file. This is useful when you don’t want to create a new directory at the destination.-f
: Force the copy operation by removing the destination file if it cannot be opened.
Copying Files Example
To copy a file from one location to another, use the cp
command with the source file path and the destination path.
cp /path/to/source/file.txt /path/to/destination/
Example:
cp ~/Documents/report.txt ~/Backup/
This command copies the report.txt
file from the Documents
directory to the Backup
directory.
Copying a File with a Different Name
You can copy a file and give it a different name in the destination directory.
cp /path/to/source/file.txt /path/to/destination/newfile.txt
Example:
cp ~/Documents/report.txt ~/Backup/report_backup.txt
This command copies the report.txt
file to the Backup
directory and renames it to report_backup.txt
.
Copy Directory to a New Directory
#Understanding #Command #Bash #Javascript #Jeep