Very often you may need to copy contents of one folder to another. In this article, we will look at how to copy files fro one directory to another.
How to Copy Files from One Directory to Another
Here are the steps to copy files from one directory to another. We will basically use cp command to copy files. Here is its syntax
cp source_path destination_path
Copy one or more files from one directory to another
Let us say you want to copy file1.txt from /home/ubuntu/ to /home/data
$ sudo cp /home/ubuntu/file1.txt /home/data
Here is the command to copy multiple files file1.txt, file2.txt and file3.txt from /home/ubuntu to /home/data. In this case you mention all file paths one after the other, with the destination path in the end.
$ sudo cp /home/ubuntu/file1.txt /home/ubuntu/file2.txt /home/ubuntu/file3.txt /home/data
Here is the command to copy all .pdf files from /home/ubuntu to /home/data. In this case, we use wildcard character * to identify all the files.
$ sudo cp /home/ubuntu/*.pdf /home/data
Copy Files & Folders from One Directory to Another
If you want to also copy folders & subfolders from one location to another then you need to use -R option for recursive copying. Here is an example to copy all files & folders present in /home/ubuntu to /home/data
$ sudo cp -R /home/ubuntu /home/data
Shell Script to Copy Files & Folders
Copying files & folders is a common task in system administration and it is always convenient to have a shell script for it. Here is a simple shell script to help you automate copying of files & folders from one location to another. Open terminal and run the following command to create an empty shell script
$ sudo vi copy_file.sh
Add the following lines to it.
#!/bin/bashsudo cp -R $1 $2
Save and close the file. In the above code, we accept source and destination paths as command line arguments and recursively copy all files & folders from source to destination. $1 stores the first argument, that is, source path, and $2 stores the second argument, that is, destination path.
Make the file executable.
$ sudo chmod +x copy_file.sh
You can run the above script by passing the source & destination as arguments. Here is an example to copy files & folders from /home/ubuntu to /home/data using the above script.
$ sudo ./copy_file.sh /home/ubuntu /home/data
If you want to run this script automatically every day, just create a cron job for it. Open crontab with the following command.
$ crontab -e
Add the following line to run the above shell script daily at 10 am. Modify it as per your requirement.
0 10 * * * sudo /home/copy_file.sh /home/ubuntu /home/data >/dev/null 2>&1
Save and close the file. In the above cronjob “0 10 * * *” means daily at 10 am. “sudo /home/copy_file.sh /home/ubuntu /home/data” is the command to be executed. Please use full paths while running shell script as cronjobs to avoid errors. Also use sudo to avoid permission denied errors. You may use online crontab generator to generate the above command as per your requirement.
That’s it. We have seen how to copy files & folders from one directory to another. We have also seen how to automate it using a shell script, and further run it periodically by using it as a cronjob.