简体   繁体   English

Linux以递归方式替换所有directorys的句点,以及除了持续下划线的文件的最后一段时间

[英]Linux recursively replace periods for all directorys and all but last period for files with underscores

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 所以输入是:this.is.a.directory this.is.a.file.txt

Output this_is_a_directory this_is_a_file.txt 输出this_is_a_directory this_is_a_file.txt

You can do this using find in a while loop and using a regex to leave last DOT for files: 您可以使用while循环中的find并使用正则表达式为文件保留最后一个DOT

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. s/\\.(?=.*\\.)/_/g只会在输入中有另一个DOT时替换DOT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM