Hackers Choice

How to Copy (and Paste) Files and Directories from Linux Command Line

How to Copy (and Paste) Files and Directories from Linux Command Line

By Yashdeep Raj •  2019-06-28T14:17:02.557Z •  Unix & Linux


Copying and pasting files is one of the most basic things you can do on a computer. On Linux, you have several options to get the job done. On the command line things are more direct, giving you more control, and in some cases, simplifying things dramatically.

Copy a Single File

Linux Cli Copy Document

Any time you want to copy a file or folder in the Linux command line, you’re going to use the cp command. cp is shorthand for copy. The syntax is simple, too. Use cp followed by the file you want to copy and the destination where you want it moved.

That, of course, assumes that your file is in the same directory you’re working out of. You can specify both.

You also have the option of renaming your file while copying it. Specify the new name in the destination.

Copy a Folder and Its Contents

Linux Cli Copy Folder

In order to copy a folder and its contents, you’re going to need to tell the cp command to copy recursively. That’s simple enough with the -r flag.

All the rest of your syntax is exactly the same. The -r flag serves to tell cp that it’s working with a directory and should copy its contents.

Copy Multiple Files

Linux Cli Copy Multiple

You can also copy multiple files. The Linux command line lets you target multiple items at once with brackets {}. You can use them to list the names of each file to be copied separated by commas.

All three files of differing file types will be copied to the Documents directory.

Copy All Files of the Same Type

Linux Cli Copy All File Type

If you have a ton of files of the same type to copy, you can use the wildcard character *. The asterisk/wildcard tells the Linux command line to accept absolutely anything in that place. So, if you tell Linux to copy *.jpg, it’ll copy all JPG files, regardless of the name or whatever comes before the .jpg part.

If you want to use multiple file types, say JPG and PNG, you can use the brackets from before.

Move a File or Folder

If you came here looking to move a file from one place to another without making a duplicate, you can do that easily too, but moving a file requires the mv command. The syntax is very similar to cp.

Similarly, you can also rename it.

There is one major difference, though. You don’t need the -r flag to move a whole folder.

That’s all there really is to it. You’re ready to start copying and moving your files from the command line. You can see that the command line way can be very efficient in some situations. In others, though, it might just be easier to use the GUI. Over time working with Linux, you’ll get used to picking which is best.

Powered by Froala Editor

Yashdeep Raj
 You may also like
Download our apps
Get it on Google Play