8

I don't know how I've done this but I've created somehow a folder named "-p". Now I'm trying to delete it but my buntu is thinking I'm passing -p as a paremeter.

What can I do?

Ubuntu Server 15.

  • Agree the duplicate flag. Sorry. The S.E. "Question Title Suggestion Machine" does not show me the dup because I don't use any of its terms (delete folder strange versus commands with dash parameter). – Magno C Nov 16 '16 at 12:44
  • 1
    No need to be sorry - the next person using the same search terms as you will find your post AND the one it links to, so your question makes the site better :) – Zanna Nov 16 '16 at 12:54
12

You can use -- to tell rm (and many other commands including many shell built-ins) not to interpret any further input as command parameters, so that -p can be interpreted correctly as an argument instead of an "unrecognised option"

rm -- -p

(This is also a good safety measure when globbing. You might have accidentally created a file called -rf...)

  • 3
    "You can use -- to tell bash not to interpret any further input as command parameters" — Isn't it actually telling rm not to interpret any further parameters as options? – Tanner Swett Nov 15 '16 at 0:43
  • -- is interpreted by rm, not bash. And while there is a recommendation in the SUS for tools to accept this special separator, there is no requirement to do so, and not all of them do. dd is well known for having a radically different syntax from what we now consider to be "standard" syntax (i.e. GNU getoptlong), for example. – Jörg W Mittag Nov 15 '16 at 0:58
  • I'll try this ASAP. – Magno C Nov 16 '16 at 12:37
  • @Zanna thanks for the answer but as this is a duplicate question I'll flag to delete. – Magno C Nov 16 '16 at 12:47
  • @MagnoC there's no need to delete - it's still a useful question/signpost. we don't delete dupes unless they are bad :) – Zanna Nov 16 '16 at 12:48
6

the proper way in this case is :

rm ./-p

-- may work with some commands, and fail with others. it is not bash that interprets it, but each command separately (and some may not recognise -- as the end of options) (especially true if you ever use non gnu commands... for example if you work on some other OSs).

Taking the habit of saying ./somefileorglob instead of just somefileorglob is a good habit, in general.

  • 1
    Yes a very good habit. Plus you need ./ to run a script in the current directory too. Whereas -- script-name doesn't work. Plus -- is a fringe thing hard to remember. – WinEunuuchs2Unix Nov 15 '16 at 3:03
  • As I can remember ( I'm at work now and the problem is at my home machine) I've already tried this and not worked. Including rm -rf "./-p"(double quoted) – Magno C Nov 16 '16 at 12:36
  • @MagnoC: this is strange... maybe there are additionnal ("invisible") characters in that name? Try in that case first to : ls -l ./*-*p* | cat -ve may help in most cases: -v will make cat make some characters visible (^M, for example) and combined with e it will also mark the end of each lines with "$", which helps when the line has trailing spaces or tabs. This can help determine the name. or use "od" on the output. You can also use Tab completion: ls -ld ./- and press TAB to let bash complete the rest (or show a selection if there are several candidates). CAREFUL: Tab, not SPACE – Olivier Dulac Nov 16 '16 at 18:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.