Creating Files
Learn how to create files using the touch command.
We'll cover the following
In Creating and Reading Files, we learned how to use the echo
command and redirection to create files. This is one of many ways to create files on the command line. Let’s look at a few alternatives.
The touch
command
If we only need to create an empty file, we can use the touch
command. This command is designed to update the timestamp of a file. It’s common for programs to watch a file for changes and then react to them by executing a process. For example, we might have a process that runs tests whenever a file changes, but we don’t actually want to open the file and make a change. We can use touch
to modify the file without actually changing the contents. This would then trigger the program or process monitoring the file.
However, if the specified file doesn’t exist, the touch
command creates the file. This makes touch
a very popular tool for creating files quickly.
$ touch file.txt
We verify that it exists by using the ls -lh
command:
$ ls -lh file.txt
Get hands-on with 1400+ tech skills courses.