Copy Files and Folders on Linux
cp Command
The cp command is one of the basic Linux commands for copying files and directories from one location to another.
When copying files from source to destination, the source file name does not change, but we can change the target file name if we need to.
cp [Option] [Source] [Destination]
+----------------------------------------------------------+
| Option | Description |
+----------------------------------------------------------+
| -v | Verbose mode (Show Progress) |
| -r/R | Copy Directories Recursively |
| -n | Don't overwrite an existing file |
| -d | Copy a link file |
| -i | Prompt before overwrite |
| -p | Preserve the specified attributes |
| -b | Make a backup of each existing destination file |
+----------------------------------------------------------+
Copy a File from One Location to Another in Linux Using the cp Command
cp /home/source/demo.txt /home/destination“cp” - Copy Command
“/home/source/demo.txt” - Source
“/home/destination” - Destination
So we have copied the source file “/home/user1/demo.txt” to the target “/home/user2” directory.
Copy a File from One Location to Another With a Different Name on Linux Using the cp Command
cp /home/source/demo.txt /home/destination/demo1.text“cp” - Copy Command
“/home/source/demo.txt” - Source
“/home/destination/demo1.txt” - Destination
Now, file is copied from the source “/home/source/demo.txt” to the target “/home/destination/demo1.txt” directory with a different name.
Copy Multiple Files from One Location to Another in Linux Using the cp Command
# cp /home/source cp-demo.txt cp-demo-1.txt cp-demo-9.txt /home/destination/
We copied three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt”.
No option is required to perform this action, and all the files must be entered with the space.
Copy a Directory Recursively from One Location to Another in Linux Using the cp Command
If you want to copy a directory recursively from one location to another using the cp command, use the -r/R
option with the cp command.
It copies the folder including the subdirectories and their files to the target directory.
The folder name remains same.
cp -r /home/source/demo-folder/ /home/destination/
Copy Multiple Folders on Linux Using the cp Command
It’s like the one above and it allows you to copy multiple folders at once.
cp -r /home/source/demo-folder/ /home/another_folder/public_html/ /home/destination_folder/test
Copy All Files from One Location to Another in Linux Using “wildcard (*)”
If you have a list of files and you want to copy them all to another location, use the following cp command format.
This excludes the directory by default, and the “-r” option must be included in this command to copy them.
cp /home/source/* /home/destination/
Copy All Files and Folders from One Location to Another in Linux Using “wildcard (*)”
To do so, use the following cp command format. This is similar to the one above, but it will copy files and folders to the target directory at the same time.
cp -R /home/source/* /home/destination/
Copy All Files, Including Hidden Files (“.” Dot Files) in Linux Using the cp Command
This command is the same as above, but you need to add “dot (.)” in addition to copy all the files, including hidden files.
cp -R /home/source/.* /home/destination/all-files/
Thanks for Reading!!!!!!!!