0

Im trying to rename a whole bulks of files with underscores, hashtags and a bunch of characters that the ftp servers have trouble dealing it.

I have always have resorted to do it with

find . -depth -name '* *' \
| while IFS= read -r f ; do
    mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ' ' _)"
done

but I have failed to adapt the tr part to make the change, this way too

....tr '(' '_')"  

here I want to change the ( character.

UPDATE
As noted by wjandrea, I did not update the definition in the -name parameter of the find comand, it should be

find . -depth -name '*(*'
wjandrea
  • 28,235
  • 9
  • 60
  • 81
pollotl
  • 3
  • 4

2 Answers2

1

To replace the space character, !, (, ) and # with an underscore, you could use:

find . -depth -name '*[ !()#]*' -exec sh -c '
  for f; do
    mv -i "$f" "$(dirname "$f")/$(basename "$f" | tr " !()#" _)"
  done
' sh {} +

If your find supports the -execdir action used in combination with the Perl rename tool:

find . -name '*[ !()#]*' -execdir rename -n 's/[ !()#]/_/g' {} +

Remove option -n if the output looks as expected.

Freddy
  • 4,548
  • 1
  • 7
  • 17
0

Your command tr '(' '_' translates an open parenthesis into an underscore. Nothing more.

From the tr man-page:

 tr [OPTION]... SET1 [SET2]

 ....  SET2  is extended to length of SET1 by repeating its last character as necessary. ....

Hence, you need to specify all the characters to be translated, for instance:

tr '()#!' _
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • This is what I did before, but It did not work out, I was thinking the ssame that updating the tr part was enough. It was needed to update the find part too. – pollotl May 25 '20 at 15:24
  • These are unrelated issues. You can loop using `find` over **all** files, which means that you will do a lot of unnecessary `mv`, and you can already preselect the files in need to be changed inside the `find` command. This is more about efficiency than correctness. – user1934428 May 25 '20 at 17:11