Many of us if not the largest parte we are used to using a graphical interface or desktop environment so to speak. The tasks of moving, editing, rename among other things files or folders they are usually done in a simple way with just a couple of clicks.
However what happens when you have to use these movements on a server since most of them are usually managed only from a command console, this is usually occupied in dedicated servers, although it never hurts to know how this is done for you never know when it can be busy.
It has happened to me that on some occasions I have lost my graphical environment and I have to use the console to recover it, but that is another point.
Day today I come to share with you some simple commands that will help us to perform the tasks of copying or moving files.
How to move files or folders in Linux?
The first thing will be to have a terminal which will be our tool that will help us with all this, the second thing is to create some folders with text documents inside this in order not to damage or lose information.
The most usual thing is to move a directory file for this we are going to use the mv command:
mv archivo.txt /home/usuario/Documentos/prueba
Here what we are doing is moving file.txt to the test folder that is in our documents folder. For this we take into account that we are currently positioned in the directory where file.txt is located
When we want to move more than one file at a time, the syntax type would be the following:
mv archivo.1 archivo.2 archivo.3 /ruta/de/destino
Now something very useful is to use a * when files have the same base in name, for example:
Amd-gpu…
Amd-gpu-pro ..
Amd-driver ...
So, as we can see, they have the same "AMD" base to move all those files with that same base nomenclature, we do the following:
mv AMD* /ruta/de/destino
The same applies for all those files with the same type, for example, .doc, .xls, .deb, .rpm etc. To move them we only apply
mv *.deb /ruta/de/destino
Up to this point it is a bit clear how it works and how we can facilitate the task in various ways, but what happens when we want to move everything in a directory, both files and subfolders.
For this we are going to use the *, for example, I want to move everything that I decompressed from wordpress to two previous directories:
mv wordpress/* …/
To know a little more about the command we can use its man or with the -help parameter, here we will see all its parameters.
How to copy files in Linux?
For this case it is almost similar unlike that, to move files or folders from one to another, here keep files and folders in their place of origin and create a copy in a chosen directory.
Un simple command to copy a file or folder from one directory to another:
cp objetoacopiar rutadedestino
A more explicit way of looking at it:
cp archivo.txt /ruta/de/destino
This command is usually used a lot to make backups of a file or folder that is going to be edited, since it creates a total copy, but with a different name, a practical example:
cp log.txt log.bak
For copy multiple files or folders:
cp archivo1 /carpeta1 /carpeta/carpeta /ruta/de/destino
Now if we want to copy everything that contains the folder where we are positioned to another directory:
cp /* /ruta/de/destino
Now if we want to copy a directory from one place to another
cp /directorio /ruta/de/destino
It is important to be one level below the directory that we are going to copy, because if we are inside it it is necessary to specify the full path, since if we only put the command in the way I put it, it will only create an empty directory.
Finally, if we want to know all its parameters, we rely on its man or with –help
Without further ado, they are extremely basic commands, their use can help you a lot and you should even be careful with them since it is always recommended to use the recursive form, which is used with the -r parameter.
If I want to copy all the files from one folder to another it would be then
cp / * / name / folder / destination ??
standing in the folder where I have the files to copy?
I want to copy a specific number of records from the source-file to the destination-file, sometimes it is a record-from record-to range, how can I do this?