问题
I have created a list of files with names in uppercase alphabets and trying to rename them to files with the same names but in lowercase alphabets. So if i have 20 files with filenames like FILE1, FILE2, FILE3, etc. I want to rename them to file1, file2, file3, etc. respectively. I am executing the below command
[root@host-1-1 files]# find . -name 'FILE*' -exec mv {} `echo {} | tr [:upper:] [:lower:]` \;
But i am receiving below errors from mv command. Could someone please tell me what i am missing here? mv: ‘./FILE1’ and ‘./FILE1’ are the same file mv: ‘./FILE2’ and ‘./FILE2’ are the same file mv: ‘./FILE3’ and ‘./FILE3’ are the same file mv: ‘./FILE4’ and ‘./FILE4’ are the same file mv: ‘./FILE5’ and ‘./FILE5’ are the same file mv: ‘./FILE6’ and ‘./FILE6’ are the same file mv: ‘./FILE7’ and ‘./FILE7’ are the same file mv: ‘./FILE8’ and ‘./FILE8’ are the same file mv: ‘./FILE9’ and ‘./FILE9’ are the same file mv: ‘./FILE10’ and ‘./FILE10’ are the same file mv: ‘./FILE11’ and ‘./FILE11’ are the same file mv: ‘./FILE12’ and ‘./FILE12’ are the same file mv: ‘./FILE13’ and ‘./FILE13’ are the same file mv: ‘./FILE14’ and ‘./FILE14’ are the same file mv: ‘./FILE16’ and ‘./FILE16’ are the same file mv: ‘./FILE18’ and ‘./FILE18’ are the same file mv: ‘./FILE20’ and ‘./FILE20’ are the same file mv: ‘./FILE15’ and ‘./FILE15’ are the same file mv: ‘./FILE17’ and ‘./FILE17’ are the same file mv: ‘./FILE19’ and ‘./FILE19’ are the same file [root@host-1-1 files]#
PS: I have already used for loop and have fulfilled my requirement. I am working on my understanding in find command.
回答1:
The problem here is that the command substitution happens first:
`echo {} | tr [:upper:] [:lower:]`
Leaving {} as the name to rename it to. Only then does find -exec replace the {} with the filenames, at which point this is the template it's filling in:
mv {} {}
Of course, that results in mv ./FILE1 ./FILE1 which gives the error you're getting.
回答2:
YMMV, but I generally reach for the rename command, because it:
- is simple,
- has a ”dry-run” option (
-n) - won’t clobber if two files rename to the same output file.
Your command then becomes:
rename -n 'y/A-Z/a-z/' FILE*
Link to some examples and man page.
来源:https://stackoverflow.com/questions/47069085/find-command-to-rename-files