touch

Overview

The touch command changes file timestamps. It’s commonly used to create empty files or update access and modification times of existing files.

Syntax

touch [options] file...

Common Options

Option Description
-a Change access time only
-m Change modification time only
-c Don’t create new files
-d time Use specified time
-r ref_file Use ref_file’s times
-t time Use specified time [[CC]YY]MMDDhhmm[.ss]
--time=WORD Change specified time: access, modify, change
--date=STRING Parse STRING and use it for time
--no-create Don’t create new files

Key Use Cases

  1. Create empty files
  2. Update timestamps
  3. Batch file creation
  4. File time synchronization
  5. File existence checking

Examples with Explanations

Example 1: Create File

touch newfile

Create empty file or update timestamp

Example 2: Specific Time

touch -t 202312201200 file

Set timestamp to specified date/time

Example 3: Reference File

touch -r ref_file target_file

Copy timestamps from ref_file

Understanding Output

  • No output by default
  • Error messages for:
    • Permission denied
    • Invalid date format
    • Directory not writable
    • Invalid option

Common Usage Patterns

  1. Create multiple files:

    touch file1 file2 file3
  2. Update access time:

    touch -a file
  3. Set specific date:

    touch -d "2 days ago" file

Performance Analysis

  • Fast operation
  • Minimal system impact
  • Inode updates only
  • No data modification
  • Multiple file efficiency

Additional Resources

Best Practices

  1. Use -c to prevent accidental creation
  2. Verify timestamp format
  3. Check file permissions
  4. Consider timezone impact
  5. Use with find for batch operations