Skip to main content
edited body
Source Link
mx0
  • 343
  • 2
  • 13

I have a lot of files where filename contains file creation date (IMG_RRRRMMDD_hhmmssIMG_YYYYMMDD_hhmmss.jpg):

IMG_20171015_133516.jpg
IMG_20171015_133827.jpg
IMG_20171015_142634.jpg
IMG_20171015_142834.jpg
IMG_20171015_142857.jpg

but actual file creation date is different. I need to set it back to date from a filename.

I know how to change the date of all files to current date:

find -type f -exec touch {} \;

and how to parse the file name to get the date in the proper format for touch -t

echo IMG_20171015_133516.jpg | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}'
201710151335.16

But I have no idea how to combine these commands together to change all files.

find -type f -exec touch -t"$(echo '{}' | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}')" {} \;

returns

touch: invalid date format ‘.’

I have a lot of files where filename contains file creation date (IMG_RRRRMMDD_hhmmss.jpg):

IMG_20171015_133516.jpg
IMG_20171015_133827.jpg
IMG_20171015_142634.jpg
IMG_20171015_142834.jpg
IMG_20171015_142857.jpg

but actual file creation date is different. I need to set it back to date from a filename.

I know how to change the date of all files to current date:

find -type f -exec touch {} \;

and how to parse the file name to get the date in the proper format for touch -t

echo IMG_20171015_133516.jpg | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}'
201710151335.16

But I have no idea how to combine these commands together to change all files.

find -type f -exec touch -t"$(echo '{}' | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}')" {} \;

returns

touch: invalid date format ‘.’

I have a lot of files where filename contains file creation date (IMG_YYYYMMDD_hhmmss.jpg):

IMG_20171015_133516.jpg
IMG_20171015_133827.jpg
IMG_20171015_142634.jpg
IMG_20171015_142834.jpg
IMG_20171015_142857.jpg

but actual file creation date is different. I need to set it back to date from a filename.

I know how to change the date of all files to current date:

find -type f -exec touch {} \;

and how to parse the file name to get the date in the proper format for touch -t

echo IMG_20171015_133516.jpg | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}'
201710151335.16

But I have no idea how to combine these commands together to change all files.

find -type f -exec touch -t"$(echo '{}' | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}')" {} \;

returns

touch: invalid date format ‘.’

edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 263
Source Link
mx0
  • 343
  • 2
  • 13

Set file modification date from date stored as filename

I have a lot of files where filename contains file creation date (IMG_RRRRMMDD_hhmmss.jpg):

IMG_20171015_133516.jpg
IMG_20171015_133827.jpg
IMG_20171015_142634.jpg
IMG_20171015_142834.jpg
IMG_20171015_142857.jpg

but actual file creation date is different. I need to set it back to date from a filename.

I know how to change the date of all files to current date:

find -type f -exec touch {} \;

and how to parse the file name to get the date in the proper format for touch -t

echo IMG_20171015_133516.jpg | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}'
201710151335.16

But I have no idea how to combine these commands together to change all files.

find -type f -exec touch -t"$(echo '{}' | awk -F_ '{print $2 substr($3,0,4) "." substr($3,5,2)}')" {} \;

returns

touch: invalid date format ‘.’