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

Community
  • 1
  • 1
Off Grid
  • 21
  • 1
  • 6

2 Answers2

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?
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • 2
    Also need to avoid the initial `./`, and the last dot that introduces the extension. So `(?<!^)\.(?=.*\.)` for perlre. – 4ae1e1 Nov 25 '15 at 20:28
  • @4ae1e1 & @l0b0 Neither `(?<!^)\.(?=.*\.)` or `s/\./-/g` worked I have no change. The command runs, no error. – Off Grid Nov 27 '15 at 00:51
  • @l0b0, No `(* *)` "SPACES" was/are not intentional `(*.*)` "DOT" is the correct search pattern. Thanks. – Off Grid Dec 04 '15 at 21:00
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
Walter A
  • 19,067
  • 2
  • 23
  • 43