When you are working in the Linux terminal, you frequently need to generate configuration files, empty log files, or placeholder documents for a script to write data into later. While you could open a text editor like nano or vim, type nothing, and save the file, doing so is incredibly inefficient.
The touch command is a core Unix utility designed to solve this exact problem. It allows you to instantly create an empty file directly from the command line in a fraction of a second, without launching a text editor or leaving your prompt.
How to Create a Single Empty File
The syntax for the touch command is incredibly straightforward. You simply type the command followed by the name of the file you wish to create.
- Open your terminal or SSH into your Ubuntu server.
- Navigate to the directory where you want the file to live using the
cdcommand. - Type the following command and press Enter:
touch config.txt
The terminal will pause for a millisecond and return a blank prompt. If you run the ls command to list the directory contents, you will see that config.txt has been created. The file will have a size of exactly 0 bytes.
How to Create Multiple Files Simultaneously
The true power of the touch command is its ability to process multiple arguments at once. If you need to scaffold out a directory structure for a new web development project, you can generate all the necessary files in a single command.
Simply separate the filenames with a single space:
touch index.html style.css script.js
The system will instantly generate all three files in the current directory.
How to Update a File’s Timestamp
While touch is most famous for creating new files, its actual, original technical purpose in the Unix architecture is to update the access and modification timestamps of an existing file.
If you run the touch command on a file that already exists, Linux will not delete the file, overwrite its contents, or throw an error. It leaves the file’s data completely intact.
Instead, it simply updates the file’s metadata to reflect the exact current time.
touch existing_script.sh
This is incredibly useful when dealing with automated backup systems, compiling software using make, or triggering cron jobs that only execute when a specific file appears to have been recently modified.