Home > Software engineering >  Using Terminal (OSX) to remove "?" character at end of basename
Using Terminal (OSX) to remove "?" character at end of basename

Time:12-23

I have a list of files with an unknown character at the end (shown as a "?")

My goal is to

  • remove the unknown character in the basenames shown as ? for every file in the folder

Here is the directory/folder path where the files are located:

/Users/MyName/Documents/InfoNotParsed

Here are the names of the files in the folder:

TextInformation?.txt
TextInformation2?.txt
TextInformation3?.txt

Here are the commands I am attempting to run in the terminal in order to remove the "?" at the end of the base name before the extension:

cd /Users/MyName/Documents/InfoNotParsed

for f in *; do mv — “$f” “$f//\?/}”; done

I am stuck at the command line saying:

for dquote>

Here is the entire Terminal text in the shell:

MyName@Name-MBP ~ % cd /Users/MyName/Documents/InfoNotParsed 
MyName@Name-MBP InfoNotParsed % for f in *; do mv -- "$f" "${f//\?/}; done
for dquote> 

Any advice?

CodePudding user response:

You are trying to match an unknown character using a ?, but the ? is not there it’s just the terminal displaying an unknown using one.

Try listing what you wish to keep and using tr to remove the complement, something like:

for f in *.txt; do g=`echo -n "$f" | tr -C -d "\40-\177"`; mv -- "$f" "$g"; done

That is far from foolproof but may work for your particular situation.

  • Related