How to remove a file with unprintable charaters in Mac OS X Terminal

7

1

Possible Duplicate:
Removing file with strange characters in filename in OS X

I have a folder that has a file who's name is Icons and ends with an additiona unprintable character. I can see the unprintable if I ls with -B option:

$ ls -B
$ Icon\015

if I use ls -b I get:

$ ls -b
$ Icon\r

I would like to delete file but I cannot find a way to type the unprintable character. If I do:

$ rm Icon\015

I get:

$ Icon\015: No such file or directory

If I do:

$ rm Icon\r

I get:

$ Icon\r: No such file or directory

I could just delete the whole folder, but I need a more generic way to do this since such files pop up on many users folders.

Periodic Maintenance

Posted 2012-12-25T12:57:49.063

Reputation: 171

Question was closed 2012-12-26T03:40:14.233

2If there are no other files that start with Icon, try rm Icon*.

Alternatively, type rm Icon and then press the <Tab> key (it would complete the filename, if there is only one file starting with Icon). – lupincho – 2012-12-25T13:02:31.573

@lupincho: The <Tab> key method does work, I actually get Icon^M this way. But I need something I can write into a shell script to automate the removal of such files. BTW: Typing Icon^M does not work.... – Periodic Maintenance – 2012-12-25T13:21:05.973

What happens if you try rm -i Icon*?, Also, are there other files staring with Icon in the same directory, but that you don't want to delete? – lupincho – 2012-12-25T14:14:12.690

@lupincho: rm -i Icon* will work, howeve it will require operator attention. There could be other files starting with Icon, so I cannot take chance that these otehr files will be erased. – Periodic Maintenance – 2012-12-25T14:30:43.813

These files contain a user-specified folder-icon. Why would you want to remove them in the first place? After all it's a users decision to give that folder a customized look. The name contains that character on purpose so that it is not easy to remove. – heiglandreas – 2012-12-26T06:38:13.523

Did you try putting the name into single quotes? rm 'Icon\r' should do the trick. – heiglandreas – 2012-12-26T06:47:03.653

Just single quotes will not do. You need the '$' as @Gordon Davisson mentions in an answer below. – Periodic Maintenance – 2012-12-26T10:42:24.343

Answers

3

The shell will automatically transform every CR (\r) in a LF (\n) and execute the preceding command. However, you can use echo to produce a LF character. All of these should work:

rm $(echo -e "Icon\r")

rm $(echo -e "Icon\015")

echo -e "Icon\r" | xargs rm

echo -en "Icon\r" | xargs -0 rm

The last option should be fairly robust and deal with all possible strange characters.

Dennis

Posted 2012-12-25T12:57:49.063

Reputation: 42 934

The Icon is a file not directory, so your method work, replacing rmdir with rm. This method could work in shell scripts so it does the work for me.The -e option to echo does not appear in the man page, how did you come by it? In any case I will accept the answer - thanks! – Periodic Maintenance – 2012-12-25T16:42:18.080

Right, I misread the question's first sentence. men echo shows -e enable interpretation of backslash escapes. – Dennis – 2012-12-25T18:44:33.827

2

There are several ways to include non-printing characters in a command line. The simplest (bash-only) option is to use $ before a single-quoted string, which makes bash do escape substitution within the string. Note that it handles several kinds of escape sequences, so both of these would work:

rm $'Icon\r'
rm $'Icon\015'

Or, you can type Control-V before Control-M (aka return), which tells the shell "the next character I type should be included literally in the command":

rm Icon^V^M

(Note that the ^V isn't really part of the command, so it won't echo on the command line).

Gordon Davisson

Posted 2012-12-25T12:57:49.063

Reputation: 28 538

The $ method works as well as the echo -e method. It's a good solution - thanks. – Periodic Maintenance – 2012-12-26T08:23:35.370

0

For a one unprintable character you can use '?' as a wildcard for one character. So, abc?def?.zip would mean abc1def2.zip and also abc!def#.zip.

pbies

Posted 2012-12-25T12:57:49.063

Reputation: 1 633