There are some big advantages to performing basic copy, move and delete functions through the Linux command line rather than a standard graphical user interface (GUI).
For starters, it's much faster and you can use it to automate file management operations with scripts or batch files.
But the real plus of using the Linux command line is that operations that would be difficult in a GUI window become far easier.
Today, I'm here to tell you how to copy, move and delete files from the Linux command line:
- Copy files (single and multiple) using the
- Move files (single and multiple) using the
- Delete files (single and multiple) with the
1. How to Copy Files Using the
is the Linux copy command. Using it is extremely straightforward, as it only requires two arguments.
cp source destination
However, just because using
to perform a basic copy function is simple, it doesn't mean there isn't plenty of room for customization. There are more than ten different modifiers which can be used by calling
in the following manner.
cp [option] source destination
Here are some of the Linux copy file modifiers you can use:
The archive modifier, it will preserve any copied files in their current state.
cp -f:The force option will ensure the copy is completed by overwriting identical files.
cp -i:This is the interactive option which will display an overwrite prompt for identical files.
cp -l:Creates a file that is hard linked to the source file, rather than a copy of it.
cp -L:This creates a soft link to the directory of the source file.
cp -n:This prevents overwriting of any identical files.
cp -p:Create a copy with the same modification and permissions as the original file.
cp -r:Perform a recursive copy that includes all folders and subdirectories of the source material.
cp -u:The update modification only copies the source file if it's more recent than the destination file.
cp -v:The v stands for verbose. This option will display update messages during the copy.
cp -r variant is the most important to remember. In a graphical interface, normally when a folder is copied, so are all subdirectories. Linux doesn't do this by default. So, to copy directory Linux, make sure you add the
command is also a versatile extension. It will produce a copy as close as possible to the original file that will include the same metadata and directory tree.
1.1 Copy Single Files with
If you want to Ubuntu copy file within your working directory, you can simply use the following line of code. In this case, I'll use
document.txt as my example file.
cp document.txt document-2.txt
This will create a copied version of your file named
document-2.txt which will be stored alongside the original in the working directory. However, there's no need for you to copy the file within the same directory.
cp /home/user/documents/document.txt ~/copied-documents/document.txt
In this case, the copied file will be placed in the
copied-documents folder. Note that I used the tilde shortcut here to refer to the home user directory. You can do this at any time in the Unix command terminal.
1.2 Copy Multiple Files with
There are two different ways
cp can be used to copy multiple files to the same directory. The most basic method is to simply list multiple files in the source section of the command and then provide a destination directory instead of a destination file name.
cp ~/document.txt ~/picture.jpg ~/copied-documents
In the example above, the document and picture file will both be copied to the
copied-documents folder from the home user root directory. If you are attempting to copy multiple files with similar names, you can use the wildcard asterisk modifier. For example, if you want to copy files named '
document-1.txt' and '
document-2.txt', you can do the following.
cp ~/document-*.txt ~/copied-documents
The * tells Unix to copy any text files in the home directory that has a name beginning with '
2. How to Move Files Using the
mv operates very similarly to
cp, requiring only two arguments. Self-evidently, instead of just creating a copy of the source file, it will delete the original and replicate it in a different directory.
mv source destination
Once again, it's possible to change the way in which files are moved with an optional modifier.
mv [option] source destination
The modifiers listed for
cp are also applicable for
mv. Remember, the command terminal will automatically list these for you if you call a help command.
However, there is one additional
mv modification worth knowing about. This is the
-b or 'backup' option, which will create a backup of the moved file in the source directory.
2.1 Move Single Files with
Once again, you will most commonly use the
mv command to transfer files from one directory to another. In the following snippet, you'll find a command to move an example text file.
mv ~/documents/document.txt ~/moved-documents/document.txt
Note that there's no need to keep the name of the file the same. While calling the
mv command, you can rename the moved file anything you like.
mv ~/documents/document.txt ~/moved-documents/moved-document.txt
The line above will move the directory of the text file as well as renaming it. And here's a tip, the Linux command line doesn't have a separate renaming function, so if you ever want to rename a file, just use the
mv command! For example, if I want to change the name of the
document text file, but not its location, I can simply remove the folders from the above line of code.
mv ~/document.txt ~/moved-document.txt
2.2 Move Multiple Files with
Just like the
cp command, there are two techniques you can use to move more than one file with
mv. The first, and most basic, is to simply add multiple source files to the command line.
mv ~/document.txt ~/picture.jpg ~/moved-documents
This will move both the document text file and the picture file to the
moved-documents folder. However, you can save yourself time by using the wild card operator.
mv ~/*.txt ~/text-files
This code will move every text file from the user's home folder into the
text-files folder. Note that if that folder doesn't exist already, this operation will create it.
3. How to Delete Files Using the
rm or 'remove' command is used to delete files from the Linux command line. If you paid attention to how to use the copy and move operators, you'll be on familiar ground here. However, while those functions require two input arguments, you only need one to perform a deletion operation.
Once again, you can use the same modification options I listed in section one with the
rm function. If you ever forget what they are, just ask in the command line for help!
3.1 Delete Single Files with
rm function to delete a file or directory is extremely simple. If, for example, we wanted to delete a file within a folder called
text-files, contained in the home user folder, this line of code would suffice.
rm can be used to delete directories too. If we wanted to remove the actual
text-files folder itself, we could run the following operation.
Note that when you call an
rm operation on a directory, by default, only the subdirectories and files within the directory will be removed. That makes the recursive
-r modifier one of the most useful
rm options to know about.
This line will not only delete the files contained in the
text-files directory, but will remove the directory itself, too.
3.2 Delete Multiple Files with
To delete more than one file with
rm, simply add extra files or folders as new arguments. The snippet below will delete both the document and picture files in the user home directory.
rm ~/document.txt ~/picture.jpg
You can also use the * wildcard operator to delete multiple files with the same name or file type. This example code will delete any text files within the
But beware! Never ever combine the recursive delete operation with the wildcard operator alone. The following command will delete every file and directory you have permission to modify.
rm -r *
How to Copy, Move and Delete Files in Linux - Conclusion
Now you know how to copy, move and delete files from the Linux command line. If you internalize these commands, you'll be able to perform file operations much faster and create batch commands or scripts.
Remember, if you ever forget how to use a particular operation, you can refer back to this guide or use the
man function to open the relevant part of the command line manual:
man [enter command you are unsure of here]
Was this Linux copy file walkthrough useful? If you want to learn about other commands, let me know by commenting below!
5 Best Linux Hosting Providers – The Finest Yo...
Linux is a powerful open-source operating system. When used for web hosting and servers, it can come in many shapes...
Debian vs Ubuntu: The Complete Rundown of Linux Serv...
Debian and Ubuntu are among the top Linux distributions available. Although Ubuntu is a way newer distro, both are equally...