+4 votes
561 views
How to use MV command in Linux

in Linux / Unix by (551k points)
reopened | 561 views

1 Answer

+5 votes
Best answer

1. How to use the MV command on Linux
2. How to prevent the mv command from overwriting an existing Linux object
3. How to remove slashes of a Linux object with MV command
4. Allow the mv command to manage the destination as a normal Linux file
5. Have the MV command generate details of the task performed Linux
6. Create a backup of existing files with MV Linux command

Linux is one of the safest operating systems on the market, but it is true that its use can be more complex than Windows or Mac. The use of commands in Linux is a natural task that executes type of users and although it may seem something really complex not It will be so with a good knowledge of the functionality of each of them..

For this reason, we will explain one of the most practical commands for management tasks, such as moving elements in the system safely and efficiently. We are referring to the mv command.

What is the MV command
MV (Move - Move), its name is clear for the task it performs within Linux and is to move directories or files, but in some cases it helps us to rename some of these, so when you use the mv command remember that you can Do the next:
  • Rename a file or folder in the system
  • Move the group of files to a different directory than the original

MV is a command that is executed without confirmation, so we must be sure that files or directories have to be selected..

Syntax
The usage syntax of the mv command is as follows:
  • mv [OPTION] ... [-T] Destination resource
  • mv [OPTION] ... Resource ... Directory
  • mv [OPTION] ... -t Resource Directory

There are some parameters that we can use with the MV command, these are:

This parameter forces moving by overwriting the target file without asking first
 mv -f 

It is an interactive message before overwriting
 mv -i 

This parameter allows you to move an element when the origin is newer than the destination, otherwise it does not
 mv -u Update 

This option prints the source and destination files
 mv -v verbose 

Access the MV help manual
 man mv 

1. How to use the MV command on Linux


For this purpose we will use Ubuntu 19 and the first example we will give will be to rename a file in the movement process.
Step 1

For this we have a file called TechnoWikis1.docx and it will be renamed TechnoWikis2.docx, we execute the following:
 mv TechnoWikis1.docx TechnoWikis2.docx 
When executing the ls command we can see that the file has been edited:

image

Step 2

Now, if we want to change the path of that file to a new one, we must enter this path followed by the name of the file, in this case we will move a file to Documents, for this we execute the following:
 mv TechnoWikis2.docx / home / TechnoWikis / Documents 
image
Launch overwrite message before moving
We have mentioned at the beginning that the mv command is executed without requesting permission to overwrite the user, but if for security we prefer this to be deployed to guarantee the integrity of the object, we must add the -i parameter as follows:
 mv -i TechnoWikis2.docx / home / TechnoWikis / Documents 
There a message will be displayed requesting whether or not we authorize the destination file to be replaced by this new file or directory: image

2. How to prevent the mv command from overwriting an existing Linux object


If to add more security we do not want the file to be replaced, replace another one in the destination path, we must add the -ny parameter and thus no action is taken:
 mv -n TechnoWikis2.docx / home / TechnoWikis / Documents 
We can see that the TechnoWikis2.docx file will still be available on the desktop:

image


3. How to remove slashes of a Linux object with MV command


In some cases it is possible to eliminate the slashes located in the final part of the arguments, to do this we must use the --strip-trailing-slashes option:
 mv --strip-trailing-slashes TechnoWikis2.docx / home / TechnoWikis / Documents 

image

Although this option is not very common, its use is when an origin argument contains a slash located at the end which can specify a symbolic link to a directory.


4. Allow the mv command to manage the destination as a normal Linux file


This value allows the destination to be managed as a file but not as a directory, for this we have the -T option:
 mv -T TechnoWikis1.docx / home / TechnoWikis / Documents 
Allow the MV command to move the file only on the condition that it is newer than the target file. It is a parameter that allows a file to be moved only when the source file is older than this, for this we execute the following:
 mv -u TechnoWikis2.docx / home / TechnoWikis / Documents 

image


5. Have the MV command generate details of the task performed Linux


To access detailed information on the movement of the elements in Linux with MV we can use the -v parameter:
 mv -v TechnoWikis1.docx / home / TechnoWikis / Documents 

image


6. Create a backup of existing files with MV Linux command


An ideal security point of the MV command is the possibility of creating a backup copy of the file to be moved, this will be created with the same name of the file in the destination only that its difference will be a tilde (~), for this we must use the parameter -b:
 mv -b TechnoWikis1.docx / home / TechnoWikis / Documents 

image

We can use the ls command to view the created copies:

image

Thanks to the MV command we can freely move objects in Linux, as well as modify their name if necessary, and take much more precise control over them..


by (3.5m points)
edited

Related questions

+3 votes
1 answer
asked Oct 20, 2020 in Linux / Unix by backtothefuture (551k points) | 220 views
+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (551k points) | 311 views
+3 votes
1 answer
asked Nov 9, 2019 in Linux / Unix by backtothefuture (551k points) | 289 views
+5 votes
1 answer
asked Oct 24, 2019 in Linux / Unix by backtothefuture (551k points) | 213 views
+5 votes
1 answer
asked Sep 19, 2019 in Linux / Unix by backtothefuture (551k points) | 297 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users