• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

how toremove files or directory in unix with name of special characters  RSS feed

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a directory name "-direc", when I try to remove it with the command rm -r -direc I could not, is there any way we can remove directories named with special characters.

Thanks,
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way I've found to do this in a cross platform way is to do an "rm -i *". This puts rm into an interactive mode. You will type 'n' for all of the files except the one you want to remove.
 
Bartender
Posts: 19668
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes. A favorite nasty trick as college CS lab is to slip a file named something like "*.*" into someone else's home directory.

Try the single-quotes version:

rm '-foo'

Single quotes remove just about all magic.

Double-quotes keep the magic in bounds (for example: rm "Program Files/*" understands that the directory name is "Program Files" and not delete file named Program and files in directory named Files)

Then there's the "backticks":

ls -l `which bash`

will execute command "which bash" and thus list the file characteristics of the bash command.
[ October 27, 2004: Message edited by: Tim Holloway ]
 
senthil sen
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

I actually had a directory created as "-test" and a file "-"

I used these command to remove, hope this will also help others too.



rm ./-filename --> Files that begin with a dash can be removed by typing


rm -- -filename --> Files that begin with a dash can be removed by typing

rm - -filename --> Files that begin with a dash can be removed by typing

rm -i * --> A file with no filename.This will prompt and press yes for blank file
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you solved this, but I thought I'd just pipe in with a few comments for posterities sake

-- is quite common to indicate the end of command line options so you can safely use - to start an actual string in other command line arguments (a file name in this case).

Another option is to use \ to escape a single character:

$ rm \-test

Layne
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "--" trick is the one that will work; no kind of quoting or escaping will mater for filenames that start with a dash, since those approaches merely prevent interpretation by the shell, and dash isn't a character that is interpreted specially by the shell in the first place: it's the command (rm, for example0 that thinks "-test" looks like an argument.

What I generally do, since I've always got an Emacs window open, is use Emacs' "dired" mode, which lets you mark and delete any file no matter how crazy its name is.

Since this is JavaRanch, another alternative, of course, is something like

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
The "--" trick is the one that will work; no kind of quoting or escaping will mater for filenames that start with a dash, since those approaches merely prevent interpretation by the shell, and dash isn't a character that is interpreted specially by the shell in the first place: it's the command (rm, for example0 that thinks "-test" looks like an argument....


Good point. Thanks for pointing that out, Ernest
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!