0

In order to write files to a USB volume I want to rename filenames containing '?', '"', '*' or ':' to replace any of these characters with a space. But I'm having trouble with the '*'. My bash script is

for file in * 
do 
    mv -v "'"$file"'" "'"$(echo "$file" | sed 's/\(.*\)[?"*:]\(.*\)/\1 \2/')"'" 
done

The '*' keeps getting expanded into the filenames in the current directory. I've tried using

set -f

but then the initial 'in *' doesn't work.

3

Just make sure that you quote you filenames properly and there will be no issues.

for name in ./*; do
    newname=${name//[?\"*:]/ }
    if [ "$newname" != "$name" ]; then
        mv -iv "$name" "$newname"
    fi
done

Your "'"$file"'" actually leaves $file completely unquoted (surrounded by single quotes, but these do not quote the variable or its value since they are themselves quoted).

See also:


Regarding your sed bit.

sed has a neat little command that many seem to overlook called y:

sed 'y/?"*:/    /'

This would change all characters in the first part of the expression to the corresponding character in the second part (here all spaces).

tr works too:

tr '?"*:' '    '

But in this case, it would be more convenient to use the built in parameter substitution facility of bash to make the changes needed.

0

You have some strange stuff happening with you quotes.

Start by changing the "'" to "

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.