What does the | symbol mean in sudo ps -ef | grep processname command?
Also can anyone please explain this command? I have used this command only for getting the PID and killing that process, but I also saw sudo ps -ef | grep processname | grep -v grep and I am under the impression that -v grep is like killing the previous generated PID for grep. If that's so how does it work?
4 Answers
It's called pipe. It gives the output of the first command as the input to the second command.
In your case it means:
The result of sudo ps -ef is fed as the input of grep processname
sudo ps -ef:
This lists all the processes running. Type man ps in the termial for more.
grep processname
So, this list of processes is fed into grep which just searches for the program defined by programname.
Example
Typing sudo ps -ef | grep firefox in my terminal returns:
parto 6501 3081 30 09:12 ? 01:52:11 /usr/lib/firefox/firefox parto 8295 3081 4 15:14 ? 00:00:00 /usr/bin/python3 /usr/share/unity-scopes/scope-runner-dbus.py -s web/firefoxbookmarks.scope parto 8360 8139 0 15:14 pts/24 00:00:00 grep --color=auto firefox 5ps -ef | grep processname It first runs sudo ps -ef and passes the output to the second command.
The second command filters all lines that contain word "processname".
ps -ef | grep processname | grep -v grep lists all lines containing processname and not containing grep.
According to man grep
-v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.) According to man ps
ps displays information about a selection of the active processes. -e Select all processes. Identical to -A. -f Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keyword args, and the format keyword comm. You can combine parameters -ef means same as -e -f.
Actually ps -ef | grep processname list all occurrences of the process called processname.
Im gonna try and answer this with a straight-forward practial answer:
The pipe | lets you do awesome stuff in the shell! It's the one single operator that i consider the most usefull and mighty.
how about counting files in a directory? simple:
ls | wc -l ..redirect the output of ls to wc wit parameter -l for lines
or counting of lines in a file?
cat someFile | wc -l what if i want to search for something? 'grep' can search for occurences of strings:
cat someFile | grep aRandomStringYouWantToSearchFor you just redirect the output of the command left of the Pipe to the command right of the pipe.
one more level: how often does something occure in a file?
cat someFile | grep aRandomStringYouWantToSearchFor | wc -l you can use the | for nearly everything :)
fortune | cowsay 5I have used this command only for getting the PID and killing that process
Other answers have answered your main question already, but I'd like to address this as well;
In general killing a process is often an overkill and leaves resources allocated by a process around in a messy state, you can often just terminate it;
Beside this, just use pkill to kill / terminate a process. pkill supports specifying an exact process name or a regular expression:
pkill -x foo # Terminates process foo pkill ^foo$ # Terminates process foo pkill -9 -x foo # Kills process foo pkill -9 ^foo$ # Kills process foo 6
