Pull out a file named to ~

filenamesquotingrename

So I have downloaded a music album from aMule and it is located in the .aMule/Incoming directory. I tried to move it out with the following command:

 mv albumName.rar ~

This left me with a file ~ in .aMule/Incoming which I could not rename because tilde is reserved for home directory. I know I can access it via Nautilus by showing hidden files. How can I pull it out in terminal?

Update
This is how it looks like now

   manuzhang@manuzhang-R458-R457:~/.aMule/Incoming$ ls -l
   total 328
   -rw-r--r-- 1 manuzhang manuzhang 297266 2012-03-19 12:07 ~
   -rw-r--r-- 1 manuzhang manuzhang  34479 2011-10-11 19:51 [kat.ph]friends.season.1.with.english.subtitles.torrent

Best Answer

mv \~ ~/albumName.rar
mv '~' ~/albumName.rar
mv ./~ ~/albumName.rar

You can always protect a character that has a special meaning in the shell by putting a backslash before it.

You can always tell the shell to interpret a sequence literally by putting it inside single quotes. The only character that you can't put inside such a literal string is the single quote itself, since it indicates the end of the literal string. You can use '\'' instead; it means “end of literal, the character ', start of literal”, but you can think of it as a weird way of putting a single quote inside a literal string within single quotes.

The character ~ is only special at the beginning of a path, so if you put a directory indication, ~ will be the name of the file in this directory.

Note that if the file name begins with -, only the last of these three methods will work. This is because - is not special to the shell; it has a special meaning to the command: it indicates an option. Another way of protecting - against this special meaning is to put the special option -- before it: -- on a command line indicates the end of the options, only file names (or other non-option arguments) may come after it.

Note also that the command you show should have copied the file to your home directory, as the ~ character should have been interpreted by the shell. It's possible that you mistyped and created a file that's not called ~. Run ls -q or ls -Q to see if the file name contains an unprintable character. If that doesn't provide any visual indication, try ls | od -t o1, which will show the octal code for each byte in the file name. In ksh, bash or zsh, you can use $'\123' in a command line to specify a character by its octal code. Alternatively, you may be able to find a pattern that matches the file. For example, if you determine that the length of the file name is 3, and that the only other files whose length is 2 are foo and bar, you can move the file with

mv ./[^bf]?? ~/albumName.rar

And if you're willing to use the mouse and the name is composed of printable characters that you don't know how to type: copy-paste the file name from the output of ls.

Related Question