1

Hello I would like to know how to scan and change the last modified date of all subfolders based on the oldest file in each subfolder.

Example of Ubuntu folder structure:

home/incoming/media/Something.something.1234/

or

/Soemthing Soemthing 1234/

Which means there are folders with dots and without dots. Same goes for files some with dots and some without.

Files are mostly MKV media files maybe some mp4.

Also script should also skip all mkv or mp4 files in the root of media folder because they are without any subfolders.

2
  • Your requirements are so arbitrary that I suspect it's a classroom exercise. We don't do homework.
    – waltinator
    Oct 16, 2023 at 3:26
  • I’m voting to close this question because Your requirements are so arbitrary that I suspect it's a classroom exercise. We don't do homework.
    – waltinator
    Oct 16, 2023 at 3:26

1 Answer 1

1

In zsh:

for dir in path/to/media/*(NF); do
  oldest=( $dir/*.(mp4|mkv)(N.Om[1]) )
  if (( $#oldest )) touch -r $oldest -- $dir
done

Beware the last modification time of a directory is updated any time an entry is added, removed or renamed in it, so that touch may not hold for long.

6
  • I would add it run periodically each 7 days to fix any changes.
    – Loop1
    Oct 16, 2023 at 11:00
  • Works beautifully, could I ask you just two more question. Now when I sorted dates I found around 50 mkv files with year 2098 and year 2097. I see that .srt files and .info files carry the right date how would you issue toush command to read oldest (srt|info) and chage mkv in the same subfolder last modified date.
    – Loop1
    Oct 16, 2023 at 12:14
  • Also its not question about the touch, but I'm thinking to move all mkv files from the root of media folder to own subfolder for each mkv file with the same name as mkv filename beside .mkv extension on the end. Thanks again on your beautiful answers
    – Loop1
    Oct 16, 2023 at 12:20
  • for f (**/*.mkv) touch -r $f:r.srt -- $f would set set the modification time of mkv files to that of the srt file with the same root name. Oct 16, 2023 at 12:22
  • As they not directly related, you'd want to ask separate questions for those. Oct 16, 2023 at 12:23

You must log in to answer this question.

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