# finding the creation date



## abdelilah (Sep 21, 2013)

Hi everyone,

I have a folder with many subfolders, each subfolder contains an image beginning with the letter R and finishing with the extension rvg, the full name is something like Rx.rvg where x is a variable starting from 1.

I want to get the creation date of each rvg file with the lowest x, until now I have been using this command :

`find . -name "R1.rvg" -exec stat -f "%Sm %N" -t %Y%m%d%H%M%S {} \;`

but unfortunately it doesn't work if the number is different than one which might be the lowest x but not all the time, please help, thank you in advance.

PS: If possible it would be better to send the output to a text file.


----------



## jrm@ (Sep 21, 2013)

How about this little shell script?


```
#!/bin/sh

for dir in *; do 
    for file in $dir/R[0-9]*.rvg; do 
	stat -f "%Sm %N" -t %Y%m%d%H%M%S $file; break 1; 
    done; 
done
```

If you call the script blah, then run it like `% blah > ~/output.txt`.  Don't forget to make it executable with `% chmod u+x blah`.


----------



## abdelilah (Sep 21, 2013)

It says 
	
	



```
stat: Y.RVG/R[0-9]*.rvg: stat: No such file or directory.
```

knowing that with the `find` command which does it recursively, it does find the files.


----------



## abdelilah (Sep 21, 2013)

Here is the tree:

Parent folder

```
A.RVG --> A0000001 + A0000002 + A000000X
B.RVG --> A0000001 + A0000002 + A000000X
C.RVG --> A0000001 + A0000002 + A000000X
```


----------



## jrm@ (Sep 21, 2013)

This isn't what I expected based on your first post.  Now, I'm assuming the tree looks like


Parent folder
A.RVG (folder)
A0000001 (image)
         .
         .
         .
A000000X (image)
      .
      .
      .
?.RVG (folder)
A0000001 (image)
         .
         .
         .
A000000X (image)



and for each *.RVG folder you want to find the creation date of A000000X with the smallest X.

Here is the updated script.


```
#!/bin/sh

for dir in *.RVG; do 
    for file in $dir/A[0-9]*; do 
	stat -f "%Sm %N" -t %Y%m%d%H%M%S $file; break 1; 
    done; 
done
```


----------



## abdelilah (Sep 22, 2013)

Sorry to confuse you, I didn't describe it correctly:


```
KODAK (Parent folder)
A.RVG (folder)
A0000001 (Folder)
Rx.rvg(image).
.
.
A0000002 (folder)
Rx.rvg(image)..
.
.
A0000003 (folder)
Rx.rvg(image)..

?.RVG (folder)
?0000001 (folder)
Rx.rvg(image).
.
?0000002 (folder)
Rx.rvg(image).
```

So basically there is a parent folder with subfolders, each subfolder contains many subfolders containing images.

Thank you, I really appreciate your help.


----------



## abdelilah (Sep 22, 2013)

Please help. It still doesn't work with the same error message (please check my last message with the path correction).


----------



## jrm@ (Sep 23, 2013)

There might be a better way, but this is what I quickly came up with.


```
#!/bin/sh

for dirl1 in *.RVG; do
    for dirl2 in ${dirl1}/A[0-9]*; do
	for file in ${dirl2}/R*.rvg; do
	    stat -f "%Sm %N" -t %Y%m%d%H%M%S ${file}
	    break
	done
    done
done
```


----------



## abdelilah (Sep 23, 2013)

First of all I would like to thank you for your help, it worked very well except a detail. Here is the result:


```
20130107095715 A.RVG/A0000065/R1.rvg
20130309211641 A.RVG/A0000066/R1.rvg
20130511092726 A.RVG/A0000067/R1.rvg
20130529114029 A.RVG/A0000068/R1.rvg
20130529160725 A.RVG/A0000069/R1.rvg
20130603161344 A.RVG/A0000070/R2.rvg
20130710125009 A.RVG/A0000071/R3.rvg
20130802130855 A.RVG/A0000072/R1.rvg
20130817070355 A.RVG/A0000073/R1.rvg
20130830091842 A.RVG/A0000074/R1.rvg
stat: B.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: C.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: D.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: E.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: F.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: G.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: H.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: I.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: J.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: K.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: L.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: M.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: N.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: O.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: Q.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: R.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: S.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: T.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: U.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: W.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: Y.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
stat: Z.RVG/A[0-9]*/R*.rvg: stat: No such file or directory
```

Once finished with the actual letter A the script must go up and then go to the next letter which it fails to do.

I really think we're nearly there, please help, and again thank you for your help.


----------



## jrm@ (Sep 23, 2013)

I'm making some guesses about the formats, but hopefully this will do it.


```
#!/bin/sh

for dirl1 in *.RVG; do
    for dirl2 in ${dirl1}/[A-Z]*[0-9]*; do
	for file in ${dirl2}/R*.rvg; do
	    stat -f "%Sm %N" -t %Y%m%d%H%M%S ${file}
	    break
	done;
    done; 
done
```


----------



## abdelilah (Sep 24, 2013)

Thank you, this fully solves the case.


----------



## grahamperrin@ (Mar 22, 2017)

Please: how does what's above relate to `st_birthtime` e.g. as in the FreeBSD manual page for stat(1)?

(I'm not great with scripts or manual pages. Sorry.)


----------



## jrm@ (Mar 22, 2017)

If you want birth time instead of modification time use `stat -f "%SB %N" -t %Y%m%d%H%M%S ${file}`.


----------



## jrm@ (Mar 22, 2017)

Also, for posterity, I don't think the other script sorted numerically.  That is, it would have sorted R10.rvg before R2.rvg.  This should do it.

```
#!/bin/sh

for dirl1 in *.RVG; do
    for dirl2 in ${dirl1}/[A-Z]*[0-9]*; do
        stat -f "%Sm %N" -t %Y%m%d%H%M%S \
             "$(find "${dirl2}" -name "R*.rvg" | sort -V | head -n1)"
    done;
done
```


----------



## grahamperrin@ (Mar 23, 2017)

Thanks.

Now for myself I have commands such as:

`find ~/Documents -type f -exec stat -f "%SB %N" -t %Y-%m-%d\ %H:%M:%S {} \; | sort | less`


----------

