Skora
Skora

Reputation: 127

How to rename files in different directories with the same name using find

I have files named test.txt in different directories like this

./222/test.txt
./111/test.txt

I want to rename all test.txt to info.txt

I've tried using this

find . -type f -iname 'test.txt' -exec mv {} {}info \;

I get test.txtinfo

Upvotes: 0

Views: 53

Answers (1)

Inian
Inian

Reputation: 85895

Your idea is right, but you need to use -execdir instead of just -exec to simplify this.

find . -type f -iname 'test.txt' -execdir mv {} info.txt ';'

This works like -exec with the difference that the given shell command is executed with the directory of the found pathname as its current working directory and that {} will contain the basename of the found pathname without its path. Also note that the option is a non-standard one (non POSIX compliant).

Upvotes: 2

Related Questions