What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags



Commands tagged ps from sorted by
Terminal - Commands tagged ps - 64 results
ps h --ppid $(cat /var/run/apache2.pid) | awk '{print"-p " $1}' | xargs sudo strace
2012-03-21 01:59:41
Functions: awk cat ps sudo xargs

Like the original version except it does not include the parent apache process or the grep process and adds "sudo" so it can be run by user.

exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files
2012-02-01 10:54:03
User: mhs
Functions: exec

`your_cmd -erase_all_files` is the real process, but harmless-looking getty appears in the process table.

Never actually had a need to do this, but interesting nonetheless... Tested in bash, dash.

-a $NAME

"pass NAME as the zeroth argument to COMMAND", i.e. customise the name of the process (as commonly seen with `ps`)

ps -fea | grep PATTERN | awk {'print $2'} | xargs kill -9
ps -ef | grep [j]ava | awk -F ' ' ' { print $1," ",$2,"\t",$(NF-2),"\t",$(NF-1),"\t",$NF } ' | sort -k4
2012-01-05 16:05:48
User: drockney
Functions: awk grep ps sort
Tags: sort awk grep ps

Tested in bash on AIX & Linux, used for WAS versions 6.0 & up. Sorts by node name.

Useful when you have vertically-stacked instances of WAS/Portal. Cuts out all the classpath/optional parameter clutter that makes a simple "ps -ef | grep java" so difficult to sort through.

sudo ps aux --sort:rss | awk '{print $2"\t"$11": "$6/1024" MB"}' | column -t | less
ps -p pid -o logname |tail -1
ps aux | grep PID | grep -v 'grep' | awk '{ print $1 }'
2011-11-05 04:53:29
User: nssy
Functions: awk grep ps
Tags: bash awk grep ps

Gets the current system user running a process with the specified pid

ps ewwo command PID | tr ' ' '\n' | grep \=
ps aux | sort -nk 6
ps aux | awk {'sum+=$3;print sum'} | tail -n 1
alias cps="ps -u root U `whoami` --forest -o pid,stat,tty,user,command |ccze -m ansi"
ps -C apache o pid= | sed 's/^/-p /' | xargs strace
ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace
2011-03-14 21:45:22
User: px
Functions: awk grep ps xargs

This one-liner will use strace to attach to all of the currently running apache processes output and piped from the initial "ps auxw" command into some awk.

ps -ef | grep c\\ommand
ps -o user,%cpu,%mem,command
2010-12-08 10:35:25
Functions: ps
Tags: ps

Using ps options rather than filtering.

cd /proc&&ps a -opid=|xargs -I+ sh -c '[[ $PPID -ne + ]]&&echo -e "\n[+]"&&tr -s "\000" " "<+/cmdline&&echo&&tr -s "\000\033" "\nE"<+/environ|sort'

Grabs the cmdline used to execute the process, and the environment that the process is being run under. This is much different than the 'env' command, which only lists the environment for the shell. This is very useful (to me at least) to debug various processes on my server. For example, this lets me see the environment that my apache, mysqld, bind, and other server processes have.

Here's a function I use:

aa_ps_all () { ( cd /proc && command ps -A -opid= | xargs -I'{}' sh -c 'test $PPID -ne {}&&test -r {}/cmdline&&echo -e "\n[{}]"&&tr -s "\000" " "<{}/cmdline&&echo&&tr -s "\000\033" "\nE"<{}/environ|sort&&cat {}/limits' ); }

From my .bash_profile at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

for p in `ps L|cut -d' ' -f1`;do echo -e "`tput clear;read -p$p -n1 p`";ps wwo pid:6,user:8,comm:10,$p kpid -A;done

While going through the source code for the well known ps command, I read about some interesting things.. Namely, that there are a bunch of different fields that ps can try and enumerate for you. These are fields I was not able to find in the man pages, documentation, only in the source.

Here is a longer function that goes through each of the formats recognized by the ps on your machine, executes it, and then prompts you whether you would like to add it or not. Adding it simply adds it to an array that is then printed when you ctrl-c or at the end of the function run. This lets you save your favorite ones and then see the command to put in your .bash_profile like mine at : http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

Note that I had to do the exec method below in order to pause with read.

t ()


local r l a P f=/tmp/ps c='command ps wwo pid:6,user:8,vsize:8,comm:20' IFS=' ';

trap 'exec 66

exec 66 $f && command ps L | tr -s ' ' >&$f;

while read -u66 l >&/dev/null; do

a=${l/% */};

$c,$a k -${a//%/} -A;

yn "Add $a" && P[$SECONDS]=$a;



pgrep -c cat
ps -axgu | cut -f1 -d' ' | sort -u
ps -eo user | sort -u
2010-07-07 12:28:44
User: dfaulkner
Functions: ps sort

Shows a list of users that currently running processes are executing as.

YMMV regarding ps and it's many variants. For example, you might need:

ps -axgu | cut -f1 -d' ' | sort -u
pkill -f <process name>
2010-06-19 02:36:31
User: eikenberry
Tags: kill ps killall

Using -f treats the process name as a pattern so you don't have to include the full path in the command. Thus 'pkill -f firefox' works, even with iceweasel.

wait $!
2010-06-07 21:56:36
User: noahspurrier
Functions: wait

Referring to the original post, if you are using $! then that means the process is a child of the current shell, so you can just use `wait $!`. If you are trying to wait for a process created outside of the current shell, then the loop on `kill -0 $PID` is good; although, you can't get the exit status of the process.

pgrep -cu ioggstream
command ps wwo pid,user,group,vsize:8,size:8,sz:6,rss:6,pmem:7,pcpu:7,time:7,wchan,sched=,stat,flags,comm,args k -vsz -A|sed -u '/^ *PID/d;10q'

I've wanted this for a long time, finally just sat down and came up with it. This shows you the sorted output of ps in a pretty format perfect for cron or startup scripts. You can sort by changing the k -vsz to k -pmem for example to sort by memory instead.

If you want a function, here's one from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

aa_top_ps(){ local T N=${1:-10};T=${2:-vsz}; ps wwo pid,user,group,vsize:8,size:8,sz:6,rss:6,pmem:7,pcpu:7,time:7,wchan,sched=,stat,flags,comm,args k -${T} -A|sed -u "/^ *PID/d;${N}q"; }
ps hax -o user | sort | uniq -c