免费注册 查看新帖 |

Chinaunix

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

请问shell怎么批量改文件名——[已解决]五行搞定,好棒! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-09 08:22 |只看该作者 |倒序浏览
如题,我想把我工作目录下的所有文件夹都改成小写,要用什么命令?我只会用mv一个一个的改,好累........

最终程序代码:

#!/bin/bash

    for f in *; do
    [ -f "$f" ] && continue
    g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
    mv "$f" "$g"
    done
           


不知道还有没有可能在缩减了,呵呵。

[ 本帖最后由 封神 于 2007-7-11 09:39 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-07-09 09:05 |只看该作者
NAME
       rename - renames multiple files

SYNOPSIS
       rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified as the first argument.  The perlexpr argument is a Perl expression which is expected to modify
       the $_ string in Perl for at least some of the filenames specified.  If a given filename is not modified by the expression, it will not be renamed.  If no filenames
       are given on the command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the extension, you might say

               rename 's/\.bak$//' *.bak
---------------------------------------------------------------------------------
       To translate uppercase names to lower, you'd use

               rename 'y/A-Z/a-z/' *

---------------------------------------------------------------------------------

论坛徽章:
0
3 [报告]
发表于 2007-07-09 09:10 |只看该作者
#!/bin/bash
#This script is used to  change capital directory.
#change capital diretory
if [ -f tmp_file ];then
rm -rf tmp_file
fi
echo "----Start change diretory!----"
sleep 1
echo
find . -type  d > tmp_file
for file in $(cat tmp_file)
do
  newfile=$(echo $file | tr A-Z a-z)
  mv $file $newfile
done
rm -rf tmp_file
echo "----Finished!----"
exit 0

论坛徽章:
0
4 [报告]
发表于 2007-07-09 09:34 |只看该作者
收到,马上去试试.没想到还有rename这样的命令.....

3楼的程序有些看不明白,因为我本来就不善长这个,不过还是要去试试效果.恩, 谢谢两位了

论坛徽章:
0
5 [报告]
发表于 2007-07-09 09:43 |只看该作者
另:

二楼的命令是不是不分文件类型啊,我只想改目录,普通文件还是要保留原文件名的

论坛徽章:
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
6 [报告]
发表于 2007-07-09 11:09 |只看该作者
if [ -d file ]
then
        rename
fi

论坛徽章:
0
7 [报告]
发表于 2007-07-09 20:02 |只看该作者
man find

google site:bbs.chinaunix.net

论坛徽章:
0
8 [报告]
发表于 2007-07-11 09:13 |只看该作者
还是有问题啊。

rename 'y/A-Z/a-z/' *
这句命令执行之后,没有效果,什么都没变化。

if [ -d file ]
then
        rename
fi

这个为什么会是 if[ -d file ],我想改的是目录的名字啊,我自己试着改成if [ -d dir],好像也不对。


#!/bin/bash
#This script is used to  change capital directory.
#change capital diretory
if [ -f tmp_file ];then
rm -rf tmp_file
fi
echo "----Start change diretory!----"
sleep 1
echo
find . -type  d > tmp_file
for file in $(cat tmp_file)
do
  newfile=$(echo $file | tr A-Z a-z)
  mv $file $newfile
done
rm -rf tmp_file
echo "----Finished!----"
exit 0

这个程序倒是好用,但是无法判断 . 和 .. ,不过不影响最终结果。

之后我根据rename在网上又找到了一个。

    for f in *; do
    g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
    mv "$f" "$g"
    done

这个程序倒是不会判断 . 和 ..,但是这个程序不分文件和目录,全部统统改掉。一直搞不清shell的正则表达式,所以我只会简单的应用,哪儿位朋友能把上面的四个程序整合一下,谢谢了。

PS:最好 . 和 .. 还有小写文件及目录就不要处理了。

[ 本帖最后由 封神 于 2007-7-11 09:15 编辑 ]

论坛徽章:
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
9 [报告]
发表于 2007-07-11 09:30 |只看该作者
try

  1.     for f in *; do
  2.     [ -f "$f" ] && continue
  3.     g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
  4.     mv "$f" "$g"
  5.     done
复制代码

论坛徽章:
0
10 [报告]
发表于 2007-07-11 09:34 |只看该作者
Yes,好棒!就是它了,谢谢楼上的兄弟
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP