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.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

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.


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:



2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.




Commands using ps from sorted by
Terminal - Commands using ps - 206 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.

adb shell ps | grep my.app.packagename | awk '{print $2}' | xargs -I ? sh -c "adb logcat -v time | grep ?"
adb shell ps | grep <process name> | awk '{print $2}' | xargs adb shell kill
2012-03-03 01:03:39
Functions: awk grep ps xargs

This is great when you need to reboot the system-server, or your own daemon that has gone crazy

for i in $(ps x | grep chrome | cut -d"?" -f1 | grep -v chrome); do kill -9 $i ; done
2012-03-01 03:41:39
Functions: cut grep kill ps

This one liner is to kill all google chrome tabs. This works similar to $ killall firefox command which is to kill all firefox processes.

kill -9 $(ps -ef | grep [h]ttpd | awk '{print $2}')
PID=httpd ; ps aux | grep $PID | grep -v grep | awk '{print $2}' | xargs kill -9
2012-02-21 23:27:47
User: esaenz
Functions: awk grep kill ps xargs

# define user pid to kill

PID=httpd ;

# kill all pids

ps aux | grep $PID | grep -v grep | awk '{print $2}' | xargs kill -9

for i in $(ps -eo pid,pmem,pcpu| sort -k 3 -r|grep -v PID|head -10|awk '{print $1}');do diff -yw <(pidstat -p $i|grep -v Linux) <(ps -o euser,pri,psr,pmem,stat -p $i|tail);done
2012-02-16 20:54:32
Functions: awk diff grep head ps sort

It grabs the PID's top resource users with $(ps -eo pid,pmem,pcpu| sort -k 3 -r|grep -v PID|head -10)

The sort -k is sorting by the third field which would be CPU. Change this to 2 and it will sort accordingly.

The rest of the command is just using diff to display the output of 2 commands side-by-side (-y flag) I chose some good ones for ps.

pidstat comes with the sysstat package(sar, mpstat, iostat, pidstat) so if you don't have it, you should.

I might should take off the timestamp... :|

ps -p $$
2012-02-09 17:27:45
Functions: ps

works as well as echo $0, but also prints process id, which pts you're using. echo $SHELL doesn't always get updated when changing shells, so this is a better solution than that. Just one more variation on a theme.

ps -fea | grep PATTERN | awk {'print $2'} | xargs kill -9
ps -eo rss,vsz,pid,cputime,cmd --width 100 --sort rss,vsz | tail --lines 10
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 -ef --sort=-%cpu
ps ewwo command PID | tr ' ' '\n' | grep \=
ps aux | sort -nk 6
ps aux | sort --key=11 | uniq -c -d --skip-fields=10 | sort -nr --key=1,1
2011-07-19 07:11:29
User: aikikode
Functions: ps sort uniq

This command will allow to search for duplicate processes and sort them by their run count. Note that if there are same processes run by different users you'll see only one user in the result line, so you'll need to do:

ps aux | grep <process>

to see all users that run this command.

ps aux | awk {'sum+=$3;print sum'} | tail -n 1
REALUSERID=`TTYTEST=$(ps | awk '{print $2}' |tail -1); ps -ef |grep "$TTYTEST$" |awk '{print $1}'`;echo $REALUSERID
2011-07-09 16:27:21
User: khyron320
Functions: awk echo grep ps tail
Tags: sudo

Find the USERid of a SUDOed user who has either left their terminal logged in or for scripting purposes to track who ran what commands.

This only applys to users that do sudo su - USERNAME. not sudo su USERNAME

Sudo su without the dash allows use of (echo $SUDO_USER)

ps -eo pid,args | grep -v grep | grep catalina | awk '{print $1}'
ps -fu userid | awk '/userid/{print $2}' | xargs kill
2011-06-16 12:20:19
User: unxscorob
Functions: awk ps xargs
Tags: awk

to be executed from root. this works well on most commercial unix systems, have not tried on linux systems.

ps -u `/usr/xpg4/bin/id -u`
who;ps aux|grep ssh
ps -eo stat= | sort | uniq -c | sort -n
2011-03-25 13:01:42
User: fossilet
Functions: ps sort uniq

Use ps instead of top. But do not use BSD options at all, they are confusing.

Use "s=" or "state=" to show consice process statuses.