Skip to main content
1 of 3
Edgar Magallon
  • 5.1k
  • 3
  • 15
  • 29

First you should get the modification date of the file by using stat command:

filedate=$(stat -c '%y' "/path/to/file" | cut -d' ' -f1)

Now it will be replaced the month with another one. For that, you can use awk:

newDate=$(awk -v month="Jun" -F '-' '{print $1"-"month"-"$3}' <<< $filedate )

And with touch command you can change the creation date:

touch -m -d $newDate /path/to/file
#-m argument is used to change modification time
#-d is used to specify the new date

Finally, if you want to change files recursively you can use find and the code provided before should be in a script file:

script.sh:

#! /usr/bin/env bash

filedate=$(stat -c '%y' "$1" | cut -d' ' -f1)

newMonth="Dec" #here you specify the month 
newDate=$(awk -v month=$newMonth -F '-' '{print $3"-"month"-"$1}' <<< $filedate )

touch -m -d $newDate $1

And with find you can use:

find /path/to/your_directory -type f -exec ./script.sh {} \;

If you want to specify the month in the find command, you can pass it to the script.sh as parameter:

Therefore, the code would now became:

script.sh

#! /usr/bin/env bash

filedate=$(stat -c '%y' "$1" | cut -d' ' -f1)

newMonth="$2"
newDate=$(awk -v month=$newMonth -F '-' '{print $3"-"month"-"$1}' <<< $filedate )

touch -m -d $newDate $1

find command

find . -type f -exec ./script {} "Nov" \;
Edgar Magallon
  • 5.1k
  • 3
  • 15
  • 29