0

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?

Kusalananda
  • 333,661
LamaMo
  • 223
  • 2
    Do the filenames contain the literal question mark character ? or are they non-graphic characters being replaced by a question mark? – Torin Jan 03 '20 at 21:45
  • They contain a literal question mark character @Torin – LamaMo Jan 03 '20 at 21:52

2 Answers2

0
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$
francois P
  • 1,219
0

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.

Kusalananda
  • 333,661