I want to write a script that obtains a list of all open programs and files, and then generates a shell script to re-open these files. For example, if I were editing the file example.txt using gedit, and editing the file myscript.js using geany, I would want to generate a script that could re-open both of these files using their respective programs. Is there any way to obtain a list of running programs and files from the command line?
|
Generally, it is not possible to do in the way you're approaching this, because the files opened in a text editor are not "open" in the sense programmers use the term (i.e. "having an active file handle somewhere within the program which can be used for I/O operations"). What most programs do is open the file, read the data in a memory buffer, then close the file. Saving a file is the same - open, write, close. Between open and save operations the file is not "open", it's just the copy of the data in the program's buffer in memory. So it is generally not possible to figure out which files are currently loaded into the program's memory - however, the program itself does know that, so from inside the program it is trivial to implement an option to remember the list of open files on shutdown and re-open those files on startup. Many text editors, for example Kate and Sublime Text 2 do provide such option. Some desktop environments, such as KDE (and also the old Gnome, I'm not sure about Gnome 3/Unity) also provide an option to save the list of running programs on shutdown and re-launch those programs on startup. Which combined with using a good editor kinda achieves what you're trying to do. |
|||||||||||
|
|
The command |
|||||||||||
|
|
There is trick you can try [may be in gnome] this show files opened by a process [may not currently using] History file
may help you.
For a DEMO just copy paste
from the output you can extract filename and process. |
|||
|
|

lsofbefore, but I'm not sure that it's comprehensive. (I've used it to see what was preventing me from ejecting a volume, but it sometimes cannot find which file was open.) – Sparhawk Dec 29 '12 at 1:39lsofoutput a list of open files (as well as a list of running programs)? – Anderson Green Dec 29 '12 at 1:43lsofdoes do that. (Give it a go!) Also, I think thatps auxis more process oriented, and hence does not list all open files. I don't have gedit on my computer (I use KDE), but when I open a file in kate, the filename is not listed inps aux. – Sparhawk Dec 29 '12 at 2:17ps auxonly lists files that are open if they were arguments of the command used to start the process. If I start kate, then open files from within it, the output ofps aux | grep [k]ateissparhawk 4606 15.5 0.3 467332 58188 ? Sl 13:28 0:01 /usr/bin/kate -b. I'll trylsofand print the output. – Sparhawk Dec 29 '12 at 2:34