Linux touch Command

Create Files and Update Timestamps - 10 Practical Examples with Detailed Explanations

Example 1

Create a New Empty File

$ touch newfile.txt
$ ls -l newfile.txt -rw-r--r-- 1 user user 0 Oct 18 10:45 newfile.txt
Creates a new empty file if it doesn't exist. This is the most common use of touch. The file has zero bytes and current timestamp. If the file already exists, touch updates its modification time without changing the content.
Quick File Creation: Perfect for creating placeholder files or testing permissions
Example 2

Create Multiple Files at Once

$ touch file1.txt file2.txt file3.txt
$ ls -l -rw-r--r-- 1 user user 0 Oct 18 10:45 file1.txt -rw-r--r-- 1 user user 0 Oct 18 10:45 file2.txt -rw-r--r-- 1 user user 0 Oct 18 10:45 file3.txt
Creates multiple files in a single command by listing all filenames separated by spaces. Much faster than running touch multiple times. All files are created with the same timestamp (the current time).
Batch Creation: Combine with brace expansion: touch file{1..10}.txt creates 10 files
Example 3

Update Only Access Time

$ touch -a file.txt
$ stat file.txt File: file.txt Access: 2025-10-18 10:50:00.000000000 -0400 Modify: 2025-10-18 10:45:00.000000000 -0400
Updates only the access time (atime) of the file using the '-a' option, leaving the modification time unchanged. Access time tracks when the file was last read. Useful for maintaining modification timestamps while updating access records.
Note: Many systems mount with noatime to improve performance, which may affect this behavior
Example 4

Update Only Modification Time

$ touch -m file.txt
$ stat file.txt File: file.txt Access: 2025-10-18 10:45:00.000000000 -0400 Modify: 2025-10-18 10:55:00.000000000 -0400
Updates only the modification time (mtime) using the '-m' option, leaving access time unchanged. Modification time tracks when file content was last changed. This is the default behavior of touch without any options.
Use Case: Trigger make or backup systems that monitor modification times
Example 5

Set Specific Date and Time

$ touch -t 202510181230.45 file.txt
$ ls -l file.txt -rw-r--r-- 1 user user 0 Oct 18 12:30 file.txt
Sets a specific timestamp using the '-t' option with format [[CC]YY]MMDDhhmm[.ss]. This example sets October 18, 2025, 12:30:45. Useful for backdating files, testing date-based logic, or correcting timestamps after system clock issues.
Format: YYYYMMDDhhmm.ss (Year, Month, Day, Hour, Minute, Seconds)
Example 6

Set Time Using Human-Readable Date

$ touch -d "2025-10-18 14:30:00" file.txt
$ ls -l file.txt -rw-r--r-- 1 user user 0 Oct 18 14:30 file.txt $ touch -d "2 hours ago" file.txt $ touch -d "next Monday" file.txt $ touch -d "yesterday 10:30" file.txt
Sets timestamp using a human-readable date string with the '-d' option. Accepts various formats including absolute dates, relative times like "2 hours ago", "yesterday", "next week", etc. Much more flexible and easier to remember than the numeric format.
Flexibility: Accepts almost any reasonable date format or relative time expression
Example 7

Copy Timestamp from Another File

$ touch -r reference.txt target.txt
$ ls -l -rw-r--r-- 1 user user 1024 Oct 18 09:00 reference.txt -rw-r--r-- 1 user user 2048 Oct 18 09:00 target.txt # target.txt now has same timestamp as reference.txt
Copies the timestamp from a reference file to the target file using the '-r' option. Both access and modification times are copied. Extremely useful for synchronizing timestamps or maintaining consistent file dates across backups or deployments.
Use Case: Synchronize timestamps after file transfers or maintain file date consistency
Example 8

Don't Create File if It Doesn't Exist

$ touch -c nonexistent.txt
$ ls nonexistent.txt ls: cannot access 'nonexistent.txt': No such file or directory # No error, no file created
Prevents file creation if it doesn't already exist using the '-c' option. This only updates timestamps of existing files without creating new ones. Useful in scripts where you want to update existing files but not accidentally create new ones.
Script Safety: Prevents unintended file creation in automated processes
Example 9

Create Files with Specific Permissions

$ (umask 077 && touch secure.txt) $ ls -l secure.txt -rw------- 1 user user 0 Oct 18 10:45 secure.txt
$ touch normal.txt && chmod 600 normal.txt # Alternative method using chmod after creation
Creates files with specific permissions by combining touch with umask in a subshell. The umask 077 creates files with 600 permissions (owner read/write only). Alternatively, use touch followed by chmod. Essential for creating sensitive files with restricted access.
Security: Always set restrictive permissions for sensitive files like keys or credentials
Example 10

Update Timestamp for Files Matching Pattern

$ touch *.log
$ ls -l *.log -rw-r--r-- 1 user user 1024 Oct 18 11:00 access.log -rw-r--r-- 1 user user 2048 Oct 18 11:00 error.log -rw-r--r-- 1 user user 512 Oct 18 11:00 system.log # All .log files updated to current time
Updates timestamps for all files matching a pattern using wildcards. The shell expands the wildcard before touch executes, effectively running touch on each matching file. Useful for bulk timestamp updates or signaling file processing systems.
Use Case: Reset log rotation timers, trigger backup systems, or mark batch processing completion
Bonus 1

Create Directory Structure with Files

$ mkdir -p project/{src,tests,docs} && touch project/src/{main,utils,config}.py project/tests/test_{main,utils}.py project/docs/README.md
$ tree project project/ ├── docs │ └── README.md ├── src │ ├── config.py │ ├── main.py │ └── utils.py └── tests ├── test_main.py └── test_utils.py
Combines mkdir with brace expansion and touch to quickly scaffold project structures. Creates multiple directories and files in one command. Incredibly useful for quickly setting up new projects or creating test environments.
Productivity: Master brace expansion to rapidly create complex file structures
Bonus 2

Understanding File Timestamps

$ stat file.txt
File: file.txt Size: 1024 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 123456 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ user) Gid: ( 1000/ user) Access: 2025-10-18 10:45:23.123456789 -0400 (atime) Modify: 2025-10-18 10:50:45.987654321 -0400 (mtime) Change: 2025-10-18 10:50:45.987654321 -0400 (ctime)
Files have three timestamps: Access time (atime) - when content was last read; Modify time (mtime) - when content was last changed; Change time (ctime) - when metadata (permissions, ownership) was last changed. The touch command manipulates atime and mtime but ctime updates automatically.
Important: ctime (change time) cannot be manually set - it updates automatically when metadata changes
Reference

Common touch Options Quick Reference

Basic Usage: touch file.txt Create file or update timestamp touch file1 file2 file3 Create multiple files touch file{1..10}.txt Create numbered files (brace expansion) Timestamp Control: touch -a file.txt Update access time only touch -m file.txt Update modification time only touch -am file.txt Update both access and modification time Setting Specific Times: touch -t YYYYMMDDhhmm.ss file.txt Set specific timestamp touch -t 202510181230.45 file.txt Example: Oct 18, 2025 12:30:45 touch -d "date string" file.txt Human-readable date touch -d "2 hours ago" file.txt Relative time touch -d "yesterday" file.txt Named relative time touch -d "2025-10-18 14:30" file.txt ISO format Copy Timestamps: touch -r source.txt target.txt Copy timestamp from source to target Behavior Modifiers: touch -c file.txt Don't create file if it doesn't exist touch -h symlink Change symlink timestamp (not target) Common Patterns: touch *.log Update all .log files touch -- -filename Create file starting with dash touch "file with spaces" Create file with spaces in name Practical Examples: mkdir -p dir && touch dir/file Create dir and file find . -name "*.bak" -exec touch {} \; Update all .bak files touch -d "$(date -d '1 day ago')" file Set to yesterday's date touch -r old.txt new.txt Match new file to old timestamp Timestamp Formats for -d option: "2025-10-18" Date only (midnight) "2025-10-18 14:30" Date and time "14:30" Time today "now" Current time "yesterday" 24 hours ago "2 hours ago" Relative time "next Monday" Future date "last week" Past date
Comprehensive reference for touch command usage including file creation, timestamp manipulation, and practical examples for system administration and scripting.