How to Use Linux Touch Command to Create Files
The touch command in the Linux operating system is
primarily designed to update file access and modification timestamps,
but it is most commonly used as a quick and efficient way to create new,
empty files. This guide explains the core function of the
touch command in file creation, how it handles both new and
existing files, and the essential syntax needed to use it effectively in
daily command-line operations.
The Role of the Touch Command in File Creation
In Unix-like operating systems, the touch command
interacts directly with file metadata. When you run the command targeted
at a file name that does not exist in the current directory,
touch immediately creates an empty file with zero bytes (0
KB). The newly generated file is assigned the current system date and
time as its creation, access, and modification timestamps.
Unlike other file creation methods, such as standard shell
redirection operators (> or >>) or
text editors (nano, vim), touch
does not open a file buffer or require data to be written. This makes it
the fastest standard utility for generating placeholder files.
Basic Syntax and Usage
The standard syntax for creating a file is:
touch filename.txtRunning this command checks the directory for
filename.txt. If it is absent, the operating system creates
it instantly with default user permissions defined by the system's
umask.
Creating Multiple Files Simultaneously
The touch command accepts multiple arguments, allowing
you to create several empty files with a single instruction:
touch file1.txt file2.txt file3.txtYou can also use bash brace expansion to generate sequences of files:
touch document_{1..5}.txtThis command creates five distinct files named
document_1.txt through document_5.txt.
Behavior on Existing Files: Safe Creation
A critical feature of the touch command is that it is
non-destructive. If you run touch on a file that already
exists, the command does not overwrite, truncate, or alter the file's
contents. Instead, it simply updates the file's access and modification
timestamps to the current system time.
This makes touch safer for file creation scripts
compared to redirection operators like > file.txt, which
instantly wipes the existing content of a file.
Preventing File
Creation with the -c Flag
If your workflow requires updating a file's timestamp only if it
already exists—without inadvertently creating a new file—you can use the
-c (or --no-create) option:
touch -c missing_file.txtWhen this flag is applied, the system suppresses file creation if the specified target does not exist, leaving the directory unchanged.