免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7539 | 回复: 12
打印 上一主题 下一主题

怎样批量改文件扩展名呢。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-20 12:13 |只看该作者 |倒序浏览
我一个目录下有n个*.tmpl文件,我要改成*.html的。mv *.tmpl *.html 或者rename *.tmpl *.html都不对。
怎么改好呢。

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
2 [报告]
发表于 2007-07-20 12:18 |只看该作者
#!/bin/sh
for file in *.tmpl
do
        mv $file ${file%.*}.html
done

论坛徽章:
0
3 [报告]
发表于 2007-07-20 12:32 |只看该作者
ls | grep ".tmpl" |xargs -n 1 | while read line;do mv $line ${line%.tmpl}.html;done

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2007-07-20 12:35 |只看该作者
rename tmpl html *.tmpl

论坛徽章:
0
5 [报告]
发表于 2007-07-20 13:25 |只看该作者
唉,看各位命令用的这么熟悉,差距啊。
这么简单的事情以前我还特意谢了个脚本
#!/bin/bash

#

#   Funtion:This Script is used to batch modifying the suffix of files

#

#   Usage:  ./scriptname old_suffix new_suffix

#   Date:   2007-07-07

#####################################################################


OLD_SUF=$1
NEW_SUF=$2
count=0

#clear the screen

clear

if [ $# != 2 ]

then
    echo Syntax error.
    echo Usage: $0 old_suffix new_suffix
    exit 1
fi
    
ls *."$OLD_SUF" > tmp1 2> errlog

if [ -s errlog ]  #if list no files in this directory

then
    echo "No \"$OLD_SUF\" files found in this directory."
    echo "lease specify the available suffix."
    exit 1
fi

#cut the suffix of the filename to tmp2

sed 's/\(^.*\)\(\..*$\)/\1/' tmp1 > tmp2

while read filename
do
    name=$filename
    count=$(($count+1))
    mv $filename.$OLD_SUF $name.$NEW_SUF
    echo Modify $filename.$OLD_SUF to $name.$NEW_SUF successfully.
done < tmp2

echo  $count files were modified from suffix \

是多么的麻烦,这就是新手:em11: 。

论坛徽章:
0
6 [报告]
发表于 2007-07-20 13:57 |只看该作者
测试了一下,在某文件夹下创建了数个*.ok的文件,然后:
rename ok tmp *.ok
结果报
rename:too many arguments.为什么会这样呢?


man了一下,发现这样可以:
rename -s/ok/tmp/g *.ok
测试通过。

[ 本帖最后由 zzzz991 于 2007-7-20 14:04 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2007-07-20 15:57 |只看该作者
原帖由 zzzz991 于 2007年7月20日 13:57 发表
man了一下,发现这样可以:
rename -s/ok/tmp/g *.ok
测试通过。

为啥我的rename没这个功能?
老兄的rename是哪里的?

论坛徽章:
0
8 [报告]
发表于 2007-07-20 16:12 |只看该作者
freebsd6.2用ports安装的,貌似是1.3 版本。

论坛徽章:
0
9 [报告]
发表于 2007-07-20 22:56 |只看该作者
seems 'rename' is not standard command in Unix-Like system. so I would prefer to use a script so that it can be more compatible and portable

[ 本帖最后由 dreammaker 于 2007-7-20 23:03 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2007-07-20 23:35 |只看该作者
fileList

sed 's/\(.*\)\.\(tmpl\)/mv \1\.\2 \1\.html/g' fileList
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP