0

I have a gallery folder with multiple images and videos, some of them has their modification date correct, and some of them has the month wrong. I want to make a script that will change all the files in a certain folder with a modification month from January to June (or any other similar combination) Example:

I have images dated (The EXIF metadata not the name) as follows:

05-Jan-2011
06-Jan-2011
07-Jan-2011

etc...

I want to keep the year and the day the same, but change only the month of all of them to June instead of Jan.

So they become (The EXIF metadata not the name) :

05-Jun-2011
06-Jun-2011
07-Jun-2011

etc....

How can I do that ?

Thanks in advance.

7
  • What do you mean by modifcation date? Is it the filename or the file metadata? Sep 26, 2022 at 4:18
  • I mean the Metadata Sep 26, 2022 at 4:30
  • 1
    Do you mean the file system's mtime metadata, or the image format's exif metadata or similar?
    – frabjous
    Sep 26, 2022 at 4:36
  • The exif metadata Sep 26, 2022 at 5:41
  • I didn't realize you were talking about exif metadata therefore my answer is not useful here. Sep 26, 2022 at 5:46

1 Answer 1

2

Update: If I am wrong with this answer please let me now.

I'm not sure if file system's modification date metadata is equivalent to exitf modification date metadata. I tested with exiftool and the dates are equivalent, so it makes me think is I can use touch for manipulating that metadata.

Solution:

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 modification 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" \;
8
  • 1
    Actually, it'd fail with a number in that format, it'd have to be YYYY-MM-DDfor MM to be a number. Thanks for confirming the stat output format. I'm trying to figure out where the OP is getting a month name abbreviation from.
    – Ed Morton
    Sep 26, 2022 at 16:06
  • 1
    @EdMorton oh yeah! you're right. What I cannot test is if those commands are POSIX compliant. I'm not sure if on macOS it might work Sep 26, 2022 at 16:10
  • 1
    @EdMorton about the month I have that doubt too, but I think the OP is using something like ls -l because that returns the date like: Jan 25 2021 Sep 26, 2022 at 16:13
  • 1
    idk about POSIX or not but they would work the same on MacOS. Hopefully the OP isn't really parsing the output of ls (see mywiki.wooledge.org/ParsingLs)!
    – Ed Morton
    Sep 26, 2022 at 16:13
  • 1
    @EdMorton thanks for that info! it's useful! I would think that the OP is using some tool/command to convert the date in DD-MM-YYYY format. Sep 26, 2022 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .