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

4 Answers 4


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? Oct 26, 2010 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). Oct 26, 2010 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 ... Oct 26, 2010 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, 2010 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, 2015 at 15:58

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

$ ln -s file symlink
$ rm file
$ touch symlink
$ touch: cannot touch `symlink': 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, 2018 at 9:14

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.


Maybe this will help someone in the future:

I got this same issue when I removed the only file which exists in my folder with git rm. Thus git removed the folder in which I was, and any attempt to create a file triggers this error because the folder is no longer there. So I had to recreate the folder manually, go into it and run the touch command successfully.

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.