silverscales
silverscales

Reputation: 37

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

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?

Upvotes: 1

Views: 704

Answers (1)

janos
janos

Reputation: 124774

Not very efficient, due to the sh invocation for each file, but this should work, and is safe:

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

How it works:

  • Find files starting with at least 2 dots.
  • Execute a sh (with a sequence of commands) in the directory of the file, passing the filename as parameter (sh -c '...' -- {})
  • Store the filename in fn
  • Store the dots prefix in dots
  • Compute the new filename as a substring of fn, starting after the length of dots
  • Execute mv

Upvotes: 1

Related Questions