103

I need to change the timestamp of about 5000 files.

Typing touch file1.txt, touch file2.txt will take me forever.

Is there a way to do something in the lines of touch -R *?

muru
  • 207,228

3 Answers3

154

You can use find command to find all your files and execute touch on every found file using -exec

find . -type f -exec touch {} +

If you want to filter your result only for text files, you can use

find . -type f -name "*.txt" -exec touch {} +
Melebius
  • 11,750
g_p
  • 19,034
  • 6
  • 59
  • 69
1

touch **/**

This updates the timestamps of all files and directories recursively within the current directory when using Zsh or bash -O globstar.

Delta
  • 11
0

g_p's answer makes the timestamp "now" but if for instance you forgot the cp -p parameter and need to replace timestamps with their originals recursively without recopying all the files. Here is what worked for me:

find /Destination -exec bash -c 'touch -r "${0/Destination/Source}" "$0"' {} \;

This assumes a duplicate file/ folder tree of Source: /Source and Destination: /Destination

  1. find searches the Destination for all files & dirs (which need timestamps) and -exec ... {} runs a command for each result.
  2. bash -c ' ... ' executes a shell command using bash.
  3. $0 holds the find result.
  4. touch -r {timestamped_file} {file_to_stamp} uses a bash replace command ${string/search/replace} to set timestamp source appropriately.
  5. the Source and Destination directories are quoted to handle dirs with spaces.