How to Use Touch Command in Linux: Create and Update Files
The
touch command in Linux creates a new empty file if it doesn't exist or updates the access and modification timestamps of an existing file. You can use it simply by typing touch filename in the terminal.Syntax
The basic syntax of the touch command is:
touch [options] filename
Here, filename is the name of the file you want to create or update. Options can modify the behavior, such as changing timestamps or creating multiple files.
bash
touch [options] filename
Example
This example shows how to create a new file named example.txt or update its timestamp if it already exists.
bash
touch example.txt ls -l example.txt
Output
-rw-r--r-- 1 user user 0 Jun 10 12:00 example.txt
Common Pitfalls
One common mistake is expecting touch to add content to a file; it only creates or updates timestamps. Another is using touch without proper permissions, which will fail silently or show an error.
Also, if you mistype the filename, you might create unwanted empty files.
bash
touch myfile.txt # Wrong: expecting content added cat myfile.txt # Right: use an editor to add content nano myfile.txt
Quick Reference
| Option | Description |
|---|---|
| -a | Change only the access time |
| -m | Change only the modification time |
| -c | Do not create any files |
| -t [[CC]YY]MMDDhhmm[.ss] | Use specified timestamp |
| -r file | Use timestamp from another file |
Key Takeaways
Use
touch filename to create an empty file or update its timestamps.Touch does not add content to files; use an editor for that.
You can modify timestamps with options like
-a and -m.Ensure you have write permissions to create or update files with touch.
Avoid typos to prevent creating unwanted empty files.