I am trying to remove a png image that was uploaded to my server via a PHP script. Whenever I try to delete it both through ftp and terminal, I get the error
No such file or directory
However, when I ls
in the dir, the file is listed and it is also listed in my ftp client. I have tried creating a file with the same name and I end up getting two files with the same name.
I can open the file that supposedly does not exist, but I still can't remove it. I have also tried rebooting my server. Any ideas what may be the issue? I am running a 64 bit version of Ubuntu, but I don't think its a 32/64 bit issue. I should also note I have removed many other png files uploaded by the same PHP script.
Output for ls -l
total 224
-rw-r--r-- 1 www-data www-data 222838 May 13 04:14 qyxdshyikfr_fishing_timeout.png
-rw-r--r-- 1 root root 272 May 14 06:54 upload.php
Output when trying to rm
rm: cannot remove ‘qyxdshyikfr_fishing_timeout.png’: No such file or directory
upload.php: http://pastebin.com/z87eypTY
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 tols
is your friend in such instances, as are tools such ascat -v
.So, too, is
rm -i -- *
Further reading
TL;DR: Run
ls -1b
, find the filename, copy the line on which it appears, and give that torm
.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
(ordir -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 byls
, 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, typerm
, type a space, paste the line, and press Enter.Further reading:
ls
: List directory contents in the GNU coreutils reference manual.ls
(anddir
) displays strange filenames.Use
find
and check the output:If the file is not found, then shortening the search term
*qyxdshyikfr*
slightly, eg:*qyxds*
or*fishing*
.If ok, than use
find
with the search term in step 1 andrm
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.
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 thels
output in the post in this screenshot:A quick little terminal session to duplicate the problem, with comments:
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 torm foo\<space><space>
, as in the last example in the code block above.once, i've created a file to open Nautilus as root, but the file name when seeing from nautilus was "File Browser (root)", then when i tried to remove as
the only anwser i got was: "rm: cannot remove 'File Browser (Root).desktop': No such file or directory"
then when i run:
i saw/remembered that the file name, in fact, was "Nautilus-root.desktop"
so i run:
worked for me, hope it helps !
I had a similar situation, after using
rsync
to backup my Pictures directory on a Mac and reading it on Ubuntu. There were two files (actually directories) with different names, but having the same content. I deleted one to the Trash (using Nautilus), but could not delete the other, even from the command line. It would say:After checking the inode numbers with ls -i -l it turned out that both directories have the same inode number. Looks like a hard link...
The solution was surprisingly simple - empty the Trash by right clicking on the icon. After that both directories were gone.