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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
Method 3
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
Method 4
The utility install is actually designed for creating files! You can pass the content of the file via /dev/stdin (in most cases, however, on most flavors of Linux, this requires that /proc is mounted) or provide another source file. You can set the ownership and permissions.
echo "New file" | install -o 0644 -m 452452 -g dumbass /dev/stdin /var/www/index.html
as a silly example.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0