Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I've used lsof before, 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:39
@Sparhawk Will lsof output a list of open files (as well as a list of running programs)? – Anderson Green Dec 29 '12 at 1:43
Here's additional relevant information:… – Anderson Green Dec 29 '12 at 1:50
lsof does do that. (Give it a go!) Also, I think that ps aux is 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 in ps aux. – Sparhawk Dec 29 '12 at 2:17
I think ps aux only 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 of ps aux | grep [k]ate is sparhawk 4606 15.5 0.3 467332 58188 ? Sl 13:28 0:01 /usr/bin/kate -b. I'll try lsof and print the output. – Sparhawk Dec 29 '12 at 2:34

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.

share|improve this answer
Is it possible to detect when a file is modified by a specific program (for example, detect when the file stuff.txt is modified using the program gedit)? – Anderson Green Dec 29 '12 at 2:52
Yes, there's an API which allows to monitor file access called inotify. There's also a command-line tool called inotify-watch from inotify-tools package. – Sergey Dec 29 '12 at 2:58
Can inotify detect the specific program that a file was modified by (instead of simply detecting file changes?) I'm still not sure if it can detect the specific program that is used to access or modify a file. – Anderson Green Dec 29 '12 at 3:00
I don't think it can, but for your usage (reinvoking a previous session), can you not just define your default programs properly, then use something like xdg-open <filename>. – Sparhawk Dec 29 '12 at 3:06
@AndersonGreen: "The inotify API provides no information about the user or process that triggered the inotify event." - however, if you monitor the "file open" event you can try using lsof to catch the file owner before the editor closes the file :) Although the approach feels very messy. See this for details:… – Sergey Dec 29 '12 at 3:10

The command ps aux will display a list of running processes. In some cases, it will also list the files that are being edited with those programs, if the file name was used as a command line argument when starting the process. For example, I opened the file stuff.txt using gedit, and gedit /home/anderson/Desktop/stuff.txt was displayed in the output of ps aux.

share|improve this answer
It appears that it would be feasible to use the output of ps aux to generate a script that could automatically re-open the list of running files (for a specific set of programs, if desired). – Anderson Green Dec 29 '12 at 2:20
ps aux does not show "the files that are being edited with those programs", it shows the command line used to start the program. Which would only work if you do something like gedit myfile.txt. It won't work if you started gedit and then used File-Open to open the file. – Sergey Dec 29 '12 at 2:37
@Sergey Thanks for the info. I updated my answer. – Anderson Green Dec 29 '12 at 2:40
It appears that the equivalent of ps aux on Windows is tasklist /v. – Anderson Green Dec 29 '12 at 2:47

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.

  1. First take a copy of recently-used.xbel
  2. Run process and exit [open file, close it. open file, close it and so on... and exit]
  3. now compare recently-used.xbel with old recently-used.xbel
  4. from the diff extract file name and associated process

For a DEMO just copy paste

diff <(cat ~/.local/share/recently-used.xbel) <(gedit /tmp/2.txt; cat ~/.local/share/recently-used.xbel)

from the output you can extract filename and process.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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