HattrickNZ
HattrickNZ

Reputation: 4653

Can I change the modified date, just the year, of a file or all files?

Can I change the modified date, just the year, of a file or all files? I have been looking here which lead me to touch.

$ ls -l *.txt
-rw-r--r-- 1 kevin.smith mkpasswd  3319 Nov 21  2017 adjectives.txt
-rw-r--r-- 1 kevin.smith mkpasswd 25562 Aug 11  2015 checklist.txt
-rwxr-xr-x 1 kevin.smith mkpasswd 11347 May  9 14:28 cw_text.txt
-rw-r--r-- 1 kevin.smith mkpasswd  9260 May  9 14:31 cw_text2.txt
-rw-r--r-- 1 kevin.smith mkpasswd  4786 May  9 14:38 cw_text3.txt
-rw-r--r-- 1 kevin.smith mkpasswd   390 Jun 25  2014 Delete_log.txt
-rw-r--r-- 1 kevin.smith mkpasswd  6891 Jul 27  2015 log.txt
-rw-r--r-- 1 kevin.smith mkpasswd 53828 Jan 17  2017 pin1.txt
-rw-r--r-- 1 kevin.smith mkpasswd 39412 Jan 17  2017 pip2.txt
-rw-r--r-- 1 kevin.smith mkpasswd   167 Dec  5  2015 romeo.txt

$ touch -t 2018* *.txt

Expected Output: Would have only the year changed to 2018

$ ls -l *.txt
-rw-r--r-- 1 kevin.smith mkpasswd  3319 Nov 21  2018 adjectives.txt
-rw-r--r-- 1 kevin.smith mkpasswd 25562 Aug 11  2018 checklist.txt
-rwxr-xr-x 1 kevin.smith mkpasswd 11347 May  9 14:28 cw_text.txt
-rw-r--r-- 1 kevin.smith mkpasswd  9260 May  9 14:31 cw_text2.txt
-rw-r--r-- 1 kevin.smith mkpasswd  4786 May  9 14:38 cw_text3.txt
-rw-r--r-- 1 kevin.smith mkpasswd   390 Jun 25  2018 Delete_log.txt
-rw-r--r-- 1 kevin.smith mkpasswd  6891 Jul 27  2018 log.txt
-rw-r--r-- 1 kevin.smith mkpasswd 53828 Jan 17  2018 pin1.txt
-rw-r--r-- 1 kevin.smith mkpasswd 39412 Jan 17  2018 pip2.txt
-rw-r--r-- 1 kevin.smith mkpasswd   167 Dec  5  2018 romeo.txt

Upvotes: 0

Views: 288

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

ls -l file.txt; touch -t "$(date -d "@$(stat -c '%Y' file.txt)" "+2020%m%d%H%M")" file.txt; ls -l file.txt
#               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-rw-r--r-- 1 jackman jackman 0 Oct 23 16:47 file.txt
-rw-r--r-- 1 jackman jackman 0 Oct 23  2020 file.txt

You'll need to use a for loop to iterate over the files, and query/update the mtime one-by-one

Upvotes: 2

Related Questions