📄 Command: touch (Create Empty Files / Update Timestamps)
The touch command is the quickest way to create a new, empty file or update the "last modified" timestamp of an existing file.
1. The "Why"
touch is also used by system administrators to "trigger" actions. Some programs look at the date a file was modified to decide if they need to run a backup or a re-compile. touch lets you fake a modification without changing any data inside.
2. Basic Syntax
touch [OPTIONS] FILENAME
- Example:
touch index.htmlcreates a file named "index.html" if it doesn't already exist.
3. Most Useful Flags
| Flag | Name | What it does |
|---|---|---|
-a |
Access Time | Changes only the access time (atime). |
-m |
Modification Time | Changes only the modification time (mtime). |
-c |
No Create | Does not create a new file if it doesn't exist; it only updates the time on existing files. |
-r |
Reference | Uses the timestamp of another file instead of the current time. |
-t |
Timestamp | Allows you to set a specific date and time (format: [[CC]YY]MMDDhhmm[.ss]). |
4. Practical Examples
A. Creating a New File Instantly
The most common use case. It’s faster than opening an editor and clicking "Save As":
touch main.java
B. Creating Multiple Files at Once
You can set up a whole project structure in seconds:
touch script.js styles.css index.html README.md
C. Using Brace Expansion for Sequences
If you need to create 10 numbered files for your e-book chapters:
touch chapter_{1..10}.tex
This creates chapter_1.tex, chapter_2.tex, etc., all the way to 10.
D. Updating a File's "Last Modified" Date
If you have a file from last year and you want the system to think it was edited today:
touch existing_file.txt
5. Pro-Tips
- Check Existence: If you aren't sure if a file exists and you don't want to accidentally create one, use
touch -c. If the file isn't there, nothing happens. - Pairing with Editors: A common workflow is
touch file.txt && code file.txt. This creates the file and then immediately opens it in VS Code. - Scripting:
touchis often used in scripts to create "lock files." If a script sees thatscript.lockexists, it knows not to run again until that file is deleted. - Arch Linux Context: You might use
touchto create a.gitkeepfile in an empty folder so that Git tracks the directory, or to create a.nomediafile to hide folders from your phone's gallery.
6. Summary Reference
| Goal | Command |
|---|---|
| Create a new empty file | touch filename |
| Create multiple files | touch file1 file2 file3 |
| Create a range of files | touch file{1..5}.txt |
| Update time to match another file | touch -r reference_file target_file |
| Don't create if file is missing | touch -c filename |