"No such file or directory" when trying to remove a file, but the file exists?
Solution 1:
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/
Solution 2:
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 pipingls
'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
tellsls
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
tellsls
to print escape sequences for any special characters. The output ofls -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
(anddir
) displays strange filenames.
Solution 3:
-
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*"
-
If ok, than use
find
with the search term in step 1 andrm
find . -maxdepth 1 -type f -name "*qyxdshyikfr*" -print0 | xargs -0 rm
Solution 4:
Reposting in detail, expanded from my comment on Eliah's answer
The problem is invisible, but can be seen if you know what to look for: The file name includes a space at the end. Because you copy/pasted the entire ls
output, it can be seen in the question if you highlight the output, or edit the post and move the cursor to the end, or (as Eliah pointed out) look at the diff in the edit history. I highlighted the ls
output in the post in this screenshot:
A quick little terminal session to duplicate the problem, with comments:
$ touch 'foo ' # Create file with a space at the end
$ ls -l # Space is not visible in ls output
total 0
-rw-rw-r-- 1 izkata izkata 0 May 14 21:59 foo
$ rm foo # Cannot remove it when not specifying the space
rm: cannot remove ‘foo’: No such file or directory
$ rm 'foo ' # Can remove it if we quote the file name and include the space
$ rm foo\ # Or can escape the space to tell bash to include it as part of the filename
Using tab completion would have also completely sidestepped the problem here, as bash is smart enough to escape spaces correctly (It's also a good habit in general, speeds up typing paths so much).
For example, had I typed rm f<tab>
, it would have auto-completed to rm foo\<space><space>
, as in the last example in the code block above.
Solution 5:
So I had this problem and none of these things worked for me. What worked was creating a file with the exact same name. It was a folder named Example.1.2.3 so I created a new folder and named it the exact same as the one that wouldn't delete. The old folder disappeared and I deleted the new one.