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.

1
  • 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
60

rm -R ./~

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

2
  • wow, way to look at things straight! kudos! 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.
    – HDave
    Apr 2 '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'?
    
3
  • 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. 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. Feb 16 '11 at 5:27
19

Brilliant problem :)

You can delete the directory by escaping the tilde:

rm -rf \~

This works for all sorts of special characters.

17

You can simply cake the folder name into apostrophes:

 rm '~'
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.
2
  • I was always interested in inodes, and was wondering whether it was possible to do it =) Feb 16 '11 at 5: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) Feb 16 '11 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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