• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic