问题
I have the following command which recursively renames all the files/directory's to lowercase and replaces spaces with _.
find . -iname "*" | rename 'y/A-Z/a-z/; s/ /_/g;'
How do I extend it to remove all periods from directories and leave just the last period for files?
So input would be: this.is.a.directory this.is.a.file.txt
Output this_is_a_directory this_is_a_file.txt
回答1:
You can do this using find
in a while loop and using a regex to leave last DOT
for files:
while IFS= read -rd '' entry; do
entry="${entry#./}" # strip ./
if [[ -d $entry ]]; then
rename 'y/A-Z/a-z/; s/ /_/g; s/\./_/g' "$entry"
else
rename 'y/A-Z/a-z/; s/ /_/g; s/\.(?=.*\.)/_/g' "$entry"
fi
done < <(find . -iname '*' -print0)
s/\.(?=.*\.)/_/g
will only replace a DOT if there is another DOT ahead in the input.
来源:https://stackoverflow.com/questions/43146061/linux-recursively-replace-periods-for-all-directorys-and-all-but-last-period-for