Batch file name modification under Linux (rename)

Source: Internet
Author: User
Tags perl script

1.rename Command Batch modified file name, in fact, Linux can use other methods to batch change the file name, but rename is too convenient

For example, change all the tables to cdb1_* to cdb_*.
In this directory
Only need # rename ' cdb1 ' CDB ' *

Used to write a for loop to do ... Think about how silly ah, hehe

Rename also has more features that suggest man rename under

From:http://www.hao32.com/unix-linux/42.html

2. Batch change file name rename

with the man rename command you can tell that the rename command is actually a Perl script command,
It is dedicated to renaming multiple files in batches (rename multiple files).

command format:
Rename [-v] [-n] [-f] perlexpr [files]
perlexpr is a regular expression in Perl script format.
Parameters:
-V,--verbose
verbose:print Names of files successfully renamed.
verbose mode: Print List of file names for successful changes
-N,--no-act
No action:show What files would has been renamed.
Test mode: Do not really execute commands, but just show which filenames should be done
change, for test mode.
-F,--force
force:overwrite existing files.
Mandatory mode: Overwrite already exists when changing file name if changed files already exist
the file.

Typical applications of rename:

0. Batch Change file name extensions
  $ ls
1.txt 2.txt 3.txt 4.txt
$ Rename ' s//.txt//.ext/' *
$ ls
1.ext 2.ext 3.ext 4.ext
1. Delete file name extensions in bulk
  $ ls
1.txt 2.txt 3.txt 4.txt
$ Rename ' s//.txt//' *
$ ls
1 2 3 4
2. Batch Add File extension
  $ ls
1 2 3 4
$ Rename ' s/$//.txt/' *
$ ls
1.txt 2.txt 3.txt 4.txt
3. Batch rename files on your own terms
  $ ls
1.ext 2.ext 3.ext 4.ext
$ Rename ' s/(/d)/cap/' *
$ ls
1th Chapter Ext 2nd Chapter EXT 3rd Chapter EXT 4th Chapter Ext

3.

Method 1: Split file name processing, you can modify the file name arbitrarily

Find-name ' *.log '-printf%f//n|awk-f '. ' ' {print '} ' |xargs-i{} mv {}.log Xiyun_{}.log

Method 2: Use rename

General Linux Rename commands are relatively simple

Rename ' Test ' Xiyun ' *.log '

Replace a part of a string in a file name

The rename command under Ubuntu supports regular expressions and is therefore more powerful.

Method 3: Use the Find and Xargs and MV directly, without awk in the middle, so you can add only the suffix name, cannot modify the file name.


This article is from the "Lonely No Color" blog, please be sure to keep this source http://tenderness.blog.51cto.com/8855468/1954371

Batch file name modification under Linux (rename)

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.