2

I have a folder in which there are thousands of images. Of those thousand images, I want to bring together all those images whose “Last created” and “Last modified” attributes are exactly same. ( In other words, I want to separate all the images I didn’t ever rename for example )

Is there a way to do this from terminal ?

these are the two commands that I want to concatenate ls -tU lists file by creation date. (I'm on a mac) ls -lt lists file by modified date

such that I want to "List files whose date created = date modified"

2

You can do this using the stat command. Here is a simple script which prints the filenames, with some allowances for embedded blanks:

#!/bin/sh
find $* -exec stat -f '%m %B %N' {} \; | \
awk '$1 == $2 { \
  s=length($1)+length($2)+3;
  $0 = substr($0,s);
  print $0;
}'

The %m and %B are respectively the modified and birth times (which appear to be what was asked). The script prints those as decimal numbers, passing to awk which then prints only the lines where those numbers are the same. Because your filenames may have embedded blanks, the rest of the script removes the timestamp values from the input and prints the remainder of the line.

Further reading:

0

I felt bored and I have use for such a script as well so I changed Thomas Dickey's in little ways, not in any drastic way but just e.g. so that you can deal with all sort of file names (even those with newlines) and easily change the separator between the file names (e.g. just set ORS to "\0" to forward it to xargs -z …)

#!/bin/sh
find "${1-.}" -type f -exec stat --printf '%n\0%Y %W\0' -- {} \; | \
awk 'BEGIN {
  RS  = "\0";
  FS  =  " ";
  ORS = "\n";
} {
  if (NR % 2) {
    f = $0;
  } else if ($1 == $2) {
    print f;
  }
}'

Not entirely POSIX because of e.g. --printf.

  • OSX find manual page does not mention -printf (my script does work with OSX). – Thomas Dickey Jun 18 '16 at 19:00
  • @ThomasDickey Oh damn, I missed the "osx" tag. – phk Jun 18 '16 at 20:44
  • Also, newlines are rarely used in filenames, other than as an example on this forum. – Thomas Dickey Jun 18 '16 at 20:52
  • @ThomasDickey I know, I know. Apart from maybe a security aspect POV (for when you are dealing with user-controlled input) there is no good reason for trying to cope with all that craziness concerning all possible file names. I do it mostly out of fun (it's kinda like a puzzle sometimes) but also to train myself for cases where it then really has relevance for security for once. – phk Jun 18 '16 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.