0

When creating a new directory using the mkdir command, I can simply add the -v option to find out whether or not the new directory is created. It saves time on not issuing a ls -F command. Is there any way to ensure that a new file was created using the touch command? Or should I use another command instead of touch?

Dsaki
  • 85
  • I understand it's not about defensive programming in an automated script; it's about ensuring the user. Right? When you say "to ensure that a new file was created using the touch command", do you mean "was created exactly by this touch command"? as opposed to a file that already existed? I'm asking because ls -F cannot tell you if the previous mkdir -v actually created a directory or found it existing; but the output of mkdir -v is different in the two cases. So it's not clear if you want to ensure that "dir/file gets created exactly by the command" or "it exists after the command". – Kamil Maciorowski Jul 25 '20 at 06:13
  • I want to know that the file exists after the command is executed, because I couldn't find any sort of --verbose option in the man page for touch that does that for me. – Dsaki Jul 25 '20 at 06:20
  • In a sane system you may assume that mkdir/touch exiting silently means there were no problems. If there were problems the tool knew of, then it would print an error message. If there were problems the tool didn't know of (artificial example: an insane filesystem pretends all actions are successful but no file gets created ever), then no option would make the difference (unless the option was to verify independently). What's wrong in seeing no error message? I think the rationale behind mkdir -v is not to ensure; it's to get a list you can parse. To ensure, "no error" should be OK. – Kamil Maciorowski Jul 25 '20 at 06:40
  • Thanks for your clear explanations. So in fact when touch exits silently, this means that it worked. The mkdir -v rationale was also a good point. – Dsaki Jul 25 '20 at 06:48
  • 1
    Well, I expect common tools in nix to follow the Unix philosophy. There is no single set of rules, but in some set(s) you can find these: Rule of Silence: When a program has nothing surprising to say, it should say nothing. Rule of Repair: When you must fail, fail noisily and as soon as possible. The philosophy is not formally imposed, nix guys just feel this is The Right Thing. – Kamil Maciorowski Jul 25 '20 at 07:01

1 Answers1

2

That's what the exit status is for.

touch is the command to update the time stamps of a file or create it if it the file didn't exist in the first place.

It will return success in its exit status if it fulfilled that goal, and failure otherwise¹. If it failed, it will generally output an error message about it to detail in which way it failed.

if touch -- "$file"; then
  printf '%s\n' "$file was created or updated"
fi

In your script, if you want to make sure you don't carry on if touch did not succeed, you'd write it:

touch -- "$file" || exit

(here the script would exit with the same failure exit status as that reported by touch).

Or add a else exit in the if statement above.


¹ touch could also report failure even though it managed to update the file in some pathological cases, like it was killed or ran out of some resource. In most of those cases, it's probably just as well for your script to consider it a failure to update the file