"No such file or directory" when trying to remove a file, but the file exists?

I have tried creating a file with the same name and I end up getting two files with the same name.

That says, absent filesystem corruption, that you have two files with two different names that appear the same because of non-printing characters or characters that look the same in your character set/font. The --escape option to ls is your friend in such instances, as are tools such as cat -v.

So, too, is rm -i -- *

Further reading

  • https://unix.stackexchange.com/questions/2182/
  • http://opensourceforu.com/2010/12/secure-upload-methods-in-php/

TL;DR: Run ls -1b, find the filename, copy the line on which it appears, and give that to rm.

As others have suggested, most likely this is due to limitations in the way ls--and some other programs, including client and server software--handle weird filenames, such as those containing control characters, by default. Your success with JdeBP's answer strongly suggests this was the case, though it would've been a good bet even before that.

  • For ls, when standard output is a terminal, ? characters are printed in their place. So if you're not piping ls's output to any other command (or redirecting it to a log for viewing), probably your filename doesn't contain control characters. But there are other problematic characters--perhaps the filename contains trailing whitespace, for example.

    This behavior of ls can be confusing but is not a bug, can can be overridden explicitly by the user (see below).

  • When attempting to access or remove a file remotely, bugs in client or server software can produce such problems.

    I've experienced this sort of thing via ftp myself several times, including for files whose names contain trailing spaces. (That it didn't work was due to a bug in my ftp client.) Even when you manually create a file yourself, depending on how you are creating it, it's sometimes quite easy to inadvertently insert a trailing space, or other whitespace that may look like spaces even though it isn't.

This is a situation where ls -1b (or dir -1) comes in handy:

  • -1 tells ls to show one entry per line. That way there is no confusion about where one filename ends and another begins. This is handy for weirdly named files.
  • -b tells ls to print escape sequences for any special characters. The output of ls -b can be copied and pasted literally into a command, with no added quoting: all problematic characters are already quoted in a way that causes the shell to recognize them as what they are.

There is only one caveat: if the last character on a line appears to be \, copy one character after that, since this means \ is quoting a space.

You can run ls -1b just like that, or you can pass a shell glob pattern to it (e.g., ls -1b qyx*). Globbing may or may not find the file, depending on whether or not the control characters (or other weird characters) are present in the portion of the name appearing in the glob pattern.

Having copied the \-quoted version of the filename given to you by ls, you can paste this into a command. You don't have to modify it manually in any way. In your case, as you wish to delete the file, type rm, type a space, paste the line, and press Enter.

Further reading:

  • 10.1 ls: List directory contents in the GNU coreutils reference manual.
  • My answer to that question, which is very different from your question but touches heavily on issues of how ls (and dir) displays strange filenames.

  1. Use find and check the output:

    If the file is not found, then shortening the search term *qyxdshyikfr* slightly, eg: *qyxds* or *fishing*.

     sudo find . -maxdepth 1 -type f -name "*qyxdshyikfr*"
    
  2. If ok, than use find with the search term in step 1 and rm

     find . -maxdepth 1 -type f -name "*qyxdshyikfr*" -print0 | xargs -0  rm
    

Tags:

Rm