Find all files with (corrupt) future date and modify to today
Asked Answered
M

2

6

231, of a few thousand, files in a folder hierarchy have bad (null?) creation & modification dates. I can identify them:

find . -mtime -0

And their dates are all well into the future. Not sure exactly but the few I've looked at with ls -l appear to be in year 2040.

Anyway, I'd like to 1st display the creation and modification date in the output of above command, if possible.

Second, and more importantly, I'd like to change the creation & modification dates of those files to today (now).

I'm not much of a bash guru but I know there's an easy way. Thanks in advance!

Monarchism answered 24/10, 2013 at 16:18 Comment(0)
I
4

I'd use "find" as follows and exec "touch" to set the modification date:

find . -mtime -0 -exec touch {} \;

Not sure how/if it is possible to change the creation date. You may have to recreate the file by copying it to a new, temporary name, then renaming it back to the original name. GNU touch may help here...

You can use "ls -Ul" for file creation date, and "ls -l" for modification times like this:

find .-mtime -0 -exec ls -Ul {} \;

or

find .-mtime -0 -exec ls -l {} \;
Iridaceous answered 24/10, 2013 at 16:55 Comment(0)
B
-1

One trick is to copy and move the file back in place. Beware of the ownership and mode of the file or it will break your system in a more severe way.

As an example here's fstab in the wrong timestamp:

$ stat /etc/fstab
  File: '/etc/fstab'
  Size: 37          Blocks: 8          IO Block: 4096   regular file
Device: b30ah/45834d    Inode: 504         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    1000/    user)   Gid: (    0/    user)
Access: 2105-12-20 17:23:10.624000001 +0000
Modify: 2018-07-31 00:29:37.000000000 +0000
Change: 2020-01-21 08:58:48.779991299 +0000
 Birth: -



$ sudo cp  /etc/fstab /etc/fstab_  --preserve=ownership --preserve=mode
$ sudo mv  /etc/fstab_ /etc/fstab



$ stat /etc/fstab
  File: '/etc/fstab'
  Size: 37          Blocks: 8          IO Block: 4096   regular file
Device: b30ah/45834d    Inode: 1534        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    1000/    user)   Gid: (    1000/    user)
Access: 2020-02-06 16:52:33.684297747 +0000
Modify: 2020-02-06 16:52:33.684297747 +0000
Change: 2020-02-06 16:52:43.093550721 +0000
 Birth: -
$ 

Within a 2 liner command set this would be:

$ sudo find /  -type f -newermt 2020-02-07 -exec cp {} {}_ --preserve=ownership -preserve=mode  \;

Then find the copied files and move them back into position

$ sudo find /  -type f -iname "*_"  -exec mv {}_ {}   \;

You can check the dates with following command:

$ sudo find /  -type f -newermt 2020-02-07

Note: As a trailing character i've chosen "_", please adapt to your needs to avoid any conflicts in your file system.

Bryantbryanty answered 6/2, 2020 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.