Unix Find Replace Special Characters in Multiple Files

26,883

Solution 1

I would recommend looking into sed. It can be used to replace the contents of the file.

So you could use the command:

find . -type f -name '*.*' -exec sed -i "s/Â//" {} \;

I have tested this with a simple example and it seems to work. The -exec should handle files with whitespace in their name, but there may be other vulnerabilities I'm not aware of.

Solution 2

Use

tr -d 'Â' 

What does the ' ' stands for? On my system using your command produces this error:

tr: extra operand `'

Only one string may be given when deleting without squeezing repeats.

Try `tr --help' for more information.

Solution 3

sed 's/ø//' file.txt

That should do the trick for replacing a special char with an empty string.

find . -name "*.*" -exec sed 's/ø//' {} \
Share:
26,883
Schoffelman
Author by

Schoffelman

Updated on July 09, 2022

Comments

  • Schoffelman
    Schoffelman almost 2 years

    I've got a set of files in a web root that all contain special characters that I'd like to remove (Â,€,â,etc).

    My command

    find . -type f -name '*.*' -exec grep -il "Â" {} \;
    

    finds & lists out the files just fine, but my command

    find . -type f -name '*.*' -exec tr -d 'Â' '' \;
    

    doesn't produce the results I'm looking for.

    Any thoughts?

  • Schoffelman
    Schoffelman over 14 years
    The '' was just to throw the character into quotes, replacing it with nothing
  • Alberto Zaccagni
    Alberto Zaccagni over 14 years
    tr -d 'Â' just deletes, I think it's fine with what you need, or am I missing something?
  • Schoffelman
    Schoffelman over 14 years
    sorry, the results I'm looking for is to have the special character deleted.
  • ghostdog74
    ghostdog74 over 14 years
    useless use of cat -- sed 's/ø//' file.txt
  • user224243
    user224243 over 14 years
    Okay, did not think that someone uses spaces in filenames in a linux environment. But you right, it's a point. I will add a correction to my post.
  • Schoffelman
    Schoffelman over 14 years
    With both Grundlefleck's & the solution above I get a sed: 1: "./index.html": invalid command code . Checking to see if I have a sys/environment setting that needs to be changed - but I don't think that's it
  • Schoffelman
    Schoffelman over 14 years
    I was able to get this one to work with a few additional flags find . -type f -name '.' -exec sed -i "s/Â//gi" {} \;
  • Admin
    Admin over 14 years
    I would upvote for the nice "trick" (however it was not a question about moving files around).