Why isn't there any shell command to create files?

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.


Adrian Frühwirth's answer is right on. I just wanted to add that there is actually a command specifically written to create files: mktemp.

NAME
       mktemp - create a temporary file or directory

SYNOPSIS
       mktemp [OPTION]... [TEMPLATE]

DESCRIPTION
       Create a temporary file or directory, safely, and print its name.  TEM‐
       PLATE must contain at least 3 consecutive 'X's in last  component.   If
       TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.
       Files are created u+rw, and directories  u+rwx,  minus  umask  restric‐
       tions.

Granted, mktemp's job is not to create a file with a specific name, it is simply to create a file. However, as you've already been told, there are so many more efficient and elegant ways to create files with a given name that providing a command for that would be pointless.

That said, you also have truncate and fallocate both of whose essential purpose is to create files. They simply have a more sophisticated approach. There will never be a simple program that does what > file does since there is no way it would be better than > file.


Most basic shell tools are not designed for any very specific purpose at all. Most basic shell tools are designed only to interact with others to achieve your purpose. Or maybe it should be said that most tools do only one very basic thing regardless of how they might be combined to achieve a goal.

: >./file

That creates an empty file. Or truncates an existing file as you will. You can:

set -o noclobber

to avoid any possibility of the latter case unless you:

: >|./file

You can get similar behavior to touch with:

: >>./file
: >>|./file

Except that : will not update a file's modtime because it isn't modified.

DEMO

mkdir test ; cd $_
touch touch.file 
: >null.file
echo >echo.file
ls -l

OUTPUT

-rw-r--r-- 1 mikeserv mikeserv 1 Apr 10 14:52 echo.file
-rw-r--r-- 1 mikeserv mikeserv 0 Apr 10 14:52 null.file
-rw-r--r-- 1 mikeserv mikeserv 0 Apr 10 14:52 touch.file

NOT TOUCH

: >>|./echo.file
ls -l 

OUTPUT

-rw-r--r-- 1 mikeserv mikeserv 1 Apr 10 14:52 echo.file
-rw-r--r-- 1 mikeserv mikeserv 0 Apr 10 14:52 null.file
-rw-r--r-- 1 mikeserv mikeserv 0 Apr 10 14:52 touch.file