linux/touch

Create File or Update Timestamps

Create new empty files or update existing file timestamps.

filesystem
basic
file
timestamp
linux

Command

touch

Explanation

The `touch` command is often used to quickly create files. If the file exists, `touch` updates its modification time. The `-t` flag allows setting a specific timestamp in the format `[[CC]YY]MMDDhhmm[.ss]`. It’s commonly used in build scripts to ensure file timestamps are current.

Common Use Cases

  • Create empty placeholder files
  • Update timestamps for build tools
  • Modify file metadata without editing contents

Best Practices

  • Use `touch` in scripts to ensure files exist before writing
  • Combine with `mkdir -p` to prepare full paths

Common Mistakes to Avoid

  • Using invalid timestamp formats with `-t`
  • Expecting `touch` to modify file contents

Troubleshooting

Problem: Permission denied creating file

Solution: Ensure you have write access to the directory or use `sudo` if appropriate.

Problem: Timestamp not set correctly

Solution: Double-check timestamp format when using the `-t` flag.

Examples

Create an empty file

touch newfile.txt

Set a specific timestamp

touch -t 202501010000 file.txt