質問

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.
役に立ちましたか?

解決

Can you try this?

mv "$file" "$lowercase_filename";
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top