Hello fellow Linux enthusiasts. Hope you’re all well and ready to broaden your knowledge. Today’s topic will be about how to copy files and folders in Linux. And no, it’s with “right-click and copy file”. We do this the cool way, the terminal way.
Just kidding… without much further delay let’s get our hands dirty.
We’ll be covering several different methods and tools we can utilize that will allow us to perfom the copy actions for various scenarios and use cases.
Copy files and folders in Linux
Copy files using cp command
Basic format of cp command looks like this:
cp [option] [source] [target]
Quite simple eh? It sure is. So, our basic example of cp command would look like:
cp sample.txt copied_sample.txt
As you can see, copied file is in the same folder, so we actually made a clone. Well that makes sense, since we didn’t specified where would we like to place copied file. Now that we have a formula for cloning, we can make a significant number of clones, raise an army and finally rule the galaxy… Huh? Oh, disregard what’ve you just read.
Enpugh with the jokes. Where were we? Oh yes, location! You’re probably wondering is there a way to make a clone and send it directly to some location? Well, it sure is.
It is enough to just add location as last part of command. In our case, it was omitted, so system interpreted that we want our file to be copied in same location as original. Like I mentioned, to send a clone through portal, you’ll add location to command:
cp sample.txt ../test2
As you can see, our sample.txt is successfully copied to another folder. Of course, there is no restriction, we can copy file fo another location and change its name.
cp sample.txt ../test2/sample_gone.txt
You can also copy multiple files at the same time. Syntax is just expanded for another file that is being copied:
cp index.html index.txt ~/test
But let’s be honest, typing all that file names is troublesome, so it’s easier to just copy complete folder or in case that just some of files are needed to be copied that you find a way of grouping them, which will shorten your command.
There are additional options that can be used with cp command and some of those are: • -b backup • -i interactive • -r recursive • -u update • -v verbose etc. (to list all additional options, type man cp)
You have probably recognized some of options already, since we had introductory with those not long ago, in an article about LINK deleting files and folders.
As a quick reminder, with -i (interactive) you’ll be prompted for confirmation (no matter how pesky can it be with “are you sure you want to overwrite?” questions, it’s still highly advisable, specially when working with a vast number of important files), -v (verbose) will inform you of progress of copying multiple files.
Option –r or –R (recursive) will make your functions recursive in a given programming language. Hah, gotcha! Just checking if you’re paying attention. What –r actually does is copying all files and subfolders to a destination folder.
Options we haven’t had mentioned before are –b (backup) and –u (update) and they are quite handy. Backup option, as its name indicates, makes a backup of your file of choice.
Previous file gets tilde (~) after its name, so you can differentiate it from current version. In cases where you keep your copies on a several different locations, -u option comes really handy when you need to update those files.
As you can file is successfully updated. For rest of options consult manual page or shortly man page.
Copy folders using cp command
Synthax for copying folders is not much different than with copying files to a separate folder. So to copy folder test to folder test2, you just need to type:
cp –r test test2
Of course, my recommendation when it comes to folders would be that you use path instead of just a folder name. In other words, our previous example would look like this:
cp –r ~/test ~/test2
This is just to be sure in case that you have files and folders with the same names. Of course, option –r is added for files and subfolders. Pretty much the same as it was with files.
And with that we can end this article. Till next time stay sa…whoa! I almost forgot. There is another command you can use to copy files and folders in Linux. It’s rsync (remote sync).
Copy files using rsync command
As its name suggests, files (and for that matter, folders too) can be copied remotely as well as locally, all combinations are available. If you’re on Ubuntu 20.04 or higher, rsync comes preinstalled, while others will just have to run a simple command to install it:
sudo apt install rsync
Rsync syntax is identical as cp’s and it basically looks like:
rsync [option] [source] [target]
Looks quite familiar, eh? Well it certainly is. Even some of options are same (-r for recursive or –v for verbose). Besides –r and –v, most commonly used options are **–**a (archive) and –z (compress data). For rest…yeah, you guessed it right – consult man page, tee-hee. Back on track, let’s see examples of usage of rsync command:
rsync –v index.html ~/test2
Part with changing name in destination also applies here. Even the part to copy files remotely isn’t too much different and complicated. Logicaly, besides destination you have to add user and ip address where are you sending file to.
rsync [option] [user@sourcehost:path] [user@targethost:target/path]
or in our case:
rsync –v index.php [email protected]:~/testing
Instead of this IP, you type IP address of your remote machine, and since I copied file from local source, user and source host were omitted.
Let’s see was file successfully copied.
Yup, there it is. Of course, copying from remote to local folder is the same, just the order is reversed.
Copy folders using rsync command
No differencies here. Syntax is also identical to cp’s.
rsync –rv ~/test ~/test2
I added –v just because I really enjoy watching detailed report and you already know that –r is a must when you copy folders. Same is with remote copying.
rsync –rv ~/test2 [email protected]:~/testing
And on remote computer there is copy of test2 folder with its subfolders and files.
As we can see, there it is, with all of its content.
Most likely you’re now wondering which command to use.
It absolutely has no matter which you choose, you can’t make mistake. I’ll just mention that cp is mostly used for copying files and folders within the machine and rsync is mostly used for copying to a remote destination. It depends on the use case.
BONUS (Honorable mentions)
Copy files with the SCP command
There is another command to copy files and folders in Linux remotely. That command is scp (secure copy protocol). It’s syntax is not much different than cp’s or rsync’s.
scp [option] [source] [target] for local files or folders or scp [option] [user@sourcehost:path] [user@targethost:target/path] for remote files and folders or in our particular case:
scp ~/scptest/scpfile.txt [email protected]:/~testing
And on the destination machine we can see that file is successfully sent.
Of course, again we can see some old acquaintances in options, like –r for recursive or –v for verbose. For full list of options, consult man page. And be sure you use –v just once, to see how much information you’ll get about what it’s happening behind the screen (I couldn’t encompass about two screens of info).
It uses same security and authentication as it is used in SSH (Secure Shell), hence the ‘secure’ part in its name. So the question arises – why have I left out this command in a regular part of article and put it in bonus section? Well, main reason is because this command is deprecated.
While it’s good to use when you need to copy one file (like SSH keys) on remote server or combination of tar and compression of smaller data sets, but don’t think to use it if you need to copy large volumes (like database backups or media folders).
For those, you’ll use rsync command and if you want that transfer to be secure you’ll use it over SSH (rsync –rsh). Why? Because rsync proved to be a bit faster and, more importantly, it can continue interrupted session (pay attention to –P option).
Move files with the MV command
Some of you probably asked “OK, so you showed us copy commands, but is there a command to cut (and paste) instead of just making copies?”. Well, there is. It’s mv (move) command and he basic syntax looks like this:
mv [option] [source] [destination]
or in our test case:
mv index.php ~/test2
Yeah, since I was already in source folder, I could omit specifying a source. Also, I omitted options because I’ll be presenting this command in next article, so you’ll see why is that. There are our well known acquaintances: -b for backup, -i for interactive, -u for update, -v for verbose etc.
With that we came to an end of this somewhat longer than usual article where we covered various tools and methods how to copy files and folders in Linux.
Thank you very much for your time…