linux rename file command

The mv command is a command similar to cp , but it is not a copy/copy of a file or directory. No matter what version of Linux system you are using, mv is installed by default on your Linux system. Let's take a look at some examples of mv command in daily operation.

[root@localhost tmp]# mv --help Usage: mv [OPTION]... [-T] SOURCE DEST or: mv [OPTION]... SOURCE... DIRECTORY or: mv [OPTION]... -t DIRECTORY SOURCE... Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too. --backup[=CONTROL] make a backup of each existing destination file -b like --backup but does not accept an argument -f, --force do not prompt before overwriting -i, --interactive prompt before overwrite -n, --no-clobber do not overwrite an existing file If you specify more than one of -i, -f, -n, only the final one takes effect. --strip-trailing-slashes remove any trailing slashes from each SOURCE argument -S, --suffix=SUFFIX override the usual backup suffix -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY -T, --no-target-directory treat DEST as a normal file -u, --update move only when the SOURCE file is newer than the destination file or when the destination file is missing -v, --verbose explain what is being done -Z, --context set SELinux security context of destination file to default type --help display this help and exit --version output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given) numbered, t make numbered backups existing, nil numbered if numbered backups exist, simple otherwise simple, never always make simple backups

GNU coreutils online help: http://www.gnu.org/software/coreutils/ For complete documentation, run: info coreutils 'mv invocation'

1. Move files

Note that when moving files, the source address and destination address of the file must be different . Here is an example. If you want to move the file_1.txt file from the current directory to another directory, take /home/pungki/ as an example, the syntax should be as follows:

  1. $ mv file_1.txt /home/pungki/office

mv command

As we can see, when we move the filetxt file, file1.txt in the previous directory is deleted .

2. Move multiple files

If we want to move multiple files at once, we can put them on one line and separate them with spaces.

  1. $ mv file_2.txt file_3.txt file_4.txt /home/pungki/office

Move multiple files

If your files are regular, then you can use wildcards. For example, in order to remove all files with a .txt extension, we can use the following command:

  1. $ mv *.txt /home/pungki/office

Move using pattern

3. Move the directory

Unlike the copy command, moving directories with the mv command is quite straightforward. To move directories, you can use the mv command without options. Look at the screenshot below to see it at a glance.

Moving directory

4. Rename the file or directory

We also use the mv command to rename files or directories. However, the target location and source location must be the same. Then the file name must be different.

Assume that our current directory is /home/pungki/Documents , and we want to rename file\ 1.txt to file* 2.txt* . Then the command should be as follows:

  1. $ mv file_1.txt file_2.txt

If it is an absolute path, it should look like this:

  1. $ mv /home/pungki/Documents/file_1.txt /home/pungki/Documents/file_2.txt

Renaming file

5. Rename the directory

The rules in the previous paragraph also apply to directories. Consider this example:

  1. $ mv directory_1/ directory_2/

6. Print mobile information

When you move or rename a lot of files or directories, you may want to know whether your own commands were successfully executed without going to the target location to check. This requires the -v option.

  1. $ mv -v *.txt /home/pungki/office

mv with verbose mode

This method also applies to directories.

mv directory with verbose mode

7. Use interactive mode

When you move a file to another location, and that location happens to have the same file, the mv command will overwrite the original file. There is generally no hint for this behavior of mv. If we want to generate a prompt about overwriting files, we can use the -i option. (Annotation: Usually the release version will pass the alias command and use -i as the default option, so there will be a prompt.)

Suppose we want to move file1.txt to /home/pungki/office. At the same time, there is already a file1.txt file in the /home/pungki/office directory.

  1. $ mv -i file_1.txt /home/pungki/office

mv with interactive mode

This prompt will let us know the existence of file_1.txt at the target location. If we press the y key, then that file will be deleted, otherwise it will not.

8. Use update options

The -i option will prompt us about overwriting the file, and -u will only perform the update when the source file is newer than the target file. Let's take a look at the following example:

Update only newer

If file1.txt and file2.txt have the following characteristics:

  1. File_1.txt has 84 bytes file size and it last modified time is12:00
  2. File_2.txt has 0 bytes file size and it last modified time is11:59

We want to move them to the /home/pungki/office directory. * But the target address already has file1.txt and file2.txt.

We use the following command to move file1.txt and file2.txt from the current directory to /home/pungki/office

  1. $ mv -uv *.txt /home/pungki/office

You can see that these files have been moved. These files can be moved because their most recent modification timestamp is newer than the files in the /home/pungki/office directory.

9. Do not overwrite any existing files

If the -i option asks if we want to overwrite the file, then the -n option will not allow us to overwrite any existing files.

Continuing to use the example in point 8, if we replace -u with -n and add the -v option, then we will see that no files have been moved to the /home/pungki/office directory.

  1. $ mv -vn *.txt /home/pungki/office

No overwrite

10. Create a backup while copying

By default, moving a file will overwrite the existing target file. But if we move the wrong file and the target file has been overwritten by the new file, what should we do? Is there a way to restore the previous files? The answer is yes . We can use the -b option. This option will back up the old file when the new file overwrites the old file. Here we also take point 8 as an example.

  1. $ mv -bv *.txt /home/pungki/office

Backup option

As you can see in the screenshot, a file named file\ 1.txt~ and file* 2.txt~ appeared in the /home/pungki/office directory . The tilde (~) means * These files are backup files. From their attributes, we can see that these files are older than file1.txt and file2.txt.

11. Overwrite existing files unconditionally

(Annotation: This section is supplemented by the translator, the original text omitted this important option)

When you want to overwrite an existing file or directory anyway, you can use the -f option. If both the -f option and the -i or -n option are specified at the same time, the -f option will overwrite them—that is, without any prompting, so you know what you are doing when using this parameter.

  1. $ mv -f *.txt /home/pungki/office

to sum up

Commands for moving files and directories are basic commands for Linux systems. Usually you can display mv's man page through man mv or mv --help for more detailed information.

The above is the Linux-related knowledge shared by Liangxu Tutorial Network for all friends.

Guess you like

Origin blog.csdn.net/manongxianfeng/article/details/113054628