Hide

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.

Hide

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:

Hide

News

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.
Hide

Tags

Hide

Functions

Commands using ps from sorted by
Terminal - Commands using ps - 221 results
ps axo pcpu,args | awk '/[p]hp.*pool/ { sums[$4] += $1 } END { for (pool in sums) { print sums[pool], pool } }' | sort -rn | column -t
while sleep 1; do if [ $(echo "$(cat /proc/loadavg | cut -d' ' -f1) > .8 " | bc) -gt 0 ]; then echo -e "\n\a"$(date)" \e[5m"$(cat /proc/loadavg)"\e[0m"; ps aux --sort=-%cpu|head -n 5; fi; done
2014-12-08 15:44:40
User: tyzbit
Functions: cat echo head ps sleep
0

This checks the system load every second and if it's over a certain threshold (.8 in this example), it spits out the date, system loads and top 4 processes sorted by CPU.

Additionally, the \a in the first echo creates an audible bell.

( ps -U nms -o pid,nlwp,cmd:500 | sort -n -k2) && (ps h -U nms -o nlwp | paste -sd+ | bc)
ps axo pid=,stat= | awk '$2~/^Z/ { print $1 }'
2014-11-26 21:29:33
User: krizzo
Functions: awk ps
0

This prints out a list of all zombie processes PIDs so you can do things like kill the zombies

PID=$(ps -ef | grep processName | grep -v grep | awk '{print $2'}); kill -9 $PID
(ps -U nms -o pid,nlwp,cmd:500 | sort -n -k2) && (ps -U nms -o nlwp | tail -n +2 | paste -sd+ | bc)
2014-09-30 18:25:56
User: cmullican
Functions: paste ps sort tail
0

I occasionally need to see if a machine is hitting ulimit for threads, and what process is responsible. This gives me the total number, sorted low to high so the worst offender is at the end, then gives me the total number of threads, for convenience.

for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do docker ps | grep $line | awk '{printf $NF" "}' && echo $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes` / 1024 / 1024 ))MB ; done
docker ps -a | grep 'Exit' | awk '{print $1}' | xargs docker rm
$ docker rm `(docker ps -q && docker ps -qa) | sort | uniq -u`
2014-09-16 14:08:54
User: vlf
Functions: ps rm sort uniq
0

Applies 'docker rm' to all container IDs that appear in 'docker ps -a' but not in 'docker ps' - i.e. the ones that are not running.

ps -efL | grep <Process Name>
while true; do ps aux | sort -rk 3,3 | head -n 11 | cut -c -120 | netcat -l -p 8888 2>&1 >/dev/null; done &
2014-08-29 07:10:57
User: manumiu
Functions: cut head ps sort
0

If you want to see your top ten cpu using processes from the browser (e.g. you don't want to ssh into your server all the time for checking system load) you can run this command and browse to the machines ip on port 8888. For example 192.168.0.100:8888

docker ps -q | xargs -n 1 docker inspect | jq '.[0].NetworkSettings.Ports +{} | map(select(. != null)[0].HostPort) | map("-L \(.):localhost:\(.)") ' | sed -n 's/.*"\(.*\)".*/\1/p' |xargs boot2docker ssh -N
docker kill $(docker ps -q)
docker ps | sed -e 's/ /\+/g' -e 's/CONTAINER ID/CONTAINER_ID/' | tr -s '+' '\t' | q -t 'select c1,substr(c7, 0, 40),c2,c6 from -' | column -t
2014-06-06 07:20:36
User: akaivola
Functions: column ps sed tr
0

When you run a lot of containers the built in docker ps output becomes unreadable. This command formats the output to be easier on the eyes.

Requires q (https://github.com/harelba/q) text as data.

sudo kill -9 $( ps -e | grep Xorg | awk '{ print $1 }' )
2014-06-01 22:43:32
Functions: awk grep kill ps sudo
0

There are times when a X Window server hangs. When this happens, you can log in on a terminal and kill the Xorg process (i.e. the X Server). This one line command will do the trick.

ps aux | grep 'mysql' | awk '{print $6/1024 " MB";}'
ps aux | grep 'httpd' | awk '{print $6/1024;}' | awk '{avg += ($1 - avg) / NR;} END {print avg " MB";}'
ps aux | grep 'httpd' | awk '{print $6/1024 " MB";}'
ps -e -o pid,vsz,comm= | sort -n -k 2
2014-05-14 00:36:50
Functions: ps sort
-1

left-most column is PID, middle is virtual memory being consumed, right-most is actual process.

ps -efa | grep httpd | grep -v grep | awk '{ print $2 }' |xargs
ps -eo pmem,comm | grep application-name
2014-02-23 13:21:29
User: Darkstar
Functions: grep ps
0

Displays memory usage for individual instances of an application that spawns multiple instances of itself. This command also works on single process applications.

ps -eo etime,pid,pcpu,ppid,args | sed -e '/\[.\+\]/d' -e '/^[ \t]*[0-9]\{2\}:[0-9]\{2\} /d' | sort -k1r
2014-02-14 00:22:31
User: neurodrone
Functions: ps sed sort
0

If you have ever been trying to look for a list of processes based on their elapsed time you don't need to look any further.

This command lets you find the list of processes ordered in a reversed order (oldest at the top) that have been running for over an hour on your system. Any system processes are filtered out, leaving only user initiated ones in. I find it extremely useful for debugging and performance analysis.

while ps -p $PID; do sleep 1; done; script2
2014-02-12 08:07:44
User: sanjiv856
Functions: ps sleep
0

Run one script after another in such a way that second script starts after finishing first one. Without using Pipe | or ampercent && i.e. the first process is already running and you want second one to start after the first one finishes. And this can be done in different folder in case the output of second script will affect the output of first script. So run this on any folder you wish to.

Where $PID is the process id of the already running job (add PID number)

script2 is your script you wish to run after first script ends

sleep 1 is sleep for one second (SUFFIX may be ?s? for seconds (the default), ?m? for minutes, ?h? for hours or ?d? for days, read man sleep)

while [ 1 ] ;do ps aux|awk '{if ($8 ~ "D") print }'; sleep 1 ;done
CMD=chrome ; ps h -o pmem -C $CMD | awk '{sum+=$1} END {print sum}'