• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 81
  • Last Modified:

in Linux, need one SHELL Script for How many files currently open

in Linux, need one SHELL Script for How many files currently open
1
Durga Charan Ojha
Asked:
Durga Charan Ojha
5 Solutions
 
Durga Charan OjhaSr. System AdministratorAuthor Commented:
any help please
0
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
you'll need the LSOF command for this I guess

https://en.wikipedia.org/wiki/Lsof
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
lsof | grep REG | wc -l
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Durga Charan OjhaSr. System AdministratorAuthor Commented:
yes its LSOF command .. but I need to create a script file .. by running it will write the output in a log file with the currently opened files in Linux
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Put this in a script:

 echo "$(date) Open files: " $(lsof | grep REG | wc -l) >> output.txt

Every time you call the script it will add a line like this to the file output.txt:

Mon Dec  4 11:38:54 CET 2017 Open files:  38025
0
 
simon3270Commented:
Do you mean in the entire machine? It's a bit of a loose question, since in Linux *everything* is a file!

Your best bet is probably the "lsof" command. This lists all of the open files, open directories, memory access, pipes, network connections etc, etc (I told you everything was a file!).

Run this as root, to get all connections (some are only readable by root). A typical output
tail      6192          sar  mem       REG              253,0 113049440     654255 /usr/lib/locale/locale-archive
tail      6192          sar  mem       REG              253,0   2254296     659732 /usr/lib64/libc-2.26.so
tail      6192          sar  mem       REG              253,0    190528     661556 /usr/lib64/ld-2.26.so
tail      6192          sar    0u      CHR              136,1       0t0          4 /dev/pts/1
tail      6192          sar    1u      CHR              136,1       0t0          4 /dev/pts/1
tail      6192          sar    2u      CHR              136,1       0t0          4 /dev/pts/1
tail      6192          sar    3r      REG              253,0       158    1962716 /etc/hosts

Open in new window


As far as I can tell, all normal files are listed as REG in the 5th column, while CHR is for input/output pipes (not sure!), and FIFO for pipes.

So, to get the number of regular files open, try:
sudo lsof | awk '{print $5}' | grep -c REG

Open in new window


Edit: TFW you spend ages crafting a response, get called away to a meeting, come back and finish the response then find that you're a bit late to the party :-(
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Durga Charan Ojha - why did you use 'Request Attention'? You can just ask about that number 38025 here by posting a comment.

The output of this simple script I suggested is showing date/time + text + number of files. If you just want the number then do this:

 echo $(lsof | grep REG | wc -l) >> output.txt
1
 
Durga Charan OjhaSr. System AdministratorAuthor Commented:
Hi Gerwin Jansen, EE MVE
Do you mean 38025  is the number of file opened in linux ?
Let say I have firefox, chrome and one libre office file opened .. is there any command to check this from ssh
so that output will show only for the opened applications
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>> Do you mean 38025  is the number of file opened in linux ?
Yes.

>> is there any command to check this from ssh
Do you mean that you open these 3 applications on a computer that is running Linux? If so then you don't need ssh to check, just use a terminal. The command is the same (lsof).

If  you just want to see files opened by firefox, chrome and one libre office then you have to filter the output. Is this what you want?

You can use -c parameter to filter applications, for example:

lsof -c firefox -c chrome | grep REG | wc -l

(this would shown amount of open files for application names starting with firefox and chrome)
0
 
Durga Charan OjhaSr. System AdministratorAuthor Commented:
I tried only lsof - the output keeps on running .. i just closed the terminal
as said I need only output like as i know in the machine I have just opened chrome, firefox, editor
so it should show output .
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>> I tried only lsof
Of course it does, you should just wait patiently (it will stop after some time) or apply a filter like "-c firefox"

Just try for firefox first:

lsof -c firefox | grep REG | wc -l

should give you just the number.

lsof -c firefox | grep REG

this should show you the files
0
 
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Gerwin answered your original question...

lsof | grep REG | wc -l

Open in new window


To gain better granularity of files returned, just stack up additional grep commands.

lsof output (with no scoping, as in using grep commands) can run for a long time...

For example here's the output from one of my servers hosting 100s of client sites...

net12 # time lsof 2>/dev/null | egrep " REG " | wc -l
412880

real	0m13.124s
user	0m7.000s
sys	0m6.300s

Open in new window


As you can see 13 seconds to run + 412880 files returned.

If you're running on a slow machine + swapping is occurring, lsof can take minutes to return a complete run.

You'll have to craft your lsof + grep pipeline accordingly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now