pollotl
pollotl

Reputation: 3

How to adapt the next command to be able to change any character

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 '*(*'

Upvotes: 0

Views: 92

Answers (2)

Freddy
Freddy

Reputation: 4698

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.

Upvotes: 1

user1934428
user1934428

Reputation: 22237

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 '()#!' _

Upvotes: 0

Related Questions