list files that start with two dots and change file names in bulk

时间:2017-10-08 07:49:56

标签: linux bash terminal centos

I've got file that start with two dots located in different directories. I need to list them all and change their names in bulk and remove the dots completely.

Any suggestions how to do that?

1 个答案:

答案 0 :(得分:1)

由于每个文件的sh调用,效率不高,但这应该有效,并且是安全的:

find path -type f -name '..*' -execdir sh -c 'fn=$1; dots=${fn%%[^.]*}; cleaned=${fn:${#dots}}; mv -nv "$fn" "$cleaned"' -- {} \;

工作原理:

  • 查找以至少2个点开头的文件。
  • 在文件目录中执行sh(带有一系列命令),将文件名作为参数传递(sh -c '...' -- {}
  • 将文件名存储在fn
  • 将点前缀存储在dots
  • fn的长度
  • 开始,将新文件名计算为dots的子字符串
  • 执行mv