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
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's all it takes to copy (and paste) files and folders from the Linux command line. You 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.
You should read it
- How to check whether a Linux PC is 64-bit or 32-bit by command line
- 5 reasons why people love the Linux command line
- How to print from the Linux command line
- How to manage Linux services using Systemd
- Basic Linux commands everyone needs to know
- Instructions for using find command in Linux
- How to find the MAC address using the command line in Linux
- How to use the which command in Linux
May be interested
- How to use the Install command to copy files in Linuxinstall is a flexible file copying command in linux and macos suitable for professional users. read this article to discover how to use the install command.
- How to copy files from Mac to USBwhen copying files from macos to a usb drive with normal copy paste or drag and drop files, the process will not work properly as on a windows computer. in the article below, tipsmake will guide you how to copy files from a mac to a usb drive.
- Fixing errors cannot Copy Paste in Windowscopy paste is one of the most basic and handy functions in windows. if you can't copy and paste on windows, follow these steps.
- Search for files and directories in Linux using the command line interfaceyou can use the graphical file management utility to find files in linux like nautilus in gnome, dolphin in kde and thunar in xfce. however, there are several ways to use the command line to find files in any linux desktop management utility.
- 12 things Linux is easier to do in the command line than graphical softwaregraphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
- Moving files between Linux systems with SCPwhen transferring files to a remote linux server, you have a few options. one of the best is to use a program called secure copy, or scp, which runs over the ssh protocol to quickly transfer files over the network to the remote system.
- Xcopy command in Windowsthe xcopy command is a command prompt command used to copy one or more files or folders from one location to another. with many options and the ability to copy entire directories, the xcopy command is similar to, but much more powerful, than the copy command.
- How to exclude files and folders with Robocopy in Windows 10on windows 10, robocopy is a powerful command-line file management tool. in this guide, you'll learn the steps to copy data with robocopy on windows 10, except for unnecessary files and folders.
- How to copy on the computer, copy and paste website contentin today's article, let's learn with taimienphi how to copy and paste text, files and images on windows and mac computers! the method is extremely simple and quick.
- How to delete files and folders using Linux command linein linux, the rm command is used to delete files and directories.