Home Linux Copy (and paste) files and folders from the Linux command line

Copy (and paste) files and folders from the Linux command line


Copying and pasting files is one of the maximum primary matters you can do for your computer. On Linux, you have numerous options to accomplish this. at the command line, the whole thing happens greater at once, supplying you with more manipulate and in a few cases, simplifying matters.

Copy a single file

Whenever you want to copy a file or directory in the Linux command line, you will use the cp command cp stands for copy. The syntax is also simple. Use cp followed by the file you want to copy and the destination where you want to reach the copy.

cp your-file.txt ~/Documents/

Of course, the above command assumes that your file is in the same directory you are working on. You can specify both the original file location and where you want to place the copy.

cp ~/Downloads/your-file.txt ~/Documents/

You also have the option to rename your file while copying it. Specify a new name at the destination.

cp ~/Downloads/your-file.txt ~/Documents/new-name.txt

Refer to the article: How to copy and rename files in Linux for more details.

Copy a directory and its content

To copy a directory and its contents, you will need to issue cp to copy. This is quite simple with the -r flag .

cp -r ~/Downloads/pictures-directory ~/Pictures/family-vacation-pics

All the rest of the syntax is identical. Flag -r is used to inform cp that it is working with a directory and must copy its contents.

Copy multiple files

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

cp ~/Downloads/{file1.txt,file2.jpg,file3.odt} ~/Documents/

All 3 files are in different file types and will be copied into the Documents folder .

Copy all files of the same type

If you have a lot of files of the same type to copy, you can use wildcards * . This asterisk or wildcard requires the Linux command line to take care of everything placed there. So if you tell Linux to copy * .jpg , it will copy all JPG files, regardless of the name or anything that appears before the .jpg extension .

cp ~/Downloads/*.jpg ~/Pictures/

If you want to use a variety of files, assume JPG and PNG, you can use curly braces {} .

cp ~/Downloads/*.{jpg,png} ~/Pictures/

Move a file or folder

If you’re looking to move a file from one place to another, but don’t make a copy, you can do it easily, but moving a file requires using the mv command The syntax is very similar to cp.

mv ~/Downloads/your-file.txt ~/Documents/

Similarly, you can also rename it.

mv ~/Downloads/your-file.txt ~/Documents/renamed.txt

There is a big difference between cp and mv. You do not need the -r flag to move the entire directory.

mv ~/Downloads/downloaded-folder ~/Pictures/vacation-pics

that is all it takes to replicate (and paste) files and folders from the Linux command line. you’re equipped to start copying and shifting your files from the command line! you may find that using the command line may be very powerful in a few situations. but, in other situations, using the GUI can be less difficult

5/5 - (1 vote)

Related Tips And Tricks

Leave a Comment

Tips and Tricks

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept