30

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 *?

  • 1
    did you mean this touch file{1..3}.txt ? – Avinash Raj Feb 1 '15 at 9:57
  • 2
    You didn't specify which shell you are using, but with zsh, touch **/* is convenient. – Marc Glisse Feb 1 '15 at 22:36
58

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 file, you can use

find  -type f  -name "*.txt" -exec touch {} +
  • 14
    I'd suggest using -exec's + terminator (instead of \;). This will chain multiple arguments onto each touch instance (up to the system argument limit) and will therefore fork out much less (and probably be faster). – Oli Feb 1 '15 at 9:53
  • @g_p ... and for a source see What is the difference between using '+' and '\;' in -exec command? – αғsнιη Feb 1 '15 at 16:21
  • 2
    And for a dry run, just leave out the -exec touch {} + part, and it'll print to your terminal what it would have affected. – Alex Feb 1 '15 at 20:11
  • 3
    Thanks, Although I think 'find' is missing his first parameter. find . -type f -exec touch {} + – giltsl Mar 22 '18 at 9:17
  • What if I only want to change the access time, not the modification time? Should it be "find -type f -exec touch -a {} +"? – weeo Apr 18 '18 at 9:05

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.