Operating System - HP-UX
1837523 Members
3386 Online
110117 Solutions
New Discussion

Re: I have a file name named ? and I can't delete it!

 
Michael Gretton
Frequent Advisor

I have a file name named ? and I can't delete it!

How can I delete a file named ?. Since the ? is a wildcard it won't let me delete it!

Thanks,

Mike
17 REPLIES 17
John Palmer
Honored Contributor

Re: I have a file name named ? and I can't delete it!

rm -i "?"

Regards,
John
James R. Ferguson
Acclaimed Contributor

Re: I have a file name named ? and I can't delete it!

Hi Michael:

You can escape the "?" with "\":

# rm -i filename\?

...or...

Using the interactive option of 'rm' and wildcards, respond "y" or "n" appropriately:

# rm -i file*

Regards!

...JRF...
Jeff Machols
Esteemed Contributor

Re: I have a file name named ? and I can't delete it!

rm "\?"

Craig Rants
Honored Contributor

Re: I have a file name named ? and I can't delete it!

You can do it inside the find command as well.

find . -type f -name ? -exec rm {} \;

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
John Palmer
Honored Contributor

Re: I have a file name named ? and I can't delete it!

To be pedantic, all the following will work:-

rm "?"
rm '?'
rm \?

rm "\?" will only remove a file called \?

Regards,
John

Darrell Allen
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Hi Michael,

Actually, if the file is named ? it will be removed with: rm ?
Unfortunately, so will all other 1 character named files.

I'm sure you probably meant that you shouldn't use rm ? because it could delete other files as well.

For the find suggestion you need to quote and escape ? like so:
find . -name "\?"
Without quoting it will error. Without escaping it will act as a wildcard.

Use John's suggestions. And when in doubt, include the -i option to be safe.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Ravi_8
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Hi,

#rm \?

never give up
Ravi_8
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Hi,

#rm \?
or
# mv \? x
#rm x

never give up
Sanjay_6
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Hi Michael,

Try this,

cd to the directory where this file is present,

do a
# ls >test_file

edit "test_file" and remove all entries other than the file you want to remove.

insert and "rm " before the file name and then run this file,

# sh test_file

Hope this helps.

Regds
Justo Exposito
Esteemed Contributor

Re: I have a file name named ? and I can't delete it!

Hi Michael,

I make a new directory (for security reasons)and then I move it the file first to this, then I tried to rm \?.

Regards,

Justo.
Help is a Beatiful word
Rita C Workman
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Here's just one more way to remove a file with "character" in name..

cd where file is

ls -i (lists all file #'s)

find . -inum <#from above> -exec rm {} \;


Rgrds,
Rita

..because in UNIX we have so many ways to do the same thing !!
Magdi KAMAL
Respected Contributor

Re: I have a file name named ? and I can't delete it!

Hi Michael,


Just use the following command :

# rm "?"

and your file will be deleted.

Magdi
Frank Slootweg
Honored Contributor

Re: I have a file name named ? and I can't delete it!

While most of the posted solutions work for *this* case, i.e. "?", a more general solution is:

rm [-i] ./?

I.e. dot+slash+difficult_character(s) also works when, for example, the file has a leading minus sign (which rm(1) will interpret as an option, but the dot+slash prevents that).
John Palmer
Honored Contributor

Re: I have a file name named ? and I can't delete it!

rm ./? will still remove every file in the current directory.

If your 'difficult filename' starts with a - then another solution is:

rm [-i] --

where -- terminates the list of command options. This works for every command that uses getopt(3C) for analysing its arguments (most do).

Regards,
John
Steve Steel
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Hi


Try

set noglob
mv \? notwanted
ll notwanted

If ok

rm notwanted


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Frank Slootweg
Honored Contributor

Re: I have a file name named ? and I can't delete it!

John wrote:

> rm ./? will still remove every file in the current directory.

It will only remove every file which has a *single-character* name (but *not* "."). But since these might exist, I said "[-i]", didn't I? :-) I assume(d) that the poster knew what "?" meant, because he said that it was a wildcard.

Thierry Poels_1
Honored Contributor

Re: I have a file name named ? and I can't delete it!

Hi,

does the filename really contain 1 character?? In my experience files listed with "?" mostly contain some escape codes.

so does "ls ?", "ls ??", ls "???", ls "????", ... show the file ???
when found, remove with "rm -i ???" or so.
To play safe, always add "-i" ;-)

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.