How to Use the ‘touch’ Command in Linux
Get hands-on with the Linux touch command for smarter file management and automation.
The touch
Linux command is useful for users who need to create, modify, or update file timestamps. Essentially, the command allows you to “touch” a file, creating it if it doesn’t exist, or updating the access and modification times if it does. It’s a simple yet powerful command that plays a vital role in file management.
While the touch
command is unique in its functionality, it often works in conjunction with commands like ls
for listing files and chmod
for changing permissions. Professionals such as system administrators, developers, and data analysts find this command handy for various tasks like scripting, file organization, and automation.
How to Use touch
1. Creating a New Empty File
Syntax: touch filename
Explanation: Creates a new empty file named “filename.”
Example: touch newfile.txt
The command creates a new empty file named newfile.txt
.
2. Updating the Timestamp of an Existing File
Syntax: touch existingfile
Explanation: Updates the access and modification timestamps of existingfile
to the current time.
Example: touch myfile.txt
The command updates the timestamps of myfile.txt
.
3. Creating Multiple Empty Files
Syntax: touch file1 file2 file3
Explanation: Creates multiple new empty files.
Example: touch file1.txt file2.txt file3.txt
The command creates three new empty files named file1.txt
, file2.txt
, and file3.txt
.
4. Using a Specific Timestamp
Syntax: touch -t TIMESTAMP filename
Explanation: Sets the access and modification timestamps of filename
to a specific time.
Example: touch -t 202201011200 myfile.txt
The command sets the timestamps of myfile.txt
to 12:00 on January 1, 2022
5. Creating a File with a Specific Date
Syntax: touch -d DATE_STRING filename
Explanation: Sets the access and modification timestamps of filename
to a specific date and time.
Example: touch -d '1 Jan 2022 12:00' myfile.txt
The command sets the timestamps of myfile.txt
to 12:00 on January 1, 2022.
6. Creating a File without Changing Timestamps if It Exists
Syntax: touch -c filename
Explanation: Creates filename
if it does not exist, but does not change timestamps if it does.
Example: touch -c existingfile.txt
If existingfile.txt
does not exist, the command creates it. If it does exist, the command does nothing.
7. Creating a File with a Reference File’s Timestamps
Syntax: touch -r REF_FILE filename
Explanation: Sets the access and modification timestamps of “filename” to match those of a reference file “REF_FILE.”
Example: touch -r reference.txt myfile.txt
The command sets the timestamps of myfile.txt
” to match those of reference.txt
.
8. Creating a File and Suppressing Error Messages
Syntax: touch -f filename
Explanation: Creates filename
and suppresses most error messages.
Example: touch -f myfile.txt
The command creates myfile.txt
and suppresses most error messages.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |