Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370338
  • 博文数量: 61
  • 博客积分: 2451
  • 博客等级: 上尉
  • 技术积分: 650
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-06 21:24
文章分类

全部博文(61)

文章存档

2012年(1)

2011年(44)

2010年(16)

分类: LINUX

2011-03-08 23:45:55

以前改文件名都用mv,今天man 了下rename

rename  will  rename  the specified files by replacing the first occur-
      rence of from in their name by to.

      For example, given the files foo1, ..., foo9, foo10, ...,  foo278,  the
      commands

             rename foo foo0 foo?
             rename foo foo0 foo??

      will turn them into foo001, ..., foo009, foo010, ..., foo278.

      And
             rename .htm .html *.htm

      will fix the extension of your html files.

mv 命令不能批量修改文件名但是mv也有rename没有的功能

rename 可以用来批量修改文件名

例如

[root@Server1 shelllearn]#ll
总计 8
-rwxr--r-- 1 root root 121 03-08 23:14 a
-rwxr--r-- 1 root root  66 03-08 23:19 b
-rw-r--r-- 1 root root   0 03-08 23:22 test2.html
-rw-r--r-- 1 root root   0 03-08 23:22 test3.html
-rw-r--r-- 1 root root   0 03-08 23:05 test file1
-rw-r--r-- 1 root root   0 03-08 23:04 test file2
-rw-r--r-- 1 root root   0 03-08 23:04 test file3
-rw-r--r-- 1 root root   0 03-08 23:04 test file4
-rw-r--r-- 1 root root   0 03-08 23:04 test file5
-rw-r--r-- 1 root root   0 03-08 23:22 test.html

在这我想将文件名中有空格的文件名中的空格改成“—”

可以用命令rename实现

rename “ ” “_" *

比下面的简单多了

#!/bin/bash
find . -name "*_*" -print |
while read name
do
        ch_na=$(echo $name | tr '_' ' ')
        mv "$name" "$ch_na"
done

然后我们把以.html结尾改成.htm结尾

rename .html .htm *

[root@Server1 shelllearn]#rename " " "_" *
[root@Server1 shelllearn]#rename .html .htm *
[root@Server1 shelllearn]#ls
a  test2.htm  test_file1  test_file3  test_file5
b  test3.htm  test_file2  test_file4  test.htm

嘿嘿 rename就这样简单记录下吧

阅读(2888) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~