W. Chris Shank on 29 Jun 2007 20:00:35 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] delete unusual file name


Thanks for the concise explanation Paul. It's posts like these that get archived and saved forever. Thanks for this.




----- Original Message -----
From: Paul L. Snyder <plsnyder@drexel.edu>
To: Philadelphia Linux User's Group Discussion List <plug@lists.phillylinux.org>
Sent: Friday, June 29, 2007 11:36:12 AM GMT-0500
Subject: Re: [PLUG] delete unusual file name

On Thu, 28 Jun 2007, Mag Gam wrote:

> What about,
>
> find . -name "*thisfilewontdelete*" -exec rm {} \;
> (not tested, but I think you get the idea)

To sum up, three of the suggestions:

% rm \-foo
% rm "-foo"
% rm -i ?foo

will not work, because of the way the shell and UNIX option parsing are
designed.  First the shell mucks with your line by performing expansion
and so on, and then passes it to the program.  Wildcards like '*', for
example, are expanded by the shell so the program is supplied with a
list of files.  If you have a wildcard or other special character in the
filename, the first two work well.  (Note that this is not how DOS and
the Windows XP command interpreter work; on that platform, executables
must explicitly support wildcards.)  The third works well if you have a
file that has a non-printable character in the name that you can't type
on the keyboard.  The '-i' puts rm into "interactive" mode, so you get
prompted before deleting each file...always a good idea when using
wildcards.

'-' isn't a special character, so it is passed through to the program
unchanged.  It's the program's (in this case rm's) option parsing code
that's causing the problem.  It assumes that any character string after
a single dash is a list of single-character options, so

% rm -r -f foo
% rm -rf foo

are identical.  Any of those three command-lines, after having been
munged by the shell, are equivalent to

% rm -foo

and the GNU coreutils rm doesn't support a '-o' option, and the
command-line doesn't have a filename even if it did.  The other three
suggestions made in this thread work:

% rm ./-foo
% # works because the argument doesn't start with the '-' character.
% # This is actually suggested by rm on my system when I try to remove
% # a filename starting with a dash.

% find -name "*foo*" -exec rm {} \;
% # works because the command this generates is actually 'rm ./-foo'

% rm -- -foo
% # works with GNU coreutils rm because '--' tells the option parser
% # for rm to stop parsing options and interpret the rest of the
% # line as filenames.

Paul

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug


--
W. Chris Shank
ACE Technology Group, LLC
www.myremoteITdept.com
(610) 640-4223

--------------------------------
Security Note: To protect against computer viruses, 
e-mail programs may prevent sending or receiving 
certain types of file attachments. Check your e-mail 
security settings to determine how attachments are 
handled. 
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug