Link to home
Start Free TrialLog in
Avatar of attila01
attila01

asked on

How list the opened file under Linux?

Hi All,

How can I check the opened file? I got an answer some days ago saying that I have to use e.g.
this: ls -l /proc/*/fd | wc -l
When I used the above I got following answer:

# ls -l /proc/*/fd | wc -l
/proc17573/fd: No such file or directory
    3282



Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Ok, you got a number, didn't you? (3283)
The error message is due to the transient nature of /proc. The process simply doesn't exist any more, and thus the directory has gone, too.
Get rid of the message by using
ls -l /proc/*/fd 2>/dev/null |wc -l
wmp
 
Hi ya
have you ever use lsof command ??

this is the best command to see what files are open by what process

suppose
if you want to see what process re running on the sytem

lsof -i

it will show you something like

httpd   21540 nobody    3u  IPv6  90970       TCP *:http (LISTEN)
httpd   21542 nobody    3u  IPv6  90970       TCP *:http (LISTEN)


now if you want to see what files are using by  httpd or process 21540

lsof -p 21540

it wills how you all the file

have a look to this site for more informaion

http://www.cyberciti.biz/faq/howto-linux-get-list-of-open-files/


basicaly you can do so many thing by lsof command

also this site

http://dmiessler.com/study/lsof/
Avatar of attila01
attila01

ASKER

After entering ...
ls -l /proc/*/fd 2>/dev/null |wc -l

I get the message:
Ambiguous output redirect
The command lsof -i and lsof -p show me a lot processes.
I see appr. 30 times txt  for my username but I have only 2 doc file open.

Not an easy task for me if a user tells me to close an opened files. e.g .xls
sorry went to launch, let me read your comments

i have doubt about this ls -l /proc/*/fd 2>/dev/null |wc -l

let me try

what actually you are trying to do ??
When I used the above I got following answer:

# ls -l /proc/*/fd | wc -l
/proc17573/fd: No such file or directory
    3282

im not sure why but the path i see is
/proc17573/fd
and probably does not exist.

if you retyped it then its probably a process that ended while the ls command was running.
first the shell interpret the * and then run the command.

you can run
lsof /home -u username
and narrow the results.
or to look for specific files
lsof /home -u oren | grep -E ".xls|.doc|pdf"
fosiul01, I try to do following:
It happens often that a user asks me to close an .xls file. The reason: The user edited the .xls file last closed the file but the next user still can't open it because the system tells him that the first user still has the file open.

Oren,
I entered following:
atkoc&kshsun01:/usr2/home/atkoc % lsof /home -u atkoc | grep -E ".xls|.doc|.pdf"

I got the following answer:

grep: illegal option --E
Usage: grep -hblcnsviw pattern file ...
lsof: status error on -u: No such file or directory
lsof: status error on atkoc: No such file or directory
 
you can try this

lsof -u atkoc |  grep -E ".xls|.doc"

it will show you all open files open by atkoc

does this one work ??
I didn't get the "No such file or directory" error anymore but the other two lines are still appear.

grep: illegal option --E
Usage: grep -hblcnsviw pattern file ...

The server I tried on has Sun Solaris. I tried the command on the file server that has Linux and got the same 2 lines and the following line as well:
lsof: WARNING: can't stat () 8 zone file system; using dev= options

I opened an .xls file on all servers hoping the system sees it after entering the command, but no succes till now.
Use 'egrep' instead of 'grep -E'
 
You should of tell that is Solaris!!!!

in solaris there is a different i guess for lsof and grep command ,  

let me check in my solaris pc [ i am new in solaris aswell]
The Sun Solaris is only available for a few user to create files, I only wanted to test it there because I thought it is the same command to use.

The users ask me to close open files, opening/editing files on the file server that has Linux.
That means I need the command for Linux.
Ok in linux pc,
what error you get if you execute thsi command as i said before

lsof -u atkoc |  grep -E ".xls|.doc"      ??

does it return anything ??
or just type this

lsof | grep filename.extenstion

it should show you if that file is open or not

lsof | grep filename.xls
Why are you all using grep? Please read the man pages of commands before posting advise to people!

If you wanted to know who has a specific file open, you would use
# lsof <path_to_file>

If you wanted to view a processes open files, you would use
# lsof -p <process_id>

If you wanted to view a users open files, you would use
# lsof -u <user_id>

Simple.
If I enter lsof -u atkoc | atkoc egrep ".doc|xls"  (on Sun Solaris) happens nothing, no error

If I enter lsof -u atkoc | atkoc egrep ".txt"   (on Sun Solaris) I get a huge amount of data like:
xterm  22787  atkoc  txt  VREG  85,10  21676   2290   /usr/lib/libmp.so.2

approximately 130 lines

@bilco105

why using grep : if you dont use grep then lsof will show you thousand of files such as

lsof -u <user_id> : in my system lsof -u fosiul , will return more then 2000 files!

[fosiul@cd4070nw ~]$ /usr/sbin/lsof -u fosiul | wc -l
2547

so use grep to make this minimum
/usr/sbin/lsof  -u fosiul | grep mydoc.xls

it will show you only one file
@author

if you type like this

lsof -u atkoc | atkoc egrep ".txt"

it will show you all the open file wiht extension .txt

which you dont want i guess

so thats why put exact file name, you said you want to kill a file name

why dont you use that file name ??

lsof -u atkoc | atkoc egrep file-name-to-look-for.txt

or lsof | egrep file-name-to-look-for.txt

if you dont know the file name, it wil give you all .txt open files
@fosiul01

No, you just have no understanding of the lsof command - hence why I said to read the man pages..

Quote:

lsof  [  -?abChlnNOPRstUvVX  ]  [ -A A ] [ -c c ] [ +c c ] [ +|-d d ] [
       +|-D D ] [ +|-f [cfgGn] ] [ -F [f] ] [ -g [s] ] [ -i [i] ] [ -k k  ]  [
       +|-L  [l]  ]  [ +|-m m ] [ +|-M ] [ -o [o] ] [ -p s ] [ +|-r [t] ] [ -S
       [t] ] [ -T [t] ] [ -u s ] [ +|-w ] [ -x [fl] ] [ -z [z] ] [ -Z [Z] ]  [
       -- ] [names]

See [names] - this means, lsof /tmp/bob would show all the processes which have /tmp/bob open. Your method of lsof | grep /tmp/bob is rubbish.
@bilco105

LOL!! i will have to say you dont READ

lsof | grep /tmp/bob  : this is not my comments, its another experts comments!!!

Please Read all the comments then give your comments

Read the  Man page of lsof (( which is your advise) does not give the solution to the author


lsof | grep filename is working. It works with egrap, too.

I opened 5 xls files on the server and all of them got the same (10951) process number.
That means if I kill the process number all 5 xls file will be closed. Tha doesn't sound good for me.
If the user edits the 1st file and then I kill the 5th file, then the 1st will be killed, too since they have the same  process number.
 

can you show me the output of

lsof | grep filename

let me see what is showing
@fosiul01

You just said thats not your command, then you've given him the exact same command to run.

WHY would you want to read your entire open file handle table and grep it, when you can ask the kernel to tell you instantly what process has a paticular file open???

lsof <filename>
@fosiul01

Also, I wasn't directing the author to read the man page - I was directing you.
@bilco105 thanks for advising me. i will remember next time.
@bilco105, by the way, lsof <filename>  does not work,

also the solution you provide in comments :ID:24362656

all those i said in my very first comments, ( please Read ) again i will suggest, to comments any thing here, Please read previous comments . good luck .
and again Thanks for Advise me to read man page, i will remember that
rob@sonic:~$ lsof /tmp
COMMAND    PID USER   FD   TYPE DEVICE SIZE NODE NAME
seahorse- 3478  rob  cwd    DIR    8,1 4096   34 /tmp
rob@sonic:~$

Actually - it does.

I'm not going to continue argueing with you. If you want to continue giving poor advise, rather than consulting the man pages and understanding how a command actually works - then I can't help you.

@bilco105 : another advise, please  dont Critisize another Experts here.

if you have any solution for the author write it here, if not then dont post. all the post you submit here, tell me what actually helping the author to solve his solution other then crisize people comments ??

BTW : we are here trying to help other people and at the same time to gather some knowledge, if you have anything to share then share, if not then... i would not comments

thanks
Well,
one of this question's tags is "Samba File Server"
Did you consider using 'smbstatus' to find open files?
Won't help with closing individual files, though.
wmp
 
@fosiul01

I wasn't criticizing initially. I told you in a friendly way that your advise was inefficient, and told you the proper way to do it. You then began to criticize my advice, to which I proved you were wrong.

The author asked for a way to find out which processes have a paticular file open. I have given the answer atleast 5 times now..;

lsof /path/to/file

See snippet for a working example


rob@sonic:~$ lsof /home/rob/
COMMAND     PID USER   FD   TYPE DEVICE SIZE NODE NAME
x-session  3266  rob  cwd    DIR    8,1 4096  423 /home/rob/
compiz     3502  rob  cwd    DIR    8,1 4096  423 /home/rob/
compiz.re  3557  rob  cwd    DIR    8,1 4096  423 /home/rob/
gnome-pan  3558  rob  cwd    DIR    8,1 4096  423 /home/rob/
nautilus   3559  rob  cwd    DIR    8,1 4096  423 /home/rob/
evolution  3565  rob  cwd    DIR    8,1 4096  423 /home/rob/
gnome-do   3568  rob  cwd    DIR    8,1 4096  423 /home/rob/
python     3571  rob  cwd    DIR    8,1 4096  423 /home/rob/
update-no  3574  rob  cwd    DIR    8,1 4096  423 /home/rob/
nm-applet  3581  rob  cwd    DIR    8,1 4096  423 /home/rob/
gnome-do   3582  rob  cwd    DIR    8,1 4096  423 /home/rob/
sh         3607  rob  cwd    DIR    8,1 4096  423 /home/rob/
compiz-de  3608  rob  cwd    DIR    8,1 4096  423 /home/rob/
gtk-windo  3610  rob  cwd    DIR    8,1 4096  423 /home/rob/
pidgin     4006  rob  cwd    DIR    8,1 4096  423 /home/rob/
rhythmbox  9811  rob  cwd    DIR    8,1 4096  423 /home/rob/
firefox   11834  rob  cwd    DIR    8,1 4096  423 /home/rob/
gnome-ter 12223  rob  cwd    DIR    8,1 4096  423 /home/rob/
bash      12225  rob  cwd    DIR    8,1 4096  423 /home/rob/
lsof      12244  rob  cwd    DIR    8,1 4096  423 /home/rob/
lsof      12245  rob  cwd    DIR    8,1 4096  423 /home/rob/
rob@sonic:~$

Open in new window

well : the author asked, someone said, example fosiul2.xls file is still open so he needs to close this file ..


lsof | grep fosiul2.xls

[root@cd4070nw ~]# lsof | grep fosiul2.xls
scalc.bin 22757    fosiul   38uW     REG      253,0    97280    3866948 /home/fosiul/fosiul2.xls


lsof /home/fosiul


[root@cd4070nw ~]# lsof /home/fosiul
COMMAND     PID   USER   FD   TYPE DEVICE SIZE    NODE NAME
gnome-ses  5795 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
gnome-key  5863 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
metacity   5879 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
gnome-pan  5884 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
nautilus   5886 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
eggcups    5893 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
bt-applet  5904 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
puplet     5906 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
nm-applet  5908 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
pam-panel  5915 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
gam_serve  5924 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
gnome-ter  6240 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
gnome-pty  6242 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
bash       6243 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
soffice   22747 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul
scalc.bin 22757 fosiul  cwd    DIR  253,0 4096 3866626 /home/fosiul




1 .so if you use grep you can minimize the output  from more then 10 lines to only 1 lines

2. if you dont know where is fosiul2.xls file reside, it could be in /var/www/fosiul2.xls then how absolute path ( lsof /home/rob) is benefiting you ??

3. by comparing 2 output lsof | grep fosiul2.xls and lsof /home/rob, i can see there is a similiaritity of 22757 which is hold by scalc.bin process

but only lsof /home/rob, is that tellign you anything about whicih process is hoding fosiul2.xls ??  I guess No

here is my point of using grep


[ but still it does not solved author's problme beause as he said, he can see same Id is holding for different xls file. ]

thats why i asked author , to show me output of lsof | grep fosiul2.xls command when its locked

currently any xls or doc file is opended in linux , its using by same process Id by scalc.bin

Please see the attached file.
lsof-command.doc
@author:

last picture of your attached document

i saw your home directory is /usr2/home/akoc

but when you are running like this losf /home/akoc

so its unable to find the home directory because you are not saying lsof /usr2/home/akoc


about your first picture, all those files is running by smbd  process which is normal ..

now let me ask you

when some one said that any file is locked,
any locking problme come up with some sort of proces id or it will say what file is lock

suppose example, i am trying to edit a file call file.txt via vim, if its locked , Os will say, file.txt is used by some and some process or its locaked by file.swap ... some thing liek that

what process number or what else infomraiton you get when file is locked ??
No error message if I enter 'lsof /usr2/home/atkoc' (Please see attached file)

To your question:
It always happens that User1 comes to me because he gets a message when trying to open an .xls file.
The message is very common. " The file is open by User2 .... You can open it Read-only...."

Then I go the User2 and see that he already closed the file. Nobody, neither me, can open the file because the system doesn't recognize that it was closed.

In Windows I'd go to the server My Computer - Manage - Opened Files where I'd see the file, by whom is it opened and I'd close it.
But in Linux it's very complicated for me.

lsof-command2.doc
ommm

you ,

by looking the output of belstel.xls and  ksab.doc , its not locked

because if a file is locked you will see there is an Extra W sign  , your one is 19uW and 13rW, if its locked there should be 2 w

you sure those files are locked ???



another thing, the Error you seeing, are those from windows client or from linux client ???
as i can see its samba file locking

is there any way you can add this in smb.conf under file share option to see if , its help to locking problem


http://www.samba.org/samba/docs/man/Samba-HOWTO-Collection/locking.html

Read Disabling Oplocks section

  oplocks = False
        level2 oplocks = False
You're right, those files in the attachment aren't locked. I simple copied it to show that the command works.

Regarding the error message:
The user sees it as a Windows message.
The same as MS Outlook. It runs on a Linux server but the users get error messages like in Windows.

ASKER CERTIFIED SOLUTION
Avatar of fosiul01
fosiul01
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I will check it with lsof the next time the problem occurs.

I close this question now and thanks for your suggestions.