touch – change file timestamps or create files

touch command
touch command

touch updates file timestamps or creates empty files if they don’t exist. It’s commonly used to quickly create new files.

Synopsis

touch [OPTIONS] FILE...

Common Options

OptionDescription
-aChange access time only
-mChange modification time only
-cDon’t create file if it doesn’t exist
-d DATEUse specified date instead of now
-t STAMPUse timestamp [[CC]YY]MMDDhhmm[.ss]
-r FILEUse timestamp from reference file

Examples

Create an empty file

$ touch newfile.txt
$ ls -l newfile.txt
-rw-r--r-- 1 greys staff 0 Jan 29 18:00 newfile.txt

Create multiple files

$ touch file1.txt file2.txt file3.txt
$ touch test{1..5}.log    # Creates test1.log through test5.log

Update timestamp to now

$ touch existing_file.txt    # Updates mtime and atime

Set specific date

$ touch -d "2024-01-15 14:30:00" file.txt
$ touch -d "yesterday" file.txt
$ touch -d "2 days ago" file.txt

Set timestamp using format

$ touch -t 202401151430.00 file.txt    # Jan 15, 2024, 14:30:00

Copy timestamp from another file

$ touch -r reference.txt target.txt

Change only access time

$ touch -a file.txt

Change only modification time

$ touch -m file.txt

Don’t create if missing

$ touch -c nonexistent.txt    # No file created, no error

Common Patterns

Create file with content

$ echo "initial content" > newfile.txt    # Better than touch for this

Create directory structure with files

$ mkdir -p project/src && touch project/src/main.py

Trigger file-based builds

$ touch src/main.c    # Make will see file as modified

Create placeholder files

$ touch .gitkeep    # Keep empty directory in git

Reset timestamps for testing

$ touch -d "2020-01-01" old_file.txt
$ find . -newer old_file.txt    # Find files modified after

Tips

  • Creating files: touch is the simplest way to create empty files
  • Build systems: Touch source files to force recompilation
  • Testing: Set old timestamps to test backup/cleanup scripts
  • Permissions: touch preserves existing permissions
  • Existence check: Use touch -c if you only want to update existing files

See Also

  • stat — Display file timestamps
  • ls — List files with timestamps (ls -l)
  • date — Display or set system date
  • mkdir — Create directories

Tutorials