Reputation: 23
I have searched looking for the right solution. I have found some close examples.Bash script to replace spaces in file names
But what I'm looking for is how to replace multiple .
dots in current DIRECTORY/SUBDIRECTORY names, then replace multiple .
dots in FILENAMES excluding the *.extention "recursively".
This example is close but not right:
find . -maxdepth 2 -name "*.*" -execdir rename 's/./-/g' "{}" \;
another example but not right either:
for f in *; do mv "$f" "${f//./-}"; done
So
dir.dir.dir/dir.dir.dir/file.file.file.ext
Would become
dir-dir-dir/dir-dir-dir/file-file-file.ext
Upvotes: 2
Views: 1403
Reputation: 58928
.
in regular expressions (such as the ones used for rename
, because by default it has the special meaning of "any single character". So the replacement statement at least should be s/\./-/g
.{}
in find
commands.find
commands, since you want to replace all dots in directory names, but keep the last dot in filenames.* *
). Is that intentional?Upvotes: 1
Reputation: 20032
You can assign to a variable and pipe like this:
x="dir.dir.dir/dir.dir.dir/file.file.file.ext"
echo "$(echo "Result: ${x%.*}" | sed 's/\./_/g').${x##*.}"
Result: dir_dir_dir/dir_dir_dir/file_file_file.ext
Upvotes: 1