0

When I have a file a.txt and do ln -s a.txt b.txt, then I edit a.txt, a.txt's timestamp changed. But when I edit b.txt, b.txt doesn't change its timestamp. Only a.txt is changed. Why didn't b.txt's timestamp change. And a.txt's does.

Example:

$ ls -la
total 8
drwxr-xr-x  2 admin admin 4096 nov  5 16:53 .
drwxr-x--- 37 admin admin 4096 nov  5 16:53 ..
$ nano a.txt
$ ln -s a.txt b.txt
$ ls -la
total 12
drwxr-xr-x  2 admin admin 4096 nov  5 16:54 .
drwxr-x--- 37 admin admin 4096 nov  5 16:53 ..
-rw-r--r--  1 admin admin    4 nov  5 16:54 a.txt
lrwxrwxrwx  1 admin admin    5 nov  5 16:54 b.txt -> a.txt
$ echo wait a minute
wait a minute 
$ nano b.txt
$ ls -la
total 12
drwxr-xr-x  2 admin admin 4096 nov  5 16:56 .
drwxr-x--- 37 admin admin 4096 nov  5 16:53 ..
-rw-r--r--  1 admin admin    9 nov  5 16:56 a.txt
lrwxrwxrwx  1 admin admin    5 nov  5 16:54 b.txt -> a.txt 
$ echo wait a minute again
wait a minute again
$ nano a.txt
$ ls -la
total 12
drwxr-xr-x  2 admin admin 4096 nov  5 16:58 .
drwxr-x--- 37 admin admin 4096 nov  5 16:53 ..
-rw-r--r--  1 admin admin   13 nov  5 16:58 a.txt
lrwxrwxrwx  1 admin admin    5 nov  5 16:54 b.txt -> a.txt

Look at this lines first:

-rw-r--r--  1 admin admin    4 nov  5 16:54 a.txt
lrwxrwxrwx  1 admin admin    5 nov  5 16:54 b.txt -> a.txt

Then look at this, only a.txt's date changed when I edited b.txt

-rw-r--r--  1 admin admin    9 nov  5 16:56 a.txt
lrwxrwxrwx  1 admin admin    5 nov  5 16:54 b.txt -> a.txt 

Okay I know that b.txt is a symlink to a.txt. So you edit a.txt. But WHY didn't my OS change the date ONLY for a.txt. When I edit b.txt. It is much more useful to also change the date of b.txt.

But then you also can say that directory's won't change its date when I file is edited in the directory (i edited a file, and i used the directory). The directory is used to edit the file in it. But the os does change the directory date when i edit a file in it

4
  • 1
    Because b.txt did NOT change. None of its contituents was modified, only its target.
    – RudiC
    Nov 5, 2018 at 16:07
  • Okay, but the directory is not changed (no files added or removed). Only edited a file in the directory. Why does the date of the directory change? The state of the directory is not changed (files, meta etc). Nov 5, 2018 at 16:13
  • @SmileDeveloper that is a new question, but I will answer it anyway. Because it did change. a.txt was removed and replaced with a file called a.txt. Nov 5, 2018 at 16:18
  • OS systems does only edit the data on the disk that points to a.txt's data. Only that is edited. Or is the WHOLE file copied to the memory, file removed and recreated. So when I have a disk file of 4 GB I need to wait before the file is in memory, can be removed, and then can be recreated. That is what you are saying in 'a.txt is removed and replaced'. Nov 5, 2018 at 16:20

2 Answers 2

2

Because when you ask to edit b.txt you edit a.txt, the link is unaltered: Traversing the link does not change it.

Trying to access a link accesses the file that it points to (or the eventual file that is pointed to, the link may point to a link that points to a link that eventually point to a file/directory/something-else). Links are only access by using special link access routines. In addition soft-links can not be edited (only created and removed).

0

The answer:

  1. When you open a symlink to a file, you exactly did open the symlink, read the text in the symlink (the link) out, close it and opens the file where it links to. So that means when you edit it. You never edited the symlink file.

  2. Directory's contains another directorys or pointers to file like so:

This is the data on the beginning of the disk.

A 16:44:
   -- B 17:18:
       -- C 3kb: 15754
       -- D 4kb: 26544
   -- E 18:10:
       -- F 6kb: 16754
       -- G 8kb: 27544

When you edit a file in a DIRECTORY. the filesize is changed, the date is changed (and more attributes). And that is changed in the directory information. So you also edited the directory data (size, date...). And so is the directory changed.

Result:

This is the data on the beginning of the disk after editing C.

A 18:20:
   -- B 18:20:
       -- C 8kb: 15754
       -- D 4kb: 26544
   -- E 18:10:
       -- F 6kb: 16754
       -- G 8kb: 27544

You must log in to answer this question.

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