Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lsof to get a list of file names

EDIT 1

I'm having problems using the arguments given. Maybe it is the way I'm passing my arguments through NSTask? Any suggestions as to how I can do this?

NSTask *file_Task = [NSTask new];
[file_Task setLaunchPath:@"/usr/sbin/lsof"];
[file_Task setArguments:[NSArray arrayWithObjects:@"+p", the_Pid, nil]];

Good Afternoon Fellow Coders....

I'm using the following command:

lsof +p 13812

to get the list of a files accessed by a process. The thing is it is giving me a lot of additional information that I don't want such as TYPE, DEVICE, etc.

Is there an argument that I can add to the above command so that I get ONLY the NAME?

Thank you, thank you thank you! :)

Eric

like image 804
Eric Brotto Avatar asked Aug 25 '10 14:08

Eric Brotto


2 Answers

You can use:

lsof -Fn +p 12345

This will output a list of lines, with the first being p followed by the process ID, and all following lines consisting of n followed by the file name.

If you'd like to quickly preprocess this, you can do something similar to the following:

lsof -Fn +p 12345 | tail -n +2 | cut -c2-

See the lsof man page for more information, specifically under the OUTPUT FOR OTHER PROGRAMS heading.

like image 182
Hasturkun Avatar answered Oct 28 '22 13:10

Hasturkun


try:

lsof | tr -s ' ' | cut -d' ' -f9
like image 39
jcubic Avatar answered Oct 28 '22 12:10

jcubic