This guide will go through how to copy a file in Linux. The article will take you through common commands used in Linux for copying files such as cp command for copying files within the same computer, scp used to copy files within two computers through SSH protocol and rsync used for several tasks e.g. copying files.
How to copy a file in Linux using CP command
- Copy and rename files in the same directory using the command
cp current_name renamed_file
e.g.
cp demofile.txt file.txt
- Copy file from the current directory to another directory
cp demofile /another directory path
cp demofile.txt /home/itnixpro/Downloads
- Copy the file to another directory and rename it.
cp demofile.txt /home/itnixpro/Downloads/anotherfile.txt
- To copy multiple files from one directory to another use the command
cp file1 file2 file3 /new_directory path
e.g.
cp anotherfile.txt demofile.txt demo.txt /home/itnixpro/Documents
- Copy file from a specific directory to another directory from anywhere using the command
cp directory_path_with_file /new_directory_path
e.g.
cp /home/itnixpro/Documents/demofile.txt /home/itnixpro/Pictures
- Additionally, you can copy the folder contents such as files and subfolders by adding
-R/-r/--recursive
option.
cp -R /home/itnixpro/Documents/Content /home/itnixpro/Videos
How to copy a file in Linux using SCP command
- The SCP command is used to copy files securely between two Linux computers, that said you need to have SSH already working on the two computers.
- Copy a file to the remote computer using the example command below.
scp /file/path remote_username@destination-IP:/destination/path
- Copy a file from a remote computer to a local(current) computer.
scp username@source-IP:/file_path /local/destination/path
Example
scp [email protected]:/home/itnixpro/static-OpenVPN.key /home/itnixpro/
- Note that if your SSH is configured to use another port other than the default port 22, specify the port used by adding the -P option.
scp -P 32456 username@sorce-IP:/file_path /local_destination_path
- To copy the directory with files, add the -r option as in the example below.
scp -r /file/path remote_username@destination-IP:/destination/path
How to copy a file in Linux using RSYNC command
- RSYNC command is an advanced version of CP and SCP combined. Copy file from the current directory to another and add -v option to show progress.
rsync -v demofile.txt /home/itnixpro/Downloads
- Copy all files from one directory to another by adding -a option.
rsync -a /home/itnixpro/Downloads/ /home/itnixpro/Documents/
- Copy the file to a remote computer using the command.
rsync -av /file/path remote_username@destination-IP:/destination/path
- To sync files between two remote computers add the -q option.
rsync -q file_path/ remote_username@destination-IP:/destination/path
- That concludes our article on how to copy a file in Linux. Enjoy!
Read more on RSYNC Documentation
Other Tutorials
Install Deepin DE Desktop Environment on Ubuntu 22.04