Rename files or folders under Linux (MV command with rename command)

Source: Internet
Author: User

To rename a file or directory under Linux, you can use the MV command or the rename command

Mv

————————————

The MV command can be renamed or moved to a file or folder.

Example: Renaming directory A to B

MV A B

Example: Move the/a directory to/b and rename to C

mv/a/b/c

In fact, in the text mode to rename the file or directory, just need to use the MV command, for example, to rename a file named ABC to 1234:

MV ABC 1234

Note that if there is also a 1234 file in the current directory, the file will be overwritten.

Rename

————————————

Batch create file names that are similar in size. "The first step in testing"

[[email protected] net]# for I in ' seq-w 10 '
> Do touch stu\_$i\_linux.jpg
> Done
[Email protected] net]# SL
Total 0
-rw-r--r--1 root root 0 Oct 9 21:22 stu_01_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_02_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_03_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_04_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_05_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_06_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_07_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_08_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_09_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_10_linux.jpg

Then, use Rename to modify

[Email protected] net]# rename \_linux ' *.jpg
[Email protected] net]# SL
Total 0
-rw-r--r--1 root root 0 Oct 9 21:22 stu_01.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_02.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_03.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_04.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_05.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_06.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_07.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_08.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_09.jpg
-rw-r--r--1 root root 0 Oct 9 21:22 stu_10.jpg

Note that if you want to replace the dash, you cannot add any quotation marks.

Create some test files:

[[email protected] net]# for I in ' seq-w 10 '; Do touch stu\_$i\_linux.jpg; Done
[Email protected] net]# SL
Total 0
-rw-r--r--1 root root 0 Oct 9 21:58 stu_01_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_02_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_03_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_04_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_05_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_06_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_07_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_08_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_09_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:58 stu_10_linux.jpg
[Email protected] net]# rename ' \_linux ' *.jpg
[Email protected] net]# SL
Total 0
-rw-r--r--1 root root 0 Oct 9 21:27 stu_01_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_02_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_03_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_04_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_05_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_06_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_07_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_08_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_09_linux.jpg
-rw-r--r--1 root root 0 Oct 9 21:27 stu_10_linux.jpg

Rename version If it is a Perl version, the Rename command is supported by regular, so in the Perl version of the Rename, the execution of the above command can be successful, if the rename version is version C, then the above command is unsuccessful.

This is a version support issue, so don't delve into it. For a comparison of the two versions, refer to the: Linux under rename command in bulk Rename in detail.

The previous problem may be due to the fact that his rename version is not supported. So, the point here is rename, although this command is not very important. But it's actually very useful, isn't it?

Of course, some people will say, you this is in a directory, then I in a different directory?
With two commands. A find first to modify the file to find out, then a pipeline, a rename is not finished?
Yes, the above approach can be said to be a way of thinking, in fact there are many kinds of ideas.

Attached, view the rename version of the information

[[email protected] net]# Man Rename | Head-1
RENAME (1) Linux Programmer ' s Manual RENAME (1)

Rename files or folders under Linux (MV command with rename command)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.