Skip to main content

Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.

touch - Create Empty Files or Update Timestamps

The touch command is a versatile utility that creates empty files or updates file timestamps (access and modification times). It's commonly used for creating placeholder files, updating file timestamps for various purposes, and triggering build processes. The command operates on multiple files simultaneously and supports both absolute and relative timestamp specifications, making it an essential tool in file management workflows, build systems, and automation scripts.

Basic Syntax

touch [OPTIONS] FILE...

Common Options

Timestamp Options

  • -a - Change only the access time (atime)
  • -m - Change only the modification time (mtime)
  • -t [[CC]YY]MMDDhhmm[.ss] - Use specified timestamp format
  • -d, --date=STRING - Parse STRING and use it as a timestamp
  • -r, --reference=FILE - Use FILE's times instead of current time
  • --time=WORD - Change specified time (access, atime, use, modify, mtime)

Creation Options

  • -c, --no-create - Don't create files if they don't exist

Output Options

  • -v, --verbose - Explain what is being done

Help and Version

  • --help - Display help message and exit
  • --version - Output version information and exit

Timestamp Format Specification

-t Option Format

[[CC]YY]MMDDhhmm[.ss]
  • [CC]YY - Optional century and year (2 or 4 digits)
  • MM - Month (01-12)
  • DD - Day (01-31)
  • hh - Hour (00-23)
  • mm - Minute (00-59)
  • [.ss] - Optional seconds (00-60)

Date String Formats (-d option)

# ISO formats
"2024-01-15"
"2024-01-15 12:30:45"
"2024-01-15T12:30:45"

# Traditional formats
"15 Jan 2024"
"01/15/2024"
"Jan 15 2024"

# Time formats
"12:30"
"12:30:45"
"12:30:45.123"

# Relative formats
"now"
"yesterday"
"tomorrow"
"next week"
"2 days ago"
"1 hour ago"
"last Monday"
"next Friday"

Usage Examples

Basic File Operations

Creating Empty Files

# Create single empty file
touch newfile.txt

# Create multiple empty files
touch file1.txt file2.txt file3.txt

# Create files with different extensions
touch config.json database.sql README.md .env

# Create numbered files using brace expansion
touch report_{1,2,3}.txt
touch data_{01..10}.csv

# Create files in subdirectories
touch logs/app.log logs/error.log logs/debug.log

# Create nested directory structure with files
mkdir -p src/{utils,models,controllers}
touch src/main.py src/utils/helpers.py src/models/user.py src/controllers/app.py

Conditional File Creation

# Don't create file if it doesn't exist
touch -c optional_file.txt

# Create file only if it doesn't already exist
[ ! -f "unique.txt" ] && touch unique.txt

# Create multiple files with no-create option
touch -c file1.txt file2.txt file3.txt

# Verbose mode shows what files were created
touch -v verbose_file.txt

Timestamp Management

Updating File Timestamps

# Update both access and modification times to current
touch existing_file.txt

# Update only access time
touch -a file.txt

# Update only modification time
touch -m file.txt

# Update timestamps with verbose output
touch -v file.txt

# Update timestamps for multiple files
touch *.txt

# Update timestamps recursively
find . -name "*.py" -exec touch {} \;

Setting Specific Timestamps

# Set specific timestamp using -t format
touch -t 202401151200.00 file.txt
touch -t 202401151230.45 report.pdf

# Set date using string format
touch -d "2024-01-15 12:00:00" file.txt
touch -d "Jan 15 2024" file.txt

# Set to specific time today
touch -d "12:30" file.txt

# Set to midnight of specific date
touch -d "2024-01-01 00:00:00" newyear.txt

# Set timestamps in the past
touch -d "2 days ago" file.txt
touch -d "last Monday" file.txt
touch -d "1 month ago" file.txt

# Set timestamps in the future
touch -d "tomorrow" file.txt
touch -d "next week" file.txt
touch -d "1 hour from now" file.txt

# Set complex relative times
touch -d "last Friday 14:30" file.txt
touch -d "3 days ago 09:00:00" file.txt

Copying Timestamps

# Copy timestamps from reference file
touch -r reference_file.txt target_file.txt

# Copy timestamps to multiple files
touch -r source_timestamp.txt *.txt

# Use system file as timestamp reference
touch -r /etc/passwd backup_passwd

# Copy timestamps between files in different directories
touch -r /var/log/syslog /backup/copy_syslog

# Apply directory timestamp to files
touch -r /data/source/ /data/target/*

Advanced File Operations

Bulk File Creation Patterns

# Create files with sequential numbering
touch report_{001..100}.txt
touch log_{20240101..20240131}.log

# Create files with alphabetic sequences
touch data_{A..Z}.csv
touch file_{a..z}.txt

# Create files with date patterns
touch log_$(date +%Y%m%d)_{01..24}.log
touch backup_$(date +%Y%m%d_%H%M%S).tar.gz

# Create test files with different sizes
for size in 1K 1M 100M; do
fallocate -l $size test_${size}.file
touch test_${size}.file
done

Project Structure Creation

# Complete web project structure
mkdir -p {src,tests,docs,config,static/{css,js,images}}
touch src/{main.py,app.py,models.py,views.py}
touch tests/{test_main.py,test_models.py,test_views.py}
touch docs/{README.md,API.md,INSTALL.md}
touch config/{settings.py,database.py,logging.conf}
touch static/{css/style.css,js/app.js,images/logo.png}

# Create .gitkeep for empty directories in version control
find . -type d -empty -exec touch {}/.gitkeep \;

# Java project structure
mkdir -p src/{main,test}/{java/com/example/myapp,resources}
touch src/main/java/com/example/myapp/{Application.java,Model.java,Service.java}
touch src/test/java/com/example/myapp/{ApplicationTest.java,ModelTest.java}
touch src/main/resources/{application.properties,logback.xml}
touch pom.xml README.md .gitignore

Build System Integration

# Force make to rebuild specific files
touch source.c
touch header.h

# Mark build completion
touch build_successful_$(date +%s)

# Create dependency files
touch .deps/main.d .deps/utils.d .deps/models.d

# Update configuration to trigger reload
touch /etc/app/config
touch /var/www/html/reload_trigger

# Create timestamp markers for incremental builds
touch .build_complete
touch .last_build_$(date +%s)

Practical Examples

Development Workflows

Version Control Integration

# Create .gitignore and other git files
touch .gitignore .gitkeep .gitattributes

# Mark directories for git tracking
find . -type d -exec touch {}/.gitkeep \;

# Create pre-commit hooks
touch .git/hooks/pre-commit .git/hooks/pre-push

# Create README and documentation structure
touch README.md CHANGELOG.md LICENSE
touch docs/{installation.md,usage.md,api.md,contributing.md}

Testing Infrastructure

# Create test file structure
mkdir -p tests/{unit,integration,e2e}
touch tests/unit/{test_models.py,test_utils.py,test_services.py}
touch tests/integration/{test_api.py,test_database.py}
touch tests/e2e/{test_user_flow.py,test_admin_flow.py}

# Create test configuration
touch tests/{conftest.py,test_config.ini,pytest.ini}

# Create test data files
touch tests/data/{users.json,products.json,orders.json}

Application Deployment

# Create deployment markers
touch deployment_$(date +%Y%m%d_%H%M%S).log
touch version_$(git rev-parse --short HEAD).txt

# Create runtime directories
mkdir -p {logs,tmp,cache,uploads}
touch logs/{app.log,error.log,access.log,debug.log}
touch tmp/.keep cache/.keep uploads/.keep

# Create environment files
touch .env.production .env.staging .env.development

System Administration

Log Management

# Create daily log files
for month in {01..12}; do
for day in {01..31}; do
touch "log_2024${month}${day}.txt"
done
done

# Create log rotation markers
touch rotate_logs_$(date +%s)
touch last_rotation_$(date +%Y%m%d)

# Set up log file structure
mkdir -p /var/log/app/{archive,current}
touch /var/log/app/app.log
touch /var/log/app/error.log
touch /var/log/app/access.log

# Create empty log files for new services
touch /var/log/newservice.log
touch /var/log/newservice_error.log

Backup and Maintenance

# Create backup timestamp markers
touch backup_started_$(date +%s)
touch backup_complete_$(date +%s)

# Create maintenance mode files
touch /tmp/maintenance_in_progress
touch /var/www/html/maintenance.mode

# Create cleanup scripts triggers
touch cleanup_needed_$(date +%Y%m%d)
touch temp_cleanup_$(date +%s)

# Mark backup verification
touch backup_verified_$(date +%s)

System Monitoring

# Create monitoring log files
touch monitoring_{cpu,memory,disk,network}.log

# Create health check files
touch health_check_$(date +%s)
touch service_status_$(date +%Y%m%d_%H%M%S)

# Create alert markers
touch alert_triggered_$(date +%s)
touch alert_resolved_$(date +%s)

# Performance monitoring files
touch perf_test_$(date +%Y%m%d).results

File Organization

Media File Management

# Create photo organization structure
mkdir -p photos/{2023,2024}/{01..12}
find photos/ -type d -exec touch {}/.gitkeep \;

# Create video catalog files
touch video_catalog_$(date +%Y).md
touch video_tags_$(date +%Y%m%d).txt

# Create music library structure
mkdir -p music/{classical,jazz,rock,electronic}
touch music/playlist_{workout,relax,focus}.m3u

Document Management

# Create document templates
touch template_{letter,report,invoice,contract}.doc
touch meeting_notes_$(date +%Y).md
touch project_status_$(date +%Y%m%d).md

# Create backup markers
touch docs_backup_$(date +%Y%m%d_%H%M%S)
touch version_$(git describe --tags).docs

# Create index files
touch document_index.md
touch file_manifest_$(date +%Y).txt

Advanced Usage

Automation and Scripting

Automated File Creation

#!/bin/bash
# Automated daily log file creation

LOG_DIR="/var/log/app"
DATE=$(date +%Y%m%d)

# Create daily log files
touch "${LOG_DIR}/app_${DATE}.log"
touch "${LOG_DIR}/error_${DATE}.log"
touch "${LOG_DIR}/access_${DATE}.log"

# Create weekly summary files
if [ $(date +%u) -eq 1 ]; then
touch "${LOG_DIR}/weekly_summary_$(date +%Y%U).log"
fi

# Create monthly archive marker
if [ $(date +%d) -eq 01 ]; then
touch "${LOG_DIR}/monthly_archive_$(date +%Y%m).marker"
fi

Build Automation

#!/bin/bash
# Pre-build setup automation

PROJECT_DIR="/home/user/project"
BUILD_TIME=$(date +%s)

# Create build markers
touch "${PROJECT_DIR}/build_started_${BUILD_TIME}"

# Update source timestamps to force rebuild
find "${PROJECT_DIR}/src" -name "*.c" -exec touch {} \;

# Create dependency directories
mkdir -p "${PROJECT_DIR}/build/deps"
touch "${PROJECT_DIR}/build/deps/.keep"

# Mark build completion
touch "${PROJECT_DIR}/build_complete_${BUILD_TIME}"

Cleanup Automation

#!/bin/bash
# Automated cleanup with timestamp tracking

TEMP_DIR="/tmp"
MAX_AGE_DAYS=30

# Find and update timestamps of files to keep
find "$TEMP_DIR" -name "*.keep" -exec touch {} \;

# Create cleanup log
touch "${TEMP_DIR}/cleanup_$(date +%Y%m%d).log"

# Mark cleanup process
touch "${TEMP_DIR}/cleanup_in_progress_$(date +%s)"

# Remove old temporary files
find "$TEMP_DIR" -type f -mtime +$MAX_AGE_DAYS -delete

# Mark cleanup completion
touch "${TEMP_DIR}/cleanup_complete_$(date +%s)"

File System Operations

Timestamp Synchronization

# Synchronize file timestamps across directories
find /source -type f -exec touch -r {} /target/{} \;

# Copy timestamps from source to backup
for file in $(find /data -name "*.txt"); do
backup_file="/backup${file#/data}"
if [ -f "$backup_file" ]; then
touch -r "$file" "$backup_file"
fi
done

# Set consistent timestamps for photo collection
reference_date="2020-01-01 12:00:00"
find /photos -name "*.jpg" -exec touch -d "$reference_date" {} \;

Directory Timestamp Management

# Update directory timestamps recursively
find . -type d -exec touch {} \;

# Set directory timestamps to match latest file
for dir in */; do
latest_file=$(find "$dir" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -d' ' -f2-)
if [ -n "$latest_file" ]; then
touch -r "$latest_file" "$dir"
fi
done

# Create timestamp-sorted directory structure
for year in {2020..2024}; do
for month in {01..12}; do
touch -d "${year}-${month}-01 00:00:00" "archive/${year}/${month}/"
done
done

Integration with Other Commands

Pipeline Integration

# Create files from find results
find . -name "*.tmp" | xargs touch

# Update files based on grep results
grep -l "TODO" *.py | xargs touch

# Create files from ls output
ls *.jpg | sed 's/$/.meta/' | xargs touch

# Process and touch files in pipeline
git status --porcelain | grep "^ M" | cut -c4- | xargs touch

Conditional Operations

# Touch files only if they exist
for file in *.txt; do
[ -f "$file" ] && touch "$file"
done

# Update timestamps based on file content
if grep -q "error" logfile; then
touch -d "1 hour ago" error_flag
fi

# Create timestamp based on file size
if [ $(stat -f%z large_file) -gt 1000000 ]; then
touch large_file_processed
fi

Timestamp Formats and Examples

Comprehensive Date Formats

# ISO 8601 formats
touch -d "2024-01-15" file.txt
touch -d "2024-01-15T12:30:45" file.txt
touch -d "2024-01-15 12:30:45+08:00" file.txt

# RFC 2822 format
touch -d "Mon, 15 Jan 2024 12:30:45 GMT" file.txt

# US date formats
touch -d "01/15/2024" file.txt
touch -d "1/15/2024 12:30 PM" file.txt

# European date formats
touch -d "15.01.2024" file.txt
touch -d "15-Jan-2024" file.txt

# Natural language formats
touch -d "January 15, 2024" file.txt
touch -d "15th January 2024" file.txt
touch -d "3rd Monday of January 2024" file.txt

Time Specifications

# Time only (uses current date)
touch -d "14:30" file.txt
touch -d "2:30 PM" file.txt
touch -d "14:30:45" file.txt

# Time with timezone
touch -d "14:30 UTC" file.txt
touch -d "14:30 EST" file.txt

# Complex time expressions
touch -d "2024-01-15 14:30:45.123456789" file.txt
touch -d "tomorrow at 09:00" file.txt
touch -d "next Monday 14:30" file.txt

Relative Time Expressions

# Relative to current time
touch -d "now" file.txt
touch -d "today" file.txt
touch -d "yesterday" file.txt
touch -d "tomorrow" file.txt

# Specific time units ago
touch -d "2 hours ago" file.txt
touch -d "30 minutes ago" file.txt
touch -d "5 days ago" file.txt
touch -d "3 weeks ago" file.txt
touch -d "6 months ago" file.txt
touch -d "2 years ago" file.txt

# Future time
touch -d "in 2 hours" file.txt
touch -d "next week" file.txt
touch -d "next month" file.txt
touch -d "next year" file.txt

# Day of week references
touch -d "last Monday" file.txt
touch -d "next Friday" file.txt
touch -d "this Wednesday" file.txt

# Complex expressions
touch -d "last Friday 17:00" file.txt
touch -d "3 days ago 09:00" file.txt
touch -d "2 weeks from now" file.txt

File Properties and Metadata

Understanding Timestamps

# View all timestamps
stat file.txt

# Show detailed file information
ls -l --time-style=full-iso file.txt
ls -lu --time-style=full-iso file.txt # access time
ls -lc --time-style=full-iso file.txt # change time

# Check timestamp updates
touch file.txt && stat file.txt
touch -a file.txt && stat file.txt
touch -m file.txt && stat file.txt

Timestamp Types Explained

# Access time (atime) - when file was last read
cat file.txt
ls -lu file.txt # Shows updated access time
touch -a file.txt # Updates only access time

# Modification time (mtime) - when file content was last changed
echo "new content" > file.txt
ls -l file.txt # Shows updated modification time
touch -m file.txt # Updates only modification time

# Change time (ctime) - when file metadata was last changed
chmod 644 file.txt
ls -lc file.txt # Shows updated change time
# Note: ctime cannot be directly set with touch

Special Use Cases

Version Control Integration

# Touch files to trigger rebuild after merge
find . -name "*.c" -exec touch {} \;

# Create timestamp for deployment
touch deployed_$(git rev-parse HEAD)

# Mark code review completion
touch reviewed_$(git log -1 --format=%h) src/main.py

# Force Git to detect changes
touch .gitignore
touch README.md

Build System Triggers

# Make build dependencies
touch dependencies.mk
touch config.h

# Mark incremental build points
touch .last_compile_$(date +%s)
touch .last_test_$(date +%s)

# Create build markers for different stages
touch compile_complete
touch link_complete
touch package_complete

Data Processing Workflows

# Mark data processing stages
touch data_imported_$(date +%Y%m%d)
touch data_processed_$(date +%s)
touch data_validated

# Create pipeline markers
touch stage1_complete
touch stage2_complete
touch stage3_complete

# Mark cleanup operations
touch cleanup_temp_files_$(date +%s)
touch archive_old_data_$(date +%Y%m)

Performance Considerations

Bulk Operations Optimization

# Efficient bulk file creation
printf 'file_%03d.txt\n' {1..1000} | xargs touch

# Parallel timestamp updates
find . -name "*.log" | xargs -P 4 -I {} touch {}

# Batch operations with find
find . -type f -name "*.tmp" -exec touch -t 202401010000 {} +

# Memory-efficient large file operations
for i in {1..10000}; do
touch "file_$i.txt" &
if (( i % 100 == 0 )); then
wait
fi
done

File System Considerations

# Avoid excessive metadata updates
touch -c newfile*.txt # Only if they exist

# Use reference file for consistency
touch -r timestamp_reference file1 file2 file3

# Minimize directory updates
touch -r ./* # Batch operation

# Handle large directory structures efficiently
find /large/directory -maxdepth 1 -type f -exec touch {} +

Troubleshooting

Common Issues and Solutions

Permission Problems

# Check write permissions
[ -w "$(pwd)" ] || echo "No write permission in current directory"

# Create file with specific permissions
umask 022 && touch newfile.txt

# Handle permission denied errors
if ! touch /root/protected_file; then
echo "Permission denied. Use sudo or check file ownership."
fi

# Create files with ownership change
touch newfile.txt && sudo chown www-data:www-data newfile.txt

File System Limitations

# Check available space
df -h . | tail -1

# Handle filename length limits
touch "very_long_filename_that_exceeds_file_system_limit" 2>&1

# Handle special characters in filenames
touch "file with spaces.txt"
touch "file'with'quotes.txt"
touch 'file"with"double"quotes.txt'
touch "file-with-@special#chars$.txt"

Timestamp Issues

# Verify timestamp format
date -d "2024-02-30" 2>&1 # Invalid date

# Check system timezone
date
timedatectl status

# Handle timezone differences
export TZ=UTC && touch -d "2024-01-15 12:00:00" file.txt

# Verify timestamp changes
touch -v file.txt
stat file.txt

Cross-Platform Compatibility

# Handle different touch implementations
if ! touch --version >/dev/null 2>&1; then
echo "Using basic touch implementation"
touch file.txt
fi

# Check available options
touch --help 2>&1 | head -20

# Portable timestamp setting
touch -t 202401151200 file.txt # Most portable format

Integration with Shell Scripts

Error Handling

#!/bin/bash
# Safe file creation with error handling

create_file_safely() {
local filename="$1"

if [ -z "$filename" ]; then
echo "Error: No filename provided" >&2
return 1
fi

if [ -e "$filename" ]; then
echo "File '$filename' already exists"
return 0
fi

if ! touch "$filename" 2>/dev/null; then
echo "Error: Cannot create file '$filename'" >&2
return 1
fi

echo "Successfully created '$filename'"
return 0
}

# Usage examples
create_file_safely "new_file.txt"
create_file_safely "" # Error case
create_file_safely "existing_file.txt"

Logging and Monitoring

#!/bin/bash
# File creation with logging

LOG_FILE="file_creation.log"

log_operation() {
local operation="$1"
local file="$2"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $operation: $file" >> "$LOG_FILE"
}

create_with_logging() {
local file="$1"

if touch "$file"; then
log_operation "CREATED" "$file"
echo "File '$file' created successfully"
else
log_operation "FAILED" "$file"
echo "Failed to create file '$file'" >&2
return 1
fi
}

# Usage
create_with_logging "report_$(date +%Y%m%d).txt"
create_with_logging "/root/protected.txt" # Will fail

Configuration Management

#!/bin/bash
# Touch-based configuration reloading

CONFIG_DIR="/etc/myapp"
RELOAD_FLAG="$CONFIG_DIR/reload_needed"

# Trigger configuration reload
trigger_reload() {
touch "$RELOAD_FLAG"
echo "Configuration reload triggered"
}

# Check if reload is needed
check_reload() {
if [ -f "$RELOAD_FLAG" ] && [ "$RELOAD_FLAG" -nt "$CONFIG_DIR/config.json" ]; then
echo "Reloading configuration..."
# Reload logic here
rm "$RELOAD_FLAG" # Remove flag after reload
echo "Configuration reloaded"
fi
}

# Usage in daemon
while true; do
check_reload
sleep 5
done

Best Practices

  1. Use descriptive filenames when creating placeholder files to ensure clarity
  2. Employ the -c option to avoid unintentionally creating unwanted files
  3. Leverage verbose mode for important operations to track changes
  4. Use relative timestamps in automation scripts for maintainability
  5. Create .gitkeep files to preserve empty directories in version control
  6. Set appropriate permissions immediately after file creation when needed
  7. Use reference files for consistent timestamp management across multiple files
  8. Document timestamp conventions when working with team projects
  9. Test timestamp formats before using them in production scripts
  10. Handle errors gracefully when creating files in uncertain conditions

Performance Tips

  1. Batch operations using xargs or find with -exec for better performance
  2. Minimize filesystem calls by using reference files (-r option)
  3. Avoid unnecessary timestamp updates by checking file existence first
  4. Use absolute paths when working with files in different directories
  5. Consider parallel processing for large-scale file operations
  6. Monitor disk space when creating large numbers of files
  7. Use appropriate timestamp precision based on your filesystem capabilities
  8. Limit scope with specific file patterns instead of touching entire directories
  9. Cache reference timestamps when applying the same timestamp repeatedly
  10. Use file system-friendly naming to avoid performance penalties
  • stat - Display detailed file or filesystem status
  • ls - List directory contents with timestamp options
  • find - Search for files by timestamp criteria
  • mkdir - Create directories
  • date - Display or set system date and time
  • chmod - Change file permissions
  • chown - Change file ownership
  • fallocate - Allocate file space
  • truncate - Shrink or extend file size
  • echo - Display text with timestamp generation

The touch command is a fundamental utility in the Linux ecosystem, serving both simple file creation needs and complex timestamp management requirements. Its versatility makes it indispensable in build systems, automation workflows, version control integration, and system administration tasks. Mastering touch's various options and use cases significantly enhances file management efficiency and workflow automation capabilities.