6

I'm not quite sure what just happened, all I did was running:

~/Desktop$ mv sublime.desktop \~/.local/share/applications/

The \ sign before ~/.local came up as autocomplete, so I thought it was okay to run it.

But instead of moving the desktop file to /.local/share/applications/ (which was my intention), the command created new folder on Desktop. (The ~ was folder)

Liso@thinkpad:~/Desktop$ ls
~  backup.sql  Apps

When I tried to remove ~:

~/Desktop$ rmdir ~
rmdir: failed to remove ‘/home/liso’: Permission denied

So what am I missing actually ?

EDIT

@Ravexina ask me to ran test command to confirm whether it was a directory or a file.

Liso@thinkpad:~/Desktop$ test -d \~ && echo "it's a dir"
it's a dir`
  • 1
    just move your file to the correct location with mv ~/Desktop/\~/.local/share/applications/sublime.desktop ~/.local/share/applications/ and remove the accidently created folders: rmdir ~/Desktop/\~/.local/share/applications ~/Desktop/\~/.local/share ~/Desktop/\~/.local ~/Desktop/\~ – dessert Aug 15 '17 at 14:40
  • 1
    Related: Tilde (~) inside working unix directory – wjandrea Aug 16 '17 at 2:07
10

You didn't move your home directory...

  • We refer to ~ as tilde expansion, most of the times it will be replaced with the value of the $HOME shell variable, before the command get executed.

  • \ is the strongest type of quoting in the shell.

So using \~ you are skipping the tilde expansion by quoting it. Means that you are actually saying: move the "sublime.desktop" to a new file named exactly "~...".

I can't reproduce your command's result, but somehow you ended up with a file/directory exactly named ~.

Check to see if it's a file or directory and get a list of its contents:

test -d ~/Desktop/~ && ls -l ~/Desktop/~ || echo 'it is a file'

Then move them to the correct path, if it was a file to move it back you have to escape its name again, otherwise it will be expanded to /home/liso:

mv \~ new-name
mv "~" new-name # works
mv '~' new-name # also works
mv ~/Desktop/~  new-name # works fine too

And remember by rmdir ~ you are trying to remove the actual home directory: /home/liso not the ~.

  • 1
    Umm, the ~ wasn't a file like you said. It was actually an folder with .local/share/applicationdirectory inside. – Jim Aug 15 '17 at 13:30
  • Are you sure about the command you ran? – Ravexina Aug 15 '17 at 13:41
  • he's currently on Destop so I think the path is ~/Desktop/~/.local/share/applications/ – phuclv Aug 16 '17 at 1:46
  • @Liso can you give me the output of ls ~/Desktop~? – Ravexina Aug 16 '17 at 3:57
6

The tilde is expanded by the shell to the $HOME of the user, in your case /home/liso. In the first command you escaped the ~ so it was not expanded to the location you wanted, instead it was passed literally to mv as the symbol ~.

I think you wanted to run

mv sublime.desktop ~/.local/share/applications

(with an optional trailing /)

I would expect the command you say you ran to fail like this

mv: cannot move 'sublime.desktop' to '~/.local/share/applications/': No such file or directory

because mv does not create destination directories like that. If you really did run that command, I think you must have already had a directory actually named ~ in your Desktop with that path, ie

/home/liso/Desktop/\~/.local/share/applications

and if so you will now find a file there:

~/Desktop/\~/.local/share/applications/sublime.desktop

And you should run

mv ~/Desktop/\~/.local/share/applications/sublime.desktop ~/.local/share/sublime.desktop

But if you ran

mv sublime.desktop \~

that would create a file ~ because sublime.desktop would be renamed ~. Try reading the file

less ~Desktop/\~

If it contains the contents of your sublime.desktop file, then run

mv \~ ~/.local/share/applications/sublime.desktop
  • pretty clear to go, assumption, solution and some other ways.... – solfish Aug 15 '17 at 14:58
  • I don't think you need to escape ~ in the middle of a path – phuclv Aug 16 '17 at 1:47
2

The tilde character is only expanded to your home directory (among other possibilities) when it is not quoted. Putting a \ character in front of it prevents tilde expansion. When in doubt, use $HOME instead, as it is a regular shell variable with a predictable syntax and behavior.

To remove a directory named ~ (make sure there's nothing of importance in it first), you should use the same trick as before: escape the tilde so it's interpreted literally. Oh, and you'll also need to run rm recursively to remove a non-empty directory:

rm -r "~"

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.