The rename command under Linux

Source: Internet
Author: User

Dos/windows, rename the file with rename. And the book said, Linux under the file or directory renamed the MV. I've always done this, but I've overlooked Linux, which has a command called rename. It's all rename, but it's a bit different in function. The rename under Linux is more like a batch renaming tool, which is provided in the Util-linux suite.

First, basic functions
From the man document in the MV and rename commands, you can see the following information:

Reference

Mv-move (rename) files
Rename-rename files


In other words, the MV can also be used for renaming, but can not achieve batch processing (renaming, not support * and other symbols), and rename.
Format used by rename:

$ rename foo foo0 foo?


Rename needs to provide three parameters before the final result can be determined.
Simulate the example of a man document, the original file:

Reference

$ for I in ' seq ';d o touch foo$i;done
$ ls
Foo1 foo18 foo27 foo36 foo45 foo54 foo63 foo72 foo81 foo90
Foo10 foo19 foo28 foo37 foo46 foo55 foo64 foo73 foo82 foo91
foo100 foo2 foo29 foo38 foo47 foo56 foo65 foo74 foo83 foo92
Foo11 foo20 Foo3 foo39 foo48 foo57 foo66 foo75 foo84 foo93
Foo12 foo21 foo30 Foo4 foo49 foo58 foo67 foo76 foo85 foo94
Foo13 foo22 foo31 foo40 foo5 foo59 foo68 foo77 foo86 foo95
Foo14 foo23 foo32 foo41 foo50 foo6 foo69 foo78 foo87 foo96
FOO15 foo24 foo33 foo42 foo51 foo60 foo7 foo79 foo88 foo97
Foo16 foo25 foo34 foo43 foo52 foo61 foo70 foo8 foo89 foo98
Foo17 foo26 foo35 foo44 foo53 foo62 foo71 foo80 foo9 foo99


Renaming results:
(Red is unchanged, blue is part of the change)

Reference

$ rename foo foo0 foo?
$ ls
Foo01 foo100 foo20 foo30 foo40 foo50 foo60 foo70 foo80 foo90
Foo02 foo11 foo21 foo31 foo41 foo51 foo61 foo71 foo81 foo91
foo03 foo12 foo22 foo32 foo42 foo52 foo62 foo72 foo82 foo92
foo04 foo13 foo23 foo33 foo43 foo53 foo63 foo73 foo83 foo93
foo05 foo14 foo24 foo34 foo44 foo54 foo64 foo74 foo84 foo94
Foo06 foo15 foo25 foo35 foo45 foo55 foo65 foo75 foo85 foo95
Foo07 foo16 foo26 foo36 foo46 foo56 foo66 foo76 foo86 foo96
foo08 foo17 foo27 foo37 foo47 foo57 foo67 foo77 foo87 foo97
foo09 foo18 foo28 foo38 foo48 foo58 foo68 foo78 foo88 foo98
Foo10 foo19 foo29 foo39 foo49 foo59 foo69 foo79 foo89 foo99
$ rename foo foo0 foo??
$ ls
foo001 foo011 foo021 foo031 foo041 foo051 foo061 foo071 foo081 foo091
foo002 foo012 foo022 foo032 foo042 foo052 foo062 foo072 foo082 foo092
foo003 foo013 foo023 foo033 foo043 foo053 foo063 foo073 foo083 foo093
foo004 foo014 foo024 foo034 foo044 foo054 foo064 foo074 foo084 foo094
foo005 foo015 foo025 foo035 foo045 foo055 foo065 foo075 foo085 foo095
foo006 foo016 foo026 foo036 foo046 foo056 foo066 foo076 foo086 foo096
foo007 foo017 foo027 foo037 foo047 foo057 foo067 foo077 foo087 foo097
foo008 foo018 foo028 foo038 foo048 foo058 foo068 foo078 foo088 foo098
foo009 foo019 foo029 foo039 foo049 foo059 foo069 foo079 foo089 foo099
foo010 foo020 foo030 foo040 foo050 foo060 foo070 foo080 foo090 foo100


This example shows the use of batch renaming of two files:

Reference

First parameter: the replaced string
Second argument: Replacing the string with a
Third parameter: match the file pattern to be replaced


Rename supports wildcard characters, with the following basic wildcard characters:

Reference

? Can replace a single character
* Can replace multiple characters
[CharSet] can replace any single character in a charset set


Second, other examples
Look at the role of *:

Reference

$ rm-f *
$ for I in ' seq ';d o touch foo$i;done
$ rename Foo foo0 foo*
$ ls
Foo01 foo018 foo027 foo036 foo045 foo054 foo063 foo072 foo081 foo090 foo010 foo019 foo028 foo037 foo046 F oo055 foo064 foo073 foo082 foo091
foo0100 foo02 foo029 foo038 foo047 foo056 foo065 foo074 foo083 foo092
foo011 foo020 foo03 foo039 foo048 foo057 foo066 foo075 foo084 foo093
foo012 foo021 foo030 foo04 foo049 foo058 foo067 foo076 foo085 foo094
foo013 foo022 foo031 foo040 foo05 foo059 foo068 foo077 foo086 foo095
foo014 foo023 foo032 foo041 foo050 foo06 foo069 foo078 foo087 foo096
foo015 foo024 foo033 foo042 foo051 foo060 foo07 foo079 foo088 foo097
foo016 foo025 foo034 foo043 foo052 foo061 foo070 foo08 foo089 foo098
foo017 foo026 foo035 foo044 foo053 foo062 foo071 foo080 foo09 foo099


And look at [CharSet] 's role:

Reference

$ rm-f *
$ for I in ' seq ';d o touch foo$i;done
$ rename Foo foo0 foo[9]*
$ ls
foo09 foo099 foo17 foo26 foo35 foo44 foo53 foo62 foo71 foo80
foo090 foo1 foo18 foo27 foo36 foo45 foo54 foo63 foo72 foo81
foo091 foo10 foo19 foo28 foo37 foo46 foo55 foo64 foo73 foo82
foo092 foo100 foo2 foo29 foo38 foo47 foo56 foo65 foo74 foo83
foo093 foo11 foo20 foo3 foo39 foo48 foo57 foo66 foo75 foo84
foo094 foo12 foo21 foo30 foo4 foo49 foo58 foo67 foo76 foo85
foo095 foo13 foo22 foo31 foo40 foo5 foo59 foo68 foo77 foo86
foo096 foo14 foo23 foo32 foo41 foo50 foo6 foo69 foo78 foo87
foo097 foo15 foo24 foo33 foo42 foo51 foo60 foo7 foo79 foo88
foo098 foo16 foo25 foo34 foo43 foo52 foo61 foo70 foo8 foo89


It's not hard to understand, just look for the environment test. It is more convenient to use rename to reduce the part of the write for loop work.

The rename command under Linux

Related Article

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.