Shell – Script for changing modification time of files and directories recursively

filesfindshell-scripttimestampstouch

I have many big folders with thousands of files in them and I want to use touchto set their modification times to be "original time"+3 hours.

I got this script from a similar thread in superuser:

#!/bin/sh
for i in all/*; do
  touch -r "$i" -d '+3 hour' "$i"
done

so I'm guessing what I need is to make it work in any directory instead of a fixed one (so I won't need to edit the script everytime I want to run it somewhere different) and for it to able to find and edit files recursively.

I have little experience using Linux and this is my first time setting up a bash script, though I do know a thing or two about programming (mainly in C).

thanks you all very much for the help 🙂

Best Answer

Use find -exec for recursive touch, with command line args for dirs to process.

#!/bin/sh
for i in "$@"; do
    find "$i" -type f -exec touch -r {} -d '+3 hour' {} \;
done

You can run it like this:

./script.sh /path/to/dir1 /path/to/dir2
Related Question