0

I recovered images from an android LOST.DIR folder.

The data recovered successfully, now I'd like to set the modified timestamp of the file to be equal to the created on value of the binary data.

I'm using Ubuntu 19.10

enter image description here

enter image description here

In this case, I'd like both modified and created on to equal 2019:06:03, the format doesn't matter.

The solution should support looping over all files in the folder.

noam steiner
  • 103
  • 3
  • AFAIK not tools exist for that. Also see this: https://unix.stackexchange.com/questions/591384/copying-or-restoring-crtime-for-files-directories-on-ext4fs-filesystem – Artem S. Tashkinov Feb 13 '21 at 14:47
  • 1
    @Artem S. Tashkinov: I understand the OP want to set the file system *modified* timestand to be equal to the EXIF *creation time*. The link you posted is about setting the *creation* time in the file system. – phunsoft Feb 13 '21 at 15:00
  • The file modified timestamp is copied and respected by most file utilities under the Sun and created on time is a whole different affair which is what the OP is seemingly looking for. – Artem S. Tashkinov Feb 13 '21 at 15:17
  • The recovery process the OP ran created new files with create and modified timestamps equal to the revocery time, right? I understand the OP would like the files system modified timestamp the reclect the creation time of the original image file (which is an EXIF tag). – phunsoft Feb 13 '21 at 15:51
  • Actually now I suspect that the recovery didn't work. Because the files doesn't have the file type suffix. But I am able to see the file type in the properties popup and to open the file in image viewer. If I add the suffix manually, I can even open it on my mobile. I tried using `photorec`. – noam steiner Feb 13 '21 at 16:05
  • You can open the files recovered in an image viewer and it looks like the images you expect? So the recovery did succeed, I'd say. The suffix in the file name is just part of the filename, and is interpreted by some programs. It has no influence on the content of the files. You might name a *pure text file* "image.jpg", but that doesn't make an image out of it. Rename the files and add the suffix (jpg, tif, or whatever applies). – phunsoft Feb 13 '21 at 16:10
  • Ok, than the recovery did work. – noam steiner Feb 13 '21 at 16:18

1 Answers1

2

You need an EXIF tool to retrieve the image creation timestamp, then use touch to set the filesystem timestamp accordingly.

I just tried this shell script (e.g. ex.sh) under ArchLinux where I installed perl-image-exiftool

#! /bin/bash

for fn; do
    ls -l "$fn"
    touch -m -t "$(exiftool -createdate -d '%Y%m%d%H%M.%S' -s3 "$fn")" "$fn"
    ls -l "$fn"
    echo "------------------"
done

You can omit the echo and ls... lines; they're just there to display before and after timestamps of the files.

./ex.sh *.jpg

or

./ex.sh 01.jpg 02.jpg
Freddy
  • 25,172
  • 1
  • 21
  • 60
phunsoft
  • 178
  • 8
  • @Freddy: Thanks. Acutally the lowercase "s" was a typo I didn't recognize :-( – phunsoft Feb 13 '21 at 16:14
  • I made a small edit, I hope you don't mind. – Freddy Feb 13 '21 at 16:38
  • Not if you help me to understand what the benefit of *"$fn"* over *$fn* is :-) I understand single quoting the parameter of the *-d* option is to avoid indvertant changes by the shell. But why to quote the shell variables which I want to be exnaded before the line is executed. – phunsoft Feb 13 '21 at 17:27
  • There is no difference if your filenames don't contain any whitespace characters :) See [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/questions/131766) and [When is double-quoting necessary?](https://unix.stackexchange.com/questions/68694). – Freddy Feb 13 '21 at 17:56
  • The single quotes around the format string are not strictly necessary in this example as there is no whitespace in the format string. Same goes for the quotes of the command substitution `"$(...)"`. But it's safer to quote them anyway. The quotes around `$fn` prevents [splitting](https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html) on the values of the `IFS` variable (space, tab, newline). – Freddy Feb 13 '21 at 18:18