[Original] rename commands in Linux

Source: Internet
Author: User

Original post: http://www.linuxfly.org/post/300/

5

[Original] Linux RENAME Command linuxing, 16:28, network service» virtual machine, comment (0), reference (0), read (61), via site original large | medium | small Reference address:
Note:This address is only valid in DOS/windows before 23:59:59 today, rename the file. The book says that the MV should be used for renaming files or directories in Linux. I have been doing this all the time, but ignored that there is also a command named rename in Linux. All are rename, but the functions are a little different. Rename in Linux is more like a batch rename tool provided in util-Linux.

I. Basic functions
From the man document of the MV and rename commands, you can see the following information: Reference MV-move (rename) files
Rename-rename files

That is to say, MV can also be used for renaming, but rename can be used for Batch Processing (* and other symbols are not supported when renaming.
Rename format:

$ Rename Foo foo0 Foo?

Rename must provide three parameters before deciding the final result.
Simulate the man document example. Original file: Reference $ for I in 'seq 100 '; do 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

Result of renaming:
(Red is not changed, and blue is a 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 how to batch rename two types of files: reference the first parameter: replaced string
Second parameter: string to be replaced
Third parameter: match the file mode to be replaced

Rename supports wildcards. The basic wildcards include: reference? Can replace a single character
* Replace multiple characters
[Charset] can replace any single character in the charset set

Ii. Other examples
Look at the role of *: Reference $ Rm-f *
$ For I in 'seq 100 '; do touch Foo $ I; done
$ Rename Foo foo0 Foo *
$ Ls
Foo01 foo018 foo027 foo036 foo045 foo054 foo063 foo072 foo081 foo090
Foo010 foo019 foo028 foo037 foo046 foo055 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

Let's take a look at the function of [charset]: Reference $ Rm-f *
$ For I in 'seq 100 '; do 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 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. Find the environment and test it. Using rename can reduce the work of writing a for loop, which is more convenient. Tags: Rename, MV

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.