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 07-01-2013, 07:19 AM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Rep: Reputation: 34
Find open files in the system


Hi, I know this is a dumb question

I am trying to find the number of open files per process, and I found few commands but each gives deferrent values. below are the commands

ls /proc/<pid>/fd/*

and

lsof -p <PID>

Can some one tell me what I am doing wrong or am I reading the out put wrong

Thanks and Best Regards
 
Old 07-01-2013, 08:23 AM   #2
eahmedshendy
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Rep: Reputation: Disabled
Hi procfs,

You want to execute line to output the number of open files per process like this
Code:
4 yum
6 http
1 vim
.
.
Or like this
Code:
[root@router scripts]# ps aux | grep -i yum | wc -l
2
[root@router scripts]# ps aux | grep -i vim | wc -l
4

Last edited by eahmedshendy; 07-01-2013 at 08:25 AM.
 
Old 07-01-2013, 10:29 AM   #3
linuxzilla.com
LQ Newbie
 
Registered: May 2013
Distribution: CentOS, Ubuntu, Debian
Posts: 22

Rep: Reputation: Disabled
Quote:
Originally Posted by eahmedshendy View Post
Hi procfs,

You want to execute line to output the number of open files per process like this
Code:
4 yum
6 http
1 vim
.
.
Or like this
Code:
[root@router scripts]# ps aux | grep -i yum | wc -l
2
[root@router scripts]# ps aux | grep -i vim | wc -l
4
Wawoo, thanks, I didn't know that before & I'll definitely save these commands with me. I think that command ps aux | grep -i vim | wc -l will bring more cooler results.
 
Old 07-02-2013, 01:42 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Quote:
Originally Posted by procfs View Post
Hi, I know this is a dumb question
I am trying to find the number of open files per process
Definitely not a dumb question!


Quote:
Originally Posted by eahmedshendy View Post
Code:
[root@router scripts]# ps aux | grep -i yum | wc -l
Sorry, that's nice but it's wrong. Actually the OP already gave the clue:
Code:
_lsof() { [ $# -eq 1 ] && lsof -Pwln -a -d '^cwd' -a -p $(pgrep -d, $1) -Fi|sort -u|wc -l || echo "${FUNCNAME}: wrong."; }
Here, let me clarify:
Code:
 ~]# cmd1() { \ps aux|grep -i httpd|wc -l; }
 ~]# time cmd1
7

real    0m0.026s
user    0m0.015s
sys     0m0.016s

 ~]# cmd2() { lsof -Pwln -a -d '^cwd' -a -p $(pgrep -d, httpd) -Fi|sort -u|wc -l; }
 ~]# time cmd2
79

real    0m0.082s
user    0m0.016s
sys     0m0.072s
 
Old 07-04-2013, 06:20 AM   #5
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Original Poster
Rep: Reputation: 34
Hi Thank you Guys for the reply, but I am bit confused about the command that unSpawn has mentioned

Kind Regards
 
Old 07-04-2013, 07:46 AM   #6
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 502

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by procfs View Post
Hi, I know this is a dumb question
I am trying to find the number of open files per process, and I found few commands but each gives deferrent values. below are the commands
ls /proc/<pid>/fd/*
and
lsof -p <PID>
Can some one tell me what I am doing wrong or am I reading the out put wrong
Thanks and Best Regards
I think its a valid question. I always used lsof whenever I was unable to unmount file system, so that I could see what files are still used by that file system. I never used /proc/<pid>/fd.

So it seems, both lsof and /proc/<pid>/fd bring different results for same pid.

I did little search in internet and it seems the difference between output from lsof and /proc/<pid>/fd/ are because lsof also displays other file descriptors and memory mapped .so files, which are mapped at /proc/<pid>/maps.

Code:
$ pidof firefox
3093

$ lsof -p 3093
COMMAND  PID  USER   FD   TYPE             DEVICE SIZE/OFF     NODE NAME
firefox 3093 madhu  cwd    DIR              253,3     4096  2883585 /home/madhu
firefox 3093 madhu  rtd    DIR              253,1     4096        2 /
firefox 3093 madhu  txt    REG              253,1    94072   661414 /usr/lib64/firefox/firefox
firefox 3093 madhu  mem    REG              253,1   156872   661248 /lib64/ld-2.12.so
firefox 3093 madhu  mem    REG              253,1    22536   661252 /lib64/libdl-2.12.so
firefox 3093 madhu  mem    REG              253,1  1922152   661249 /lib64/libc-2.12.so
firefox 3093 madhu  mem    REG              253,1   145720   661250 /lib64/libpthread-2.12.so
firefox 3093 madhu  mem    REG              253,1   598680   661283 /lib64/libm-2.12.so
firefox 3093 madhu  mem    REG              253,1    47064   661251 /lib64/librt-2.12.so
<TRUNCATED>

$ lsof -p 3093 | grep -v '^COMMAND' | wc -l
303

$ ls -l /proc/3093/fd/*
lr-x------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/0 -> /dev/null
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/1 -> /home/madhu/.xsession-errors
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/10 -> pipe:[20623]
lrwx------. 1 madhu madhu 64 Jul  4 16:37 /proc/3093/fd/101 -> /home/madhu/.mozilla/firefox/yu5ujt41.default/downloads.sqlite
lrwx------. 1 madhu madhu 64 Jul  4 16:37 /proc/3093/fd/102 -> socket:[42045]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/11 -> socket:[20624]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/12 -> /home/madhu/.mozilla/firefox/yu5ujt41.default/.parentlock
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/13 -> anon_inode:[eventpoll]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/14 -> socket:[20630]
lrwx------. 1 madhu madhu 64 Jul  4 11:17 /proc/3093/fd/15 -> socket:[20631]
<TRUNCATED>

$ ls -l /proc/3093/fd/* | wc -l
79
So, as you can see lsof counts to 303, while /proc//fd counts to 79

Here is the list of File Descriptors
Quote:
cwd - current working directory
Lnn - library references (AIX)
err - FD information error (see NAME column)
jld - jail directory (FreeBSD)
ltx - shared library text (code and data)
Mxx - hex memory-mapped type number xx
m86 - DOS Merge mapped file
mem - memory-mapped file
mmap - memory-mapped device
pd - parent directory
rtd - root directory
tr - kernel trace file (OpenBSD)
txt - program text (code and data)
v86 - VP/ix mapped file
I put the File Descriptors in a file, and when used with grep, both are almost same now.
Code:
$ cat fds
cwd
Lnn
err
jld
ltx
Mxx
m86
mem
mmap
pd
rtd
tr
txt
v86

$ lsof -p 3093 | grep -v '^COMMAND' | grep -ivf fds | wc -l
84

$ ls -l /proc/3093/fd/* | wc -l
79
Almost Equal!!!

Last edited by mddnix; 07-04-2013 at 08:15 AM.
 
Old 07-05-2013, 02:46 PM   #7
justjustin
LQ Newbie
 
Registered: Jul 2013
Posts: 3

Rep: Reputation: Disabled
This is some great info! Never knew pidof existed.
 
Old 07-09-2013, 05:27 AM   #8
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 590

Original Poster
Rep: Reputation: 34
Hi this is great thank you mddesai, and for all for replying

Best Regards
 
  


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
Where can i find the open office files? vitalstrike82 Slackware 7 04-08-2009 10:49 AM
How can find out what ports are open in my system linuxlainen Linux - Security 10 08-03-2006 05:05 PM
how to find files on the linux system? arcow Linux - Software 1 02-07-2006 03:29 AM
Can I find links in the system that point to certain files throughout the system? HGeneAnthony Linux - Newbie 3 02-18-2005 09:28 AM
Can find files on my system! pmorkert Linux - Software 2 07-30-2003 12:40 PM


All times are GMT -5. The time now is 06:39 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration