I have many files in a directory that have names containing ?
, and I want to remove these ?
characters.
Could you please help me with that?
I have many files in a directory that have names containing ?
, and I want to remove these ?
characters.
Could you please help me with that?
rename 's/\?//g' *
whould do the job to rename all the files
francois@zaphod:~/tmp$ ls
toto_?_ toto_?_1 toto_?_2
francois@zaphod:~/tmp$ rename 's/\?//g' *
francois@zaphod:~/tmp$ ls
toto__ toto__1 toto__2
francois@zaphod:~/tmp$
sed
command almost certainly did, unfortunately. I hope that you have backups. Note https://unix.stackexchange.com/q/275254/5132 and https://unix.stackexchange.com/q/229230/5132 for the now revised answer.
– JdeBP
Jan 03 '20 at 22:16
Assuming bash
or any similar shell that knows how to replace all occurrences of a string matching a pattern in the value of a variable with ${variable//pattern/replacement}
(e.g. zsh
or ksh
):
for name in ./*'?'*; do
mv -i "$name" "${name//'?'/}"
done
This is a short loop that iterates over all names in the current directory that contains at least one ?
(skipping hidden names). For each such name, the ?
characters are removed from the filename and the result is used as the new filename of the file.
The single quotes around ?
in the patterns stops it from being treated as a the special globbing character that matches any single character (which is what an unquoted ?
does). You could also have used either \?
or [?]
.
If the question marks that you see are from the output of ls
, then they may represent non-printable characters.
To remove these, replace each '?'
in the code above with [![:print:]]
. The [![:print:]]
globbing pattern matches a character that is not printable. The [:print:]
character class is similar to [:graph:]
, but the former matches the space character while the latter does not. Using [![:print:]]
would therefore not remove spaces, while using [![:graph:]]
would remove spaces.
?
or are they non-graphic characters being replaced by a question mark? – Torin Jan 03 '20 at 21:45