Miles
Miles

Reputation: 677

Set mtime based on date string in filename

Given a group of files with the following naming convention:

datetime_restofname.txt

an example of which would be:

200906290700_somewordorphrase.txt

how could I batch change the mtime of the files to match the date and time in the filename?

Upvotes: 1

Views: 2656

Answers (1)

sunny256
sunny256

Reputation: 9576

$ for f in *.txt; do touch -t `echo $f | cut -f1 -d _` "$f"; done

This will set the file modtime to the date string before the underscore.

Upvotes: 7

Related Questions