1

How can I get the last changed content of a file, since its last read?

Something more precise that this:

tail - <nr >file_1.txt >file_2.txt

where file_2.txt gets the last modified content.

1 Answer 1

0

The only way I can think of doing something like this would be to keep a copy of the file in a temporary name when you last read it and then when you re-read it again you'll need to use a tool such as diff to compare the previously read copy of the file with the current version of the file.

Example

Some sample file.

$ seq 10 > somefile.txt
$ ls -l |grep somefile.txt 
-rw-rw-r--   1 saml saml      21 Oct 26 13:29 somefile.txt

Now let's read it.

$ cat somefile.txt | tee somefile_READ1.txt
1
2
3
4
5
6
7
8
9
10

Here the command tee will show us the file and create a "backed up" copy that shows us the lines we read the next time. We're now left with these files:

$ ls -l |grep somefile
-rw-rw-r--   1 saml saml      21 Oct 26 13:30 somefile_READ1.txt
-rw-rw-r--   1 saml saml      21 Oct 26 13:29 somefile.txt

Now some additional lines get added:

$ seq 11 20 >> somefile.txt

We can see by the date that it's been written to since the last time we read it by comparing the dates of the 2 files:

$ ls -l |grep somefile
-rw-rw-r--   1 saml saml      21 Oct 26 13:30 somefile_READ1.txt
-rw-rw-r--   1 saml saml      51 Oct 26 13:32 somefile.txt

Now compare the 2 files:

$ diff -y somefile.txt somefile_READ1.txt  | column -t
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10  10
11  <
12  <
13  <
14  <
15  <
16  <
17  <
18  <
19  <
20  <

Above we can see the 2 files side by side. The updated file's contents is on the left, the original contents are on the right.

If you only want to see the differences you can use this form of diff instead:

$ diff somefile.txt somefile_READ1.txt 
11,20d10
< 11
< 12
< 13
< 14
< 15
< 16
< 17
< 18
< 19
< 20

You must log in to answer this question.

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