问题描述
我有以下命令,该命令以递归方式将所有文件/目录重命名为小写,并用_替换空格.
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?
因此输入将是:这是一个目录this.is.a.file.txt
So input would be:this.is.a.directorythis.is.a.file.txt
输出this_is_a_directorythis_is_a_file.txt
Outputthis_is_a_directorythis_is_a_file.txt
推荐答案
您可以在while循环中使用 find
并使用正则表达式将最后一个 DOT
保留为文件:
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
仅在输入前面有另一个DOT时才会替换DOT.
s/\.(?=.*\.)/_/g
will only replace a DOT if there is another DOT ahead in the input.
这篇关于Linux递归地替换所有目录的句点以及带下划线的文件的除最后一个句点以外的所有句点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!