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?
Advertisement
Answer
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 ofdots
- Execute
mv