32

This is the output of ls -l

ls -l
total 53484
drwxr-xr-x 3 root root     4096 2011-02-10 05:59 ~
-rw-r--r-- 1 root root 54313810 2011-02-13 05:09 jobs.jar
-rw-r--r-- 1 root root   384035 2011-02-15 05:33 jobsLog.out

I can't do rm -rf ~ because that will remove my home directory.

What should I do? Its not a problem for me, but just an eye-sore.

Kees Cook
  • 17,243
  • 9
  • 68
  • 96
theTuxRacer
  • 15,945
  • 22
  • 71
  • 91
  • 8
    I'd avoid writing `rm -rf` on something with such potential for failure if you mis-type. Consider entering your `./~` folder and removing its contents from there before moving up a directory and running `rmdir ./~` to remove the ~ directory itself. – adamnfish Feb 15 '11 at 10:52

5 Answers5

60

rm -R ./~

That will make it look for ~ in the current folder.

Ward Muylaert
  • 3,864
  • 2
  • 23
  • 31
  • wow, way to look at things straight! kudos! – theTuxRacer Feb 15 '11 at 11:29
  • 2
    This answer is correct, but I think Oli's answer is better because it is safer. If you slightly mistype this command, you will permanently trash your system. – Dave Apr 02 '13 at 13:00
44

I've made silly mistakes with rm before so here are a few tips I've learnt over the years to try and keep you data safe from accidents:

  1. Use a graphical solution like Nautilus. Soft-delete it to the trash. Then when you know you haven't moved your $HOME into the trash (everything would have started crashing and looking funky), empty your trash.

  2. Move instead of delete. Rename the directory with mv, eg:

    mv ./\~ ./a-nice-sensible-directory-name
    

    Then delete it.

  3. If in doubt, use the -i flag when dealing with potential fubars. It will prompt you for every file removed and should let you very quickly know if something bad is going to happen.

    oli@bert:~/Desktop$ rm -rfi ./del/
    rm: descend into directory `./del'? y
    rm: remove regular file `./del/output2.pdf'?
    
Oli
  • 289,791
  • 117
  • 680
  • 835
  • 1
    cant GUI, its a ssh terminal :P Good idea about the `mv`. I think I should make the rm alias with a `rm -i`. – theTuxRacer Feb 15 '11 at 11:28
  • 7
    You can bung `ssh://user@ip/folder` into nautilus. Doesn't work for everything (eg if you need to sudo), but there you go. Aliasing `-i` is a double-edged sword. It's helpful but it's also vastly irritating when you do lots of files. Consider `-I` too. And look at `man rm` for more tips. – Oli Feb 15 '11 at 11:32
  • +1 for the move idea. Now I wonder why there isn't a drop-in replacement for `rm` that moves to (say) ~/.Trash. – ShreevatsaR Feb 16 '11 at 05:27
19

Brilliant problem :)

You can delete the directory by escaping the tilde:

rm -rf \~

This works for all sorts of special characters.

Stefano Palazzo
  • 85,787
  • 45
  • 210
  • 227
17

You can simply cake the folder name into apostrophes:

 rm '~'
alexia
  • 225
  • 1
  • 10
Yuriy Voziy
  • 1,105
  • 1
  • 8
  • 15
6

Just another, a little more complex, way to do it is using inode numbers:

$ ls -li
total 24
 7146369 drwxr-xr-x   4 user  staff   136 Jan 19 21:50 ~
$ find . -xdev -inum 7146369 -exec rm -rf {} \;

Pros

  • It works with wathever fancy name you can have.
  • Should be safe because inode numbers are unique (-xdev: Don't descend directories on other filesystems) and you can test the search first, just in case, removing -exec rm -rf {} \;.

Cons

  • Doing find . in a directory with a lot of files and/or directories will take a lot of time, and disk reading.
superfav
  • 161
  • 1
  • 4
  • I was always interested in inodes, and was wondering whether it was possible to do it =) – theTuxRacer Feb 16 '11 at 05:33
  • Of course, this only works if your current filesystem has the concept of an inode (not all the filesystems Ubuntu supports have this concept -- though most of those that do not are not native Unix filesystems like vFat and NTFS) – Billy ONeal Feb 16 '11 at 16:19