Ubuntu – Rename all .txt files inside subfolders with “find”

bashbatch-renamecommand line

I want to rename all .txt files inside a folder. This rename do rename all .txt for me:

rename 's/\.txt//g' *.txt -v

but when I want to rename all sub folders with

find ./ -type d -execdir rename 's/\.txt//g' *.txt -v ";"

It shows me:

Can't rename *.txt *: No such file or directory
Can't rename *.txt *: No such file or directory
...

Also find ./ -type -d shows me current and all sub folders correctly.

Why I have No such file or directory message?

Best Answer

You need to use find's -exec syntax correctly, using '{}' to represent the found files

find ./ -type d -name '*.txt' -execdir rename -n 's/\.txt//g' '{}' \;

Remove -n after rename once you've tested it.

(assuming you really did want to change directory names and not regular file names - if that was the case, use -type f instead of type -d)

Related Question