0

When exiting a file as root, I typed :x!~ and hit enter. Now I have the following:

root@host:/etc/shinken/services$ ll
total 64K
-rw-r--r--  1     0     0  515 Sep  1 18:24 ~
drwxr-xr-x  2 10003 10003 4.0K Sep  1 18:24 .
drwxr-xr-x 29 10003 10003 4.0K Aug  8 17:11 ..
<other files omitted> 

If I cd ~, it takes me to /root as expected. How can I delete the new ~ without accidentally deleting my actual home?

3
cd /etc/shinken/services
rm ./~

Not much to say about that really. By specifying ./ in front of ~ you stop the shell from performing tilde expansion, and it will be treated as the name of a file in the current directory.

Another option would have been

cd /etc/shinken/services
rm '~'

or just

rm /etc/shinken/services/~

In Vi and Vim, :x will work like :wq and write the file before quitting. By appending a filename (~ in this case), the file with the given name will be written. The difference between the two commands is that :x only writes the file if the current buffer has been modified since the last save. The ! overrides writing to a read-only file.

  • oh... right.... duh.. :) – MrDuk Sep 1 '17 at 18:39
1
rm -- '~'

The single quotes around the ~ stop bash from expanding the path and simply passes it to rm.

-- stops rm from interpreting any more arguments as flags and assumes they are all files (no so important in this case but useful if you have a file with a leading -).

You can test this with ls first.

ls -- '~'

For example in a directory that does not contain a literal '~' you would get

ls: cannot access '~': No such file or directory

Showing that ls has received the path unmodified.

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.