7

I've created a file named \;:$"\' to test a software of mine. I ended up with an error, because I cannot delete the file property. I'm trying to find a precise character combination to remove it via rm, but I cannot find a way.

rm \\;:$\"\\\'
rm: cannot remove `\\': No such file or directory

rm "\\"\;:$\"\\\'
rm: cannot remove `\\;:$"\\\'': No such file or directory

rm '\;:$"\'''
rm: cannot remove `\\;:$"\\': No such file or directory

(This last try killed me)

And many many other attempts. Helping hand needed!

  • 2
    possible duplicate of bash - directories ending in . and whitespace cannot be removed -- while the question isn't exactly the same, the same techniques can be used for removing any file with a weird name. – user Mar 24 '15 at 13:06
  • I'll take a look. This case is far more complex as back-slash is not enough to parse otherwise straight parsing chars as the whitespace. – Alex Mar 24 '15 at 13:09
  • The easy general technique for removing files with awkward names is rm -i ./* which will prompt you to choose whether or not to delete each file in the directory. If you can type a more specific glob, do -- in this case I'd try rm -i ./*:* as : is not meaningful to the shell in this context. – zwol Mar 24 '15 at 16:46
  • @MichaelKjörling I'm sure this question has been asked and answered before, but the particular dupe candidate you found is not a good choice. It seems to have attracted a bunch of incorrect answers which may have misled you -- it's the nastier case, "I somehow created a file whose name contains characters that the filesystem does not normally permit, and now I can't get rid of it." – zwol Mar 24 '15 at 16:49
12

If the files name is exactly \;:$"\', then you should be able to remove it with:

rm \\\;\:\$\"\\\'

Just ecape all the characters with a single \.

  • Thanks Chaos. I wanted to avoid backslashes everywhere (plus, I'm afraid this method could fail for some GNU commands). The correct polished answer would be: \\\;:$\"\\\' but I'm taking yours as valid. – Alex Mar 24 '15 at 13:23
  • 4
    @Alex if your program's environment has a library function to shell-escape strings, you should use that. If you're in an interactive shell environment with tab-completion, I find that extremely handy for operating on files with weird names! – Riking Mar 24 '15 at 17:56
  • I know. I think is logical. But not the case, sorry. – Alex May 8 '15 at 10:52
5

You can try ls -li in the directory containing the file and deleting the file with the inode returned by issuing find . -inum <inode-number> -exec rm -i {} \;

I added the -i to the find command to prompt before deletion in case another file is found by the find command than you expect.

In addition to the comment I tried to create and remove the file myself:

ls -l
total 0
-rw-r--r-- 1 test test 0 mrt 24 14:11 \;:$"\'

[test@testhost +1] /tmp/ff$ rm \\\;\:\$\"\\\' 

[test@testhost +1] /tmp/ff$ ls -l
total 0
  • I would rather use the proper bash command. I could use the inode marking as a last attemp but I would rather focus on the bash command. – Alex Mar 24 '15 at 12:56
  • In my case rm \\\;\:\$\"\\\' worked – Lambert Mar 24 '15 at 13:13
  • 1
    Note, if the file has multiple (hard) links, this will remove all of them in the directory. – Brian Mar 24 '15 at 22:36
  • Hence the -i to allow you to only 'select' the file which you actually want to remove. – Lambert Mar 26 '15 at 6:25
2

You can use single quotes, and then you only have to worry about quoting the single quote itself.

rm '\;:$"\'\'

In interactive use, you could simply use tab completion, starting with '\ or \\. Tab completion from '\ yields '\;:$"\'\''', since bash simply replaces every embedded single quote with '\''. Tab completion from nothing or from \\ yields \\\;\:\$\"\\\'.

2

In bash and similar shells

read -r; rm "$REPLY"

then just type in the filename literally, \;:$"\', and press Enter. The read command reads a line from standard input and saves it to the variable REPLY. The -r flag tells it to read the line literally and not interpret backslashes as escape sequences. Quoting $REPLY ensures that the contents of REPLY will be passed to rm as a single argument so this will also work even if the filename contains spaces or tabs.

(Note: If the filename contains newline characters, apparently you can use the -d option to read to change the terminating character for the string you want to enter.)

1

If you are not limited to using bash or another shell script environment, an easy way to remove such a file would probably be to either write a short program that simply calls unlink() in your favorite language, or use a file manager such as Gnome Commander or Midnight Commander to delete the file manually. The latter option would allow you to select the file in question and delete it without ever having to enter the file name anywhere, which would remove the possibility of metacharacters having any special meaning at all.

0

Try this,

$ rm \\\;\:\$\"\\\'

"\" is used as escape sequence in bash scripting which has a litral meaning.

  • 1
    Hi Vengat, and welcome to Super User. While I realize that your answer was posted very soon after that of chaos, we like for answers to differentiate themselves. Can you perhaps add something to this that was not already in chaos' answer? – user Mar 24 '15 at 13:13
0

I find it easiest in such cases to type/escape at most a few characters and then let bash complete the command with <TAB>: bash then makes sure to quote the stuff suitable for its own purposes.

In this case, if I do

touch '\;:$"\'\'
rm \ <TAB>

the completion for repeated <TAB> subsequently goes to

rm \\
rm \\\\
rm \\\\\\\\
rm \\\;\:\$\"\\\' 

Now I am actually a bit at a loss explaining the intermediate stages, but the final proposal is sensible.

As a rule of thumb when not using completion, I tend to quote everything by including it in '...' marks. Obviously, you then need to write ' itself as '\'', namely end the quotes, write \; to produce a single ' and reenter another quoted string. That's the recipe I used for the touch command except for omitting the final resulting '' since it does not add anything.

0

Another good solution is to install oh-my-zsh,
type rm, and tab over to the file you want to delete. ;)

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.