How to copy and paste files and folders from the Linux command line

Copying and pasting files is one of the most basic things you can do on your computer. On Linux , you have several options to accomplish this. On the command line, everything takes place more directly, allowing you more control and in some cases, simplifying things significantly.

Copy a single file

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

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

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

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's all it takes to copy (and paste) files and folders from the Linux command lineYou are ready to start copying and moving your files from the command line! You may find that using the command line can be very effective in some situations. However, in other situations, using the GUI will be easier. After working with Linux for a while, you will find out what is the best option for you.
Hope you are succesful.
Related Posts

Related Posts

Post a Comment