I have the following command that I run to lowercase files/directories, replace . with _ and various other manipulations. The problem with it is that it seems to try to rename the directories first and then it cannot find the files below it. Is there a way to rename all files in a directory first then the directory itself recursively? e.g.
- directory1
- file1
- file2
- directory1
- file1
- file2
- directory2
- file1
- directory2
- file1
- file2
My code is
while IFS= read -rd '' entry; do
entry="${entry#./}" # strip ./
if [[ -d $entry ]]; then
rename 'y/A-Z/a-z/; s/ /_/g; s/_-_/-/g; s/sometext//g; s/_extras/-extras/g' "$entry"
else
rename 'y/A-Z/a-z/; s/\.(?=.*\.)/_/g; s/ /_/g; s/_-_/-/g; s/sometext//g; s/_extras/-extras/g' "$entry"
fi
done < <(find . -iname '*' -print0)
find
to create a list and sort that bottom up regarding directory/file depth. – yacc