Linux – Rename a question mark (?) in filenames

bashlinuxrenamespecial charactersubuntu-10.04

With rename it is possible to bulk change filenames. I managed to get rid of all + with this command and replace them with underscores:

rename 's/\+/_/g' * 

I could change normal letters like a to A with.

rename 's/a/A/g' *

but I could not rename the ?, not like this /\? and not like this /?.

Is there any way to adress the "?" in the filename? Most FTP programs fail to rename files with ? as well. Midnight Commander fails. The only way I found that works so far is:

mv ?myfile.txt myfile.txt

but this command is not flexible enough. I would prefer to bulk rename all ? in all files.

Best Answer

How about this:

for filename in *
do 
    if [ "$filename" == *"?"* ] 
    then
        mv "$filename" "$(echo $filename | tr '?' '-')" 
    fi
done

Or as a one liner:

for filename in *; do mv "$filename" "$(echo $filename | tr '?' '-')" ; done

However, it looks like your issue isn't that there are question marks in your filenames, but rather that your filenames contain characters that ls doesn't recognize.

Related Question