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

in Linux, need one SHELL Script for How many files currently open
LVL 4
Durga Charan OjhaSr. System AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Durga Charan OjhaSr. System AdministratorAuthor Commented:
any help please
0
Alex [***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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.