On this information, we are going to study copying information and directories in Linux.
Stipulations:
To carry out the steps which might be demonstrated on this information, you want the next parts:
Copying Information and Directories
Copying is likely one of the most elementary operations that you must carry out when working a pc. In Linux, there are a number of instruments which might be obtainable for copying information and directories:
- cp: The default instrument for copying information and directories. It’s obtainable on all Linux distros.
- rsync: A robust instrument that’s primarily used for file synchronization. Nonetheless, we will additionally use it for copying information and directories.
For demonstration functions, we configured two directories: /tmp/supply and /tmp/vacation spot.
Copying Information and Directories Utilizing Cp
The command construction of cp is as follows:
$ cp <choices> <supply> <vacation spot>
For all of the obtainable choices, take a look at the person web page:
Copying a File
The next command copies the “1.txt” from /tmp/supply to /tmp/vacation spot:
$ cp -v /tmp/supply/1.txt /tmp/vacation spot
For those who specify a distinct file identify because the vacation spot, cp renames it accordingly:
$ cp -v /tmp/supply/1.txt /tmp/vacation spot/take a look at.txt
Copying A number of Information
The next command copies all of the textual content information below /tmp/supply to /tmp/vacation spot:
$ cp -v /tmp/supply/1.txt /tmp/supply/2.txt /tmp/supply/3.txt /tmp/supply/4.txt /tmp/supply/5.txt /tmp/vacation spot
If the supply information are named in a sample, cp can work with that:
$ cp -v /tmp/supply/*.txt /tmp/vacation spot
Copying a Listing
Within the subsequent instance, we are going to copy the “subdir1” to /tmp/vacation spot:
$ cp -v -r /tmp/supply/subdir1 /tmp/vacation spot
Right here, the “-r” flag tells the cp command to repeat the listing and all its content material recursively to the vacation spot.
If a distinct listing identify is specified within the vacation spot, cp renames it accordingly:
$ cp -v -r /tmp/supply/subdir1 /tmp/vacation spot/yellow
If the vacation spot listing already exists, cp copies the supply listing and all the things it accommodates. Nonetheless, we will specify to solely copy the information and sub-directories, not the goal listing:
$ cp -v -rT /tmp/supply/subdir1 /tmp/vacation spot/yellow
Copying Information and Directories Utilizing Rsync
The first utilization of rsync is synchronizing the information between the native/distant servers. It comes with quite a few further options. Nonetheless, we will additionally use it for “syncing” information from one listing to a different (copying in different phrases).
The rsync command construction is as follows:
$ rsync <possibility> <supply> <vacation spot>
Try the person web page for all of the obtainable choices:
Copying Information
The next command copies the “1.txt” from /tmp/supply to /tmp/vacation spot:
$ rsync -a /tmp/supply/1.txt /tmp/vacation spot && tree /tmp/vacation spot
Right here, the “-a” parameter tells rsync to function in archive mode.
We are able to additionally copy the file with a distinct identify within the vacation spot:
$ rsync -a -v /tmp/supply/1.txt /tmp/vacation spot/completely different.txt && tree /tmp/vacation spot
To repeat a number of information, specify the information one after the other or describe the sample to match:
$ rsync -a -v /tmp/supply/1.txt /tmp/supply/2.txt /tmp/vacation spot && tree /tmp/vacation spot
$ rsync -a -v /tmp/supply/*.txt /tmp/vacation spot && tree /tmp/vacation spot
Copying Directories
Not like cp, to repeat the directories with its information and sub-directories, there’s no change of syntax with rsync. When working in archive mode, rsync routinely copies all of the contents recursively.
One key distinction right here is the trailing slash (/).
- If a slash is current within the supply listing, rsync copies the contents of the supply listing to the vacation spot.
- If a slash isn’t current within the supply listing, rsync copies the supply listing contained in the vacation spot listing.
The next instructions show the distinction completely:
$ rsync -a -v /tmp/supply /tmp/vacation spot
$ rsync -a -v /tmp/supply/ /tmp/vacation spot
Copying Information to Distant Machine Utilizing Scp
The scp command is a specialised instrument that copies the information and directories over SSH. It requires having the OpenSSH server put in on the distant host.
The basics of the scp command are the identical because the cp command. Study extra in regards to the scp command.
Copying Information
Within the following command, we copy the Ubuntu 22.04 ISO to the /tmp listing on a distant host:
$ scp ubuntu-22.04-desktop-amd64.iso root@192.168.99.15:/tmp
To repeat a file from a distant host, use the next command:
$ scp root@192.168.99.15:/tmp/ubuntu-22.04-desktop-amd64.iso.
Copying Directories
To repeat a listing to a distant host, we use the next scp command:
$ scp -r test_dir root@192.168.99.15:/tmp
Right here, the “–r” parameter is to function in recursive mode (wanted to repeat the listing).
Conclusion
We showcased the assorted methods of copying the information in Linux. We demonstrated how you can use the cp and the rsync instructions to repeat the information and directories domestically. We additionally showcased how you can use the scp command to repeat the information and directories to a distant host.
For extra superior copying and backup configurations, rsync is a greater possibility. It could actually make a system backup, sync with a number of distant hosts, replace new information, and extra.