2

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

1
  • Assuming you can use perlre, this is the re you need: (?<!^)\.(?=.*\.).
    – 4ae1e1
    Commented Nov 25, 2015 at 20:26

2 Answers 2

1

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
1
  1. You have to escape . 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.
  2. You should not quote {} in find commands.
  3. You will need two find commands, since you want to replace all dots in directory names, but keep the last dot in filenames.
  4. You are searching for filenames which contain spaces (* *). Is that intentional?
3
  • 2
    Also need to avoid the initial ./, and the last dot that introduces the extension. So (?<!^)\.(?=.*\.) for perlre.
    – 4ae1e1
    Commented Nov 25, 2015 at 20:28
  • @4ae1e1 & @l0b0 Neither (?<!^)\.(?=.*\.) or s/\./-/g worked I have no change. The command runs, no error.
    – Off Grid
    Commented Nov 27, 2015 at 0:51
  • @l0b0, No (* *) "SPACES" was/are not intentional (*.*) "DOT" is the correct search pattern. Thanks.
    – Off Grid
    Commented Dec 4, 2015 at 21:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.