Wednesday, June 27, 2018

linux - Rename recursively files, while keeping the extension


I have a bunch of directories that each hold one *.txt file in it (and other non *.txt files).


I'd like to rename those files recursively from the command line, basically *.txt to info.txt.
So all *.txt files should be renamed to info.txt.
There is always only one .txt file in each directory.


I have been looking at multiple questions here (and elsewhere) that seem similar, but either they focus to change the extension, or they do not work recursively,...


Any help would be very much appreciated.
Platform: Linux or MacOSX.


Answer



find ~/dir -type f -name "*.txt" -not -name "info.txt" \
-execdir mv -v {} info.txt \;

Alternatively:


find ~/dir -type f -name "*.txt" -not -name "info.txt" \
| while read -r file; do
mv "$file" "${file%/*}/info.txt"
done

No comments:

Post a Comment