How do I count how many files running in a Linux directory? - linux

I have to list the files running in current directory and display the count of those listed
files .
[root#xxxx ~]# ps -eaf | grep perl
root 16278 16196 48 10:38 pts/1 00:40:19 perl filename.pl
root 16379 16293 0 12:02 pts/0 00:00:00 grep perl
[root#xxxx ~]# ps -AF | grep -i "/var/www/anand/file/sample" wc -l
1
[root#xxxx ~]#
There are 2 files running in same directory "sample" i have to count the no of files the above comment doesn't work please provide any solution.

$ ls | wc -l
Or when you need only regular files:
$ ls -l | grep ^- | wc -l
When you need the number of files that were started from the directory, say /home/user,
you must use something like:
$ ps aux | grep /[h]ome/user | wc -l
Note [] characters that you can place around any letter in the name.

ps -AF | grep -i "/usr/local/" | wc -l
"/usr/local/" is the directory you intrested in

Related

Linux use grep command

I know use ps -ef | grep test| grep -v grep |wc -l can list the num of process test,and now i plan to list the test processes belong to user :forme.is this right as below :
ps -ef | grep test|grep -x forme| grep -v grep |wc -l
For a start, grep test| grep -v grep can be replaced with grep '[t]est'. See here for an explanation.
Secondly, if you want to limit the processes to a single user, that's what the -u option to ps is for:
ps -fu forme | grep '[t]est' | wc -l
And, finally, grep already has a -c option to count lines, so you can ditch the wc part of the pipeline:
ps -fu forme | grep -c '[t]est'

how to get PIDs spawn from a particular folder

I have a program that in distributed mode creates a folder and spawns a bunch of sub processes. Is there any way to find all PIDs that were executed from this folder? Sort of opposite of
$ pwdx pid
where you give a path name and you get a bunch of pids.
thanks
Reporting all processes which absolute path is inside '/usr/bin/' may be done like this:
ls -l /proc/*/exe 2>/dev/null | grep /usr/bin/ | sed 's#.*/proc/##;s#/exe.*##;' | grep -v "self"
Reporting all processes which working directory (working directory can be changed by a simple cd) is inside /tmp/a could be done like this:
ps axo pid | xargs -n1 pwdx 2>/dev/null | grep ': /tmp/a' | sed 's/:.*//'

How to get pid by unique process name in linux?

I have two java program running on server MyProgram and MyProgramTest.
ps -ef | grep -i java
root 505 17711 0 16:54 pts/4 00:00:00 grep -i MyProgram
root 16450 16448 99 16:46 pts/4 00:07:29 java MyProgram
root 16473 16471 99 16:46 pts/4 00:07:29 java MyProgramTest
I want to search there pid using below commands
ps ax | grep -v grep | grep MyProgram
It should give me PID 16450 but it is giving both
16450 pts/4 Sl 9:19 java MyProgram
16473 pts/4 Sl 9:19 java MyProgramTest
Expected Output :
16450 pts/4 Sl 9:19 java MyProgram
How to get PID by Unique Process Name in linux ?
ps ax | grep -v grep | grep -w "MyProgram"
or
ps ax | grep -v grep | grep "\MyProgram\b"
You can use,
ps ax | grep -v grep | grep -w MyProgram
-w for Whole Word Match. However, It will also match things like MyProgram or MyProgram Hello.
To avoid tricks like grep -v grep it is better to use pgrep:
pgrep --exact MyProgram
Suffix $, ie; the process name ends with "MyProgram",
$ ps -ef |grep MyProgram$

ps command in linux

I'm a newbie to unix-like. And I met a weird issue that I really cannot find answers by searching.
#!/bin/bash
me=`basename "$0"`
echo $(ps -e | grep "$me" | wc -l)
ps -e | grep "$me" | wc -l
After executing that bash script, the echo shows me 2, and ps just shows me 1 which is what I want. How can this happen? Why echo shows me an extra process?
As Charles Duffy pointed out, $() creates a subshell. That answers my question. Apparently I still have a lot to learn. Thanks for all the help.
As noted by a comment by Cyrus; this script:
me=$(basename $0)
ps -ef |grep $me
when launched with "./ps.sh", prints:
auser#pc:/tmp$ ./ps.sh
auser 4425 4422 0 08:42 pts/3 00:00:00 grep ps.sh
auser#pc:/tmp$
No subshells are involved here, it is the grep(1) itself that is listed by ps(1). The same script, launched with "bash ps.sh" outputs:
auser 4426 3946 0 08:44 pts/3 00:00:00 bash ps.sh
auser 4429 4426 0 08:44 pts/3 00:00:00 grep ps.sh
This is the result the OP gets, even without subshells. Even more explicit:
auser#pc:/tmp$ ps -ef |grep grep
auser 4467 3946 0 08:49 pts/3 00:00:00 grep grep
although you are creating a sub shell by using $() you can grep this out by using grep -v grep.
So:
$(ps -e | grep "$me" | grep -v grep | wc -l)
which will return 1 instead of 2

How many open files for each process running for a specific user in Linux

Running Apache and Jboss on Linux, sometimes my server halts unexpectedly saying that the problem was Too Many Open Files.
I know that we might set a higher limit for nproc and nofile at /etc/security/limits.conf to fix the open files problem, but I am trying to get better output, such as using watch to monitor them in real-time.
With this command line I can see how many open files per PID:
lsof -u apache | awk '{print $2}' | sort | uniq -c | sort -n
Output (Column 1 is # of open files for the user apache):
1 PID
1335 13880
1389 13897
1392 13882
If I could just add the watch command it would be enough, but the code below isn't working:
watch lsof -u apache | awk '{print $2}' | sort | uniq -c | sort -n
You should put the command insides quotes like this:
watch 'lsof -u apache | awk '\''{print $2}'\'' | sort | uniq -c | sort -n'
or you can put the command into a shell script like test.sh and then use watch.
chmod +x test.sh
watch ./test.sh
This command will tell you how many files Apache has opened:
ps -A x |grep apache | awk '{print $1}' | xargs -I '{}' ls /proc/{}/fd | wc -l
You may have to run it as root in order to access the process fd directory. This sounds like you've got a web application which isn't closing its file descriptors. I would focus my efforts on that area.

Resources