0

In Ubuntu if the user is not having permission to write a file but if he tries to change, the last updated time of that file will change(even after getting write error: Operation not permitted). So, I wanted the last modified time of the file if the user successfully save or modified the content is that possible.

  • 1
    Can you give a minimal example for the claim "the last updated time of that file will change even after getting write error". For me (16.04, ext4), this is clearly not the case. Which file system? – pLumo Feb 27 at 12:22
  • @RoVo actually an editor that saves a R/O file can save a new copy, remove the R/O file (possible since the parent directory is R/W) and then rename the new copy (and set it R/O). But I agree that the OP should describe the use case more clearly (even though I have little hope that there is a solution to this). – xenoid Feb 27 at 12:32
  • But then it is a new file, and this does not happen after a write error. – pLumo Feb 27 at 12:33
1

The command stat gives you that information. You can see three timestamps. Access, Modify and Change. By (succesfuly) reading the file, Access is modified. By changing for example the permissions, Change is modified. If the content is changed, Modify is changed. Take a look at this:

#case@maru:~/temp:[0]$ rm test 
#case@maru:~/temp:[0]$ echo test>test;stat test
  File: test
  Size: 5           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:52:39.715956782 +0000
Modify: 2019-03-04 17:52:39.715956782 +0000
Change: 2019-03-04 17:52:39.715956782 +0000
 Birth: -

File created. All the timestamps are the same.

#case@maru:~/temp:[0]$ echo test2>test;stat test
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:52:39.715956782 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:52:53.739979011 +0000
 Birth: -

Content modified. Modify and Change has changed.

#case@maru:~/temp:[0]$ cat test ; stat test 
test2
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:52:53.739979011 +0000
 Birth: -

File read. Access changed.

#case@maru:~/temp:[0]$ chmod 0000 test ; stat test
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0000/----------)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:54:13.792092496 +0000
 Birth: -

Changing perms. Change modified.

#case@maru:~/temp:[0]$ echo test3>test;stat test
bash: test: Permission denied
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0000/----------)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:54:13.792092496 +0000
 Birth: -

Failed modification. No changes

#case@maru:~/temp:[0]$ cat test ; stat test 
cat: test: Permission denied
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0000/----------)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:54:13.792092496 +0000
 Birth: -

Failed reading. No changes.

#case@maru:~/temp:[0]$ chmod 0644 test ; stat test
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:55:45.860198022 +0000
 Birth: -

Restoring perms. Change modified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.