The terminal in Linux is a very powerful tool if you know how it works, and you know how to use it. A big problem for all beginners is to know the basic commands, a very basic command that we should all know, is the command that allows us to copy files from the Terminal, and it is so simple that it is not necessary to write it down.
1. Copy files
The command we will use is cp. The format that we will be using is the name of the command, followed by a space and the absolute or relative path of the source file to be copied, and another space followed by the full path of the directory where the new file will be copied, including the name that you will have this, which may be different from the source file.
Now in the terminal:
cp /tmp/source-folder/old-file.bin /root/dest-folder/new-file.bin
With this small piece of code, a file is copied to another location, the only drawback is that we have to adjust the permissions after copying it, unless we want it for the same root user.⇧
2. Using wildcards for copy, and recursive copy
Sometimes we need to copy files based on their extension or filename, so using wildcards is best.
If we need to copy files in the first level or recursively from the current folder with the extension
Simple, only the first level:
cp *.js /home/user/dest-folder/
Recursive, all files with the previous extension are copied from the top level and in all subfolders:
cp -R *.js /home/user/dest-folder/
List of useful wildcards:
*refers to all content within the directory, including other directories, does not select hidden files and folders.
.refers to the current folder.
.*refers to all files and folders, including hidden ones.
*.*refers to all files that use the
3. Copy files and folders recursively
To copy all the content of the source folder to the destination folder, we use the
cp -R /tmp/source-folder /root/dest-folder
4. Copy contents of current folder to another folder, recursively
If we are inside a folder, and we want to copy all the files recursively to another folder, we use the wildcard "." and the parameter
cp -R . /root/folder/
For the above command to work, we must place ourselves with
cd in the directory that has the content that we want to copy.
cd /root/folder-with-content/ and from there execute the command that copies all the contents of the folder but without copying the parent folder.
5. Copy with relative paths
Normally when we copy files and folders, we do it with absolute paths, in other words complete paths, but we can also use relative paths to the directory in which we are. There are special cases in which this is needed, for example, if we download a zip file and unzip it in a directory, but its content was saved in a directory called directory-123-downloaded, which is the same name of the zip file, directory-123-downloaded.zip, then we need all the contents of the directory to be copied one directory back.
Relative paths in Linux are handled with a colon followed by
.. to indicate one level up and should not start with a slash. It may be the case to use the colon with absolute paths, but that is another matter. A single dot after the slash indicates that the contents of that folder will be used and not the folder itself. On the other hand, we can replace the colon with the wildcard
* to level up.
Example to recursively copy one level back and only the contents of the directory:
cp unzipped/. ../
It looks a bit strange, but it is quite simple and practical, you can even combine the two points with several levels, for example:
cp unzipped/. ../../../../
I just do not recommend it, because it's a bummer having to be counting the levels and calculating the positions. But for simple situations in which only one or two levels of difference are handled, there should be no problem.
Example to recursively copy one level back and two levels forward with wildcards and only the contents of that directory:
cp unzipped/. ../new-directory-incomp*/direc*y/
As we can see, we can complete the routes with wildcards, in one of the situations that helps us the most is when we have a folder with the name of a project followed by its version, and we do not know the version, for example, "php-7. *" .⇧