Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...