在 Mac OS X 上小写或重命名目录中的所有文件
在投入了几个小时后,我放弃了,并寻求帮助。这已经在之前的SO问题中得到了完美的回答:How do I rename所有文件都小写?
问题是,它在 Mac OS X 上不起作用。所以我开始重做它,以便它可以工作。了解了一些关于强/弱引用的知识,我认为这与它有关。目前,我对所有内容都使用强引用。
#!/bin/bash
echo ""; echo "Start run `basename $0` on `date`";
echo "Script running from: `pwd`";
# Change into the directory with the files to rename
cd /Users/me/Desktop/files
echo "Working on files in: `pwd`"; echo "";
# Read in all files from a directory
for file in *; do
lowercase_filename=`echo $file | tr '[:upper:]' '[:lower:]'`;
echo \'$file\' \'$lowercase_filename\';
mv \'$file\' \'$lowercase_filename\';
echo "--------------------";
done
以下是上面的脚本运行时将输出的内容:
./renamer.sh
Start run renamer.sh on Fri Nov 25 04:35:00 PST 2011
Script running from: /Users/me/Desktop
Working on files in: /Users/me/Desktop/files
'This IS A test TEST.txt' 'this is a test test.txt'
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
由于某种原因,mv
不起作用。然而,奇怪的是,如果我获取调试输出并手动运行它,它会正常工作。所以我有一个文件名的前后字符串,在这种情况下,前面是混合大小写,后面是小写。字符串用单引号引起来。我将它们回显出来,就像将它们作为两个参数传递给 mv 命令一样。
'This IS A test TEST.txt' 'this is a test test.txt'
该脚本给了我一个错误,但如果我手动运行这些命令:
# "l" is an alias for ls with some args to remove fot files
# and other junk I don't want to see.
me@whitebook:\ $cd files
me@whitebook:\ $l
-rw-r--r--+ 1 me staff 0 Nov 25 03:49 This IS A test TEST.txt
me@whitebook:\ $mv 'This IS A test TEST.txt' 'this is a test test.txt'
me@whitebook:\ $l
-rw-r--r--+ 1 me staff 0 Nov 25 03:49 this is a test test.txt
如您所见,文件被用小写重命名为“这是一个测试 test.txt”。如果我可以手动mv
这些,那么脚本环境中就会发生一些事情,导致它变得混乱。知道那可能是什么吗?
我应该能够像另一位海报所做的那样将其写成一行,但无论我尝试什么,它对我都不起作用。
感谢您的任何指导。我使用的是 Mac OS X,这里是一些相关的系统信息......
$uname -a
Darwin whitebook.local 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug 9 20:56:15 PDT 2011; root:xnu-1699.24.8~1/RELEASE_I386 i386
$bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
After putting hours into this, I give up, and am asking for help. This has been answered perfectly in a previous SO question here: How do I rename all files to lowercase?
The trouble is, it does not work on Mac OS X. So I went about working to redo it so it would work. Learned a little about strong/weak quoting, which I thought had something to do with it. For now, I am using strong quoting on everything.
#!/bin/bash
echo ""; echo "Start run `basename $0` on `date`";
echo "Script running from: `pwd`";
# Change into the directory with the files to rename
cd /Users/me/Desktop/files
echo "Working on files in: `pwd`"; echo "";
# Read in all files from a directory
for file in *; do
lowercase_filename=`echo $file | tr '[:upper:]' '[:lower:]'`;
echo \'$file\' \'$lowercase_filename\';
mv \'$file\' \'$lowercase_filename\';
echo "--------------------";
done
Here is what the above script will output when run:
./renamer.sh
Start run renamer.sh on Fri Nov 25 04:35:00 PST 2011
Script running from: /Users/me/Desktop
Working on files in: /Users/me/Desktop/files
'This IS A test TEST.txt' 'this is a test test.txt'
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
For some reason, mv
doesn't work. However, what is strange, is if I take the debugging output and manually run this, it will work fine. So I have a before and and after string of a filename, in this case, the before is the mixed case and the after is the lowercase. The strings are quoted in single tic marks. I echo them out just as I would pass them as two args to the mv
command.
'This IS A test TEST.txt' 'this is a test test.txt'
The script gives me an error, but if I run these commands by hand:
# "l" is an alias for ls with some args to remove fot files
# and other junk I don't want to see.
me@whitebook:\ $cd files
me@whitebook:\ $l
-rw-r--r--+ 1 me staff 0 Nov 25 03:49 This IS A test TEST.txt
me@whitebook:\ $mv 'This IS A test TEST.txt' 'this is a test test.txt'
me@whitebook:\ $l
-rw-r--r--+ 1 me staff 0 Nov 25 03:49 this is a test test.txt
As you can see the file was renamed with lowercase just fine to "this is a test test.txt". If I can mv
these by hand, then something is happening inside the scripts environment that is getting it confused. Any idea what that may be?
I should be able to one-line this as the other poster has done, but no matter what I try, it doesn't work for me.
Thanks for any guidance. I am on Mac OS X, here is some relevant system info...
$uname -a
Darwin whitebook.local 11.2.0 Darwin Kernel Version 11.2.0: Tue Aug 9 20:56:15 PDT 2011; root:xnu-1699.24.8~1/RELEASE_I386 i386
$bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能试试这个吗?
Can you try this?