In this tutorial, you will learn how to copy and move your files and directories in Linux using the terminal.
If you have ever had the need to use the Linux terminal to copy or move a file or directory in this tutorial, we will see how to copy and move folders or directories and files in Linux using the terminal. It is very simple and quick to do.
How to Copy from the terminal
To copy a file from one folder to another is very easy, the instruction to execute is as follows.
cp file_to_copy destination_folder
If we want to copy a folder or directory in Linux we will use the -r parameter:
cp -r folder_to_copy destination
The -r indicates that you copy the directory recursively, in this case, you can also use -R.
Examples of cp
In the examples we assume that we are located in the path of the file or directory that we are going to copy, otherwise, we would have to put its route.
If we want to copy the file test.txt to the folder/home/linuxcode:
cp test.txt /home/linuxcode
If we want to copy a folder called webLinuxcode to the var directory:
sudo cp -r webLinuxcode /var
Note that the var directory in Linux doesn't have permissions for the user, therefore we have to execute it as root, that's why we have put sudo, and the -r because webLinuxcode is a folder.
How to Move from the terminal
To move a file or directory we will use the mv command, the instruction is the following:
mv file_or_target folder
Unlike copying, we don't need any extra parameters to move a folder.
Examples of mv
In the examples we assume that we are located in the path of the file or directory that we are going to move, otherwise, we would have to put its route.
If we want to move the file test.txt to the folder/home /Linuxcode/Desktop, we use the following:
mv test.txt /home/Linuxcode/Desktop
And if we move a webLinuxcode call folder to the var directory:
sudo mv webLinuxcode /var
Like in copying, we use sudo.