Unix Find Replace Special Characters in Multiple Files
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/ø//' {} \
Schoffelman
Updated on July 09, 2022Comments
-
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 over 14 yearsThe '' was just to throw the character into quotes, replacing it with nothing
-
Alberto Zaccagni over 14 yearstr -d 'Â' just deletes, I think it's fine with what you need, or am I missing something?
-
Schoffelman over 14 yearssorry, the results I'm looking for is to have the special character deleted.
-
ghostdog74 over 14 yearsuseless use of cat -- sed 's/ø//' file.txt
-
user224243 over 14 yearsOkay, 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 over 14 yearsWith 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 over 14 yearsI was able to get this one to work with a few additional flags find . -type f -name '.' -exec sed -i "s/Â//gi" {} \;
-
Admin over 14 yearsI would upvote for the nice "trick" (however it was not a question about moving files around).