Shell Command – Why No Command to Create Files

bashcommand linefilesshell

Attention please:

I am not asking how to make a file from the command line!


I have been using touch for making files for years without paying attention that its main purpose is something else. If one wants to create a file from command line there are so many possibilities:

touch foo.bar
> foo.bar
cat > foo.bar
echo -n > foo.bar
printf '' > foo.bar

And I'm sure there are more.

But the fact is, none of the commands above are actually designed for creating files. For example, man touch suggests this command is for changing file timestamps. Why doesn't an OS as complete as Unix (or Linux) have a command solely designed for creating files?

Best Answer

I would say because it's hardly ever necessary to create an empty file that you won't fill with content immediately on the command line or in shell scripting.

There is absolutely no benefit in creating a file first and then using I/O redirection to write to the file if you can do so in one step.

In those cases where you really want to create an empty file and leave it I'd argue that > "${file}" could not be briefer and more elegant.

TL;DR: It doesn't exist because creating empty files most often has no use, and in cases it has there are already a myriad of options available to achieve this goal.

On a side note, using touch only works if the file does not exist whereas options using redirection will always truncate the file, even if it exists (so technically, those solutions are not identical). > foo is the preferred method since it saves a fork and echo -n should be avoided in general since it's highly unportable.

Related Question