MV command in Linux

Anonim

MV command in Linux

Syntax

MV is one of the standard Distributions based on the Linux kernel. Each user who wants to explore the basic terminal commands will be known about her to study any necessary actions through the console. This utility allows you to rename directory and individual objects, as well as move them. Of course, the same actions can be implemented through the graphical interface, but it does not always have access to it or it is necessary to make the task through the "Terminal", without being distracted by the environment of the desktop. Enable the MV command in the console is very simple, since its syntax is not difficult, and the available options can be leaning literally in a few minutes, only looking at them. However, we still refund a separate attention to the rules of input and the arguments present, so that even novice users do not have any questions on this topic. We propose from the syntax, namely, with the rules for drawing a line of action in the console.

As you know, the programming syntax is responsible for the rules for entering words when drawing up one or more requests. Not bypassed this rule and the team considered today. From the string sequences and depends, whether the user requires correctly. The correctness of writing looks like this: MV + options + source_ files + place_name. Let's consider each fragment in more detail so that you can understand his role:

  • MV - respectively, the challenge of the utility itself. It is always the beginning of the line, except for the installation of the sudo argument responsible for the execution of the command on behalf of the superuser. Then the string acquires the type of sudo mv + options + source_files + place_name.
  • Options are installed additional tasks, such as backup, rewrite files and other actions that we will talk about in a separate section of today's material.
  • Source_Files - those objects or directories with which you want to make action, for example, rename or move.
  • The location_nation is indicated when the objects are moved, and if renaming, the new name is indicated.

These are all input rules that need to be remembered. There are no more features, so you can proceed to the analysis of available options.

Options

You already know that options are additional arguments in the form of letters that are specified if necessary for the work of a team of additional actions. Almost all commands existing in Linux can be performed with one or more options, which also applies to MV. Its opportunities are aimed at the following tasks:

  • -Help - Displays the official documentation about the utility. It will be useful if you have forgotten other options and want to quickly get a general summary.
  • -Version - Displays the MV version. It is almost never used by users, since the definition of the version of this tool is almost never required.
  • -B / -Backup / -Backup = method - creates a copy of files that have been moved or overwritten.
  • -f - when activated, will not ask the permission from the owner of the file, if it comes to moving or renaming the file.
  • -I - on the contrary, will ask permission from the owner.
  • -N - Disables the overwriting of existing objects.
  • -Strip-trailing-slashes - deletes the final symbol / from the file if available.
  • -t Directory - Moves all files to the specified directory.
  • -U - Moves only if the source file is new than the destination object.
  • -V - Displays information about each element during command processing.

In the future, you can use the above options to specify them in one bar during renaming or moving individual objects or directories. Next, we propose to deal in more detail with the most popular examples of interaction with the MV command who have stopped at all major actions.

Moving files and folders

From the above information you already know that the team under consideration is used to move files. To do this, you will need to run the "terminal" in a convenient way and write there MV MyFile1.txt MyDir /, replacing the specified file name and the final folder to the necessary. If the object is not in the current directory, you should register the full path to it, which we still talk next. The same can be performed with a separate folder.

Move the file to the specified folder through the MV command in Linux

Rename objects and directories

The second purpose of the MV console utility is to rename objects. This is also done through one command. Above, we promised to show how the operation is carried out indicating the full path. In this case, the string acquires the MV / HOME / LUMPICS view / desktop / test.txt test2.txt, where / home / lumpics / desktop / test.txt is the required location of the object, taking into account its name and expansion, and test2.txt - The name that will be assigned to him after the activation of the team.

Rename a file through the MV utility in Linux

If there is no desire to specify the full path to the object or directory, for example, when you need to make several actions in one session, it is recommended to move to the location by entering the CD command. After that, the full way to write is not needed.

Transition to the specified location to interact with the MV utility in Linux

After that, let's rename the folder via MV test1 Test, where test1 is the original name, and test1 is final.

Rename the folder using MV in Linux in the current folder

Immediately after clicking on the Enter key, you will see a new input string, which means that all changes have passed successfully. Now you can open a file manager or any other tool to check the new directory name.

Successful application of the MV command in Linux in the current location

Creating backup copies of objects

When familiarizing with the command options, it was possible to notice the -b argument. It is he who is responsible for creating backup copies. The correct decoration of the string looks like this: MV -B /test/test.txt test1.txt, where /test/test.txt is the immediate path to the file, and the test1.txt is the name for its backup.

Creating a backup copy of an existing file with the MV command in Linux

By default, backup objects at the end of their name have a symbol ~, respectively, the MV command also creates it automatically. If you want to change it, you should use the MV -B -S .txt string Test.txt test1.txt when creating a backup. Here instead of ".txt" write the optimal file extension for you.

Moving multiple files at the same time

Sometimes there is a need to move several files at once. With this task, the utility under consideration is perfectly coping. In the Terminal, you should enter only MV MyFile1 MyFile2 MyFile3 MyDir /, replacing the names of objects and the final folder to the necessary.

Simultaneous movement of multiple files through the MV utility in Linux

If the commands from the console are now activated from the directory where all files are located for moving, use MV * MyDir / to immediately transfer them all to the specified directory. So you will save a significant amount of time on alternately moving or manually entering the names of all objects.

Move all files from the current folder using the MV command in Linux

The same applies to elements with the same format. If there is a desire to move, for example, only images of the JPG type, you should change the line on the MV * .jpg MyDir. The same applies to all other well-known types of files.

Moving all files with the specified extension through the MV command in Linux

Moves missing in the target file directory

There are situations where a number of files must be moved to a specific directory, but some of them are already available in this directory. Then you need to use the -n option so that in the end the team has found the MV -N MYDIR1 / * MyDir2 /. Replace the specified folders here on the necessary to move correctly.

Moving non-existent files in the target file directory via MV in Linux

As you can see, the MV command can be used for different purposes and with certain arguments that allow without any problems to rename or move the object group or some particular file. If you are interested in interaction with other standard console utilities in Linux, we advise you to explore the materials on this topic using the links below.

See also:

Frequently used commands in "Terminal" Linux

LN / FIND / LS / GREP / PWD / PS / ECHO / Touch / DF command in Linux

Read more