20

What could cause touch to fail with this error message?

touch: cannot touch `foo': No such file or directory

Note that an error due to incorrect permissions looks different:

touch: cannot touch `foo': Permission denied
19

Following sequence causes this error message:

$ mkdir foo
$ cd foo

In another terminal:

$ rm -r foo

In the previous terminal:

$ touch x
touch: cannot touch `x': No such file or directory

Of course, other events that also result in invalidating the current working directory (CWD) of a process that tries to create a file there also yield this error message.

  • 2
    In my case, it was occurring because of a misconfigured GlusterFS mount point, but I was curious about the general case that would cause this to happen. Would this type of question really be a homework problem in a class? – Lorin Hochstein Oct 26 '10 at 17:54
  • 3
    The general case is the current directory not existing. Apart from its having been deleted as in maxschlepzig's example, this can only happen in weird circumstances like a misbehaving filesystem (your case), or perhaps a security mechanism like SELinux having made the directory invisible (I don't know if SELinux can actually produce this effect). – Gilles 'SO- stop being evil' Oct 26 '10 at 19:51
  • @lorin: Well, at a Unix introduction course in university we asked such kind of questions as part of homework assignments (because e.g. NFS problems may manifest in similar error messages). Just have asked out of curiosity ... – maxschlepzig Oct 26 '10 at 20:35
  • 3
    In the original answer, no need for another terminal: rmdir ../foo in the same terminal should work. Another possibility without misbehaving filesystems: ln -s directory-that-doesnt-exist/foo foo; touch foo. – ephemient Oct 27 '10 at 3:16
  • I feel like you should edit "Hm, homework question?" out of the answer.... It's honestly very offensive. I'm at this very page because this actually happened on my computer. But this answer doesn't solve that at all. It was an NFS problem that rebooting fixed, but I want the underlying cause. – codetaku Mar 26 '15 at 15:58
3

For me it was trying to write to a symlink that was pointing to a file that was no longer there:

$ ln -s ~/doomed/file ~/mysymlink
$ rm ~/doomed/file
$ touch ~/mysymlink
$ touch: cannot touch `mysymlink': No such file or directory
  • That was it!! Feeling dumb now! Just doing an "ls -la" in the directory the file was supposed to be showed it immediately. It was "resolv.conf", which pointed to a place that due to crashed update wasn't there.. – stolsvik Mar 10 '18 at 9:14
1

Another (simpler) way to trigger the message:

$ mkdir foo
$ touch foo/bar/baz.txt
touch: cannot touch 'foo/bar/baz.txt': No such file or directory

Basically trying to create a file in a non-existent folder.

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.