0

This question already has an answer here:

I've noticed sometimes I touch a file that does not exist at all and after the command, the files get created. However, depending on what directory I'm touching, I get "No such file or directory" error.

For example, If I run touch foo.txt, the foo.txt file gets created in my home directory. Instead, if I run touch /bar/foo.txt, I get touch: cannot touch '/bar/foo.txt': No such file or directory.

What's the logic behind this? Thanks.

marked as duplicate by don_crissti, thrig, Christopher, Stephen Rauch, Michael Homer Oct 18 '17 at 23:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Does /bar directory exist? That's what touch complains about. – Arkadiusz Drabczyk Oct 18 '17 at 22:55
  • A NFS mount gone awry (or similar flakey filesystems) could be another cause. – thrig Oct 18 '17 at 23:06
0

The touch command doesn't need the target file to exist, but it needs the parent directory of the target file to exist. So if you want to run touch /bar/foo.txt then you need to make sure that the /bar/ directory already exists.

The touch command (like most command-line programs) also assumes that the path argument that you give it is a relative path. So touch foo.txt will create a file in your current working directory. If your current directory happens to be your home directory then foo.txt will be created in your home directory.

0

You are getting "No such file or directory" error because /bar/foo.txt is looking in root/bar/foo.txt. From your home directory try "bar/foo.txt" (not /bar/foo.txt). Assuming that bar directory exists in your home directory

Not the answer you're looking for? Browse other questions tagged or ask your own question.