LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-03-2016, 03:35 AM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Rep: Reputation: Disabled
Detecting a txt file being opened


Is it possible to detect when a file has been opened?

If I place a 'readme.txt' file on the desktop is it possible to log when users open it up?

Many thanks
Tim
 
Old 09-03-2016, 03:39 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
man lsof
detect if it is open.
logging probably requires additional software.
 
Old 09-03-2016, 03:47 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Try this http://linux.die.net/man/1/inotifywait ; some examples https://superuser.com/questions/1815...a-file-changes
 
2 members found this post helpful.
Old 09-03-2016, 05:45 AM   #4
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
Code:
man lsof
detect if it is open.
logging probably requires additional software.
Ok thanks. So I would have thought that if I had a file called readme.txt on my desktop and it's open by me then by using:

Code:
lsof +d /home/tim/Desktop
It would return infirmation saying the readme.txt file is open but what I get back is
Code:
wineserve 13449  tim   47r   DIR    8,1     4096 189548 /home/tim/Desktop
and this does not change if the file is open or not.

Last edited by Entropy1024; 09-03-2016 at 05:57 AM.
 
Old 09-03-2016, 06:12 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You're using lsof on the wrong entity: "/home/tim/Desktop" isn't a file in this context (everything is a file) and it isn't the file you want to track. Following chrism01s suggestion of using inotifywait, esp. with logging, makes more sense.
 
1 members found this post helpful.
Old 09-03-2016, 08:25 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
You have plenty of options:
https://superuser.com/questions/1815...a-file-changes

Best regards,
HMW
 
Old 09-03-2016, 03:34 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by HMW View Post
That thread may elaborate a bit but it actually doesn't add much and most have been discussed here already. As Gilles says over there:
Quote:
(..) most systems have a file change notification mechanism (inotify on Linux, kqueue on FreeBSD, ...)
Using anything else is nice (wheel-reinvention isn't bad if that's the goal) but not efficient.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] read txt file into an array and make a second txt file zimbot Linux - General 12 09-05-2015 01:39 PM
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 07:23 AM
Copy the contents of a txt file to other txt files (with similar names) by cp command Aquarius_Girl Linux - Newbie 7 07-03-2010 12:54 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration