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 - 207 results
echo COMMAND | xargs -ixxx ps -C xxx -o pid= | xargs -ixxx ls -l /proc/xxx/cwd
ps -ef | grep PROCESS | grep -v grep | awk '{print $2}' | xargs kill -9
2009-02-21 15:55:38
User: f4nt
Functions: awk grep kill ps xargs
4

kills all pids matching the search term of "PROCESS". Be careful what you wish for :)

ps auxwww | grep outofcontrolprocess | awk '{print $9}' | xargs kill -9
ps aux | awk '/name/ {print $2}'
2009-02-20 21:35:52
User: evil_otto
Functions: awk ps
-5

This finds a process id by name, but without the extra grep that you usually see. Remember, awk can grep too!

ps -eo stat,pid,user,command | egrep "^STAT|^D|^R"
2009-02-20 19:00:17
User: jyoder
Functions: egrep ps
4

Want to know why your load average is so high? Run this command to see what processes are on the run queue. Runnable processes have a status of "R", and commands waiting on I/O have a status of "D".

On some older versions of Linux may require -emo instead of -eo.

On Solaris: ps -aefL -o s -o user -o comm | egrep "^O|^R|COMMAND"

ps auxw |egrep "PID|process_to_look_at"
2009-02-20 00:17:43
User: tsaavik
Functions: ps
4

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1828 0.0 0.0 5396 476 ? Ss 2008 0:00 /usr/sbin/sshd

x11vnc -display :0 -auth $(ps -ef|awk '/xauth/ {print $15}'|head -1) -forever -bg &
2009-02-19 18:55:54
User: nottings
Functions: ps
4

the $15 may change for you depending on your distro, etc...

ps aux | grep -i firefox | grep -v grep | awk '{print $2}' | xargs -t -i kill -9 {}
2009-02-19 18:50:00
User: blackdude
Functions: awk grep kill ps xargs
-7

This is a nice way to kill processes.. the example here is for firefox!!! substitute firefox for whatever the process name is...

ps -o ppid= <given pid> | xargs ps -p
psg () { ps auxwww | egrep "$1|PID" | grep -v grep }
2009-02-18 23:37:35
User: mulad
Functions: egrep grep ps
1

Yet another ps grep function, but this one includes the column headings.

psg() { if [ -z "$2" ]; then psargs="aux"; greparg="$1"; else psargs="$1"; greparg="$2"; fi; ps $psargs | grep -i "$(echo $greparg | sed -e 's/^\(.\)/[\1]/')\|^$(ps $psargs | head -1)" ; }
2009-02-18 20:57:17
User: DEinspanjer
Functions: grep ps
0

My variant on this common function. Some highlights:

Allows you to override the default ps args of "aux"

Uses bracket trick to omit the grep process itself without having to use a second grep

Always prints the correct header row of ps output

Limitations: Ugly ps error output if you forget to quote your multi word grep argument

ps -C thisdaemon || { thisdaemon & }
2009-02-18 14:12:17
User: sil
Functions: ps
3

This comes in handy if you have daemons/programs that have potential issues and stop/disappear, etc., can be run in cron to ensure that a program remains up no matter what. Be advised though, if a program did core out, you'd likely want to know why (gdb) so use with caution on production machines.

ps -e
ps -ef | grep $USERNAME | awk {'print $2'} | xargs kill [-9]
2009-02-17 20:35:35
User: TheNomad
Functions: awk grep kill ps xargs
3

This is a 'killall' command equivalent where it is not available.

Prior to executing it, set the environment variable USERNAME to the username, whose processes you want to kill or replace the username with the $USERNAME on the command above.

Side effect: If any processes from other users, are running with a parameter of $USERNAME, they will be killed as well (assuming you are running this as root user)

[-9] in square brackets at the end of the command is optional and strongly suggested to be your last resort. I do not like to use it as the killed process leaves a lot of mess behind.

ps aux | grep "[s]ome_text"
2009-02-17 02:10:50
User: SiegeX
Functions: grep ps
11

The trick here is to use the brackets [ ] around any one of the characters of the grep string. This uses the fact that [?] is a character class of one letter and will be removed when parsed by the shell. This is useful when you want to parse the output of grep or use the return value in an if-statement without having its own process causing it to erroneously return TRUE.

ps -eo pcpu,pid,args | sort -n
2009-02-16 14:23:03
Functions: ps sort
3

Useful to detect which process is causing system loads. It shows process PID so as we can take further actions.

ps aux | awk '{sum+=$6} END {print sum/1024}'
xprop | awk '/PID/ {print $3}' | xargs ps h -o pid,cmd
2009-02-16 07:55:19
User: jackhab
Functions: awk ps xargs
9

This command is useful when you want to know what process is responsible for a certain GUI application and what command you need to issue to launch it in terminal.

ps gv [pid] | head -2
2009-02-12 05:04:55
User: ren
Functions: head ps
-1

sh as:

#! /bin/sh

while [ 1 -ne 6 ]; do

pid=`ps -ef | grep -v "grep" | grep "trans_gzdy" | cut -c10-17`

ps gv $pid | head -2

sleep 1

done

check changes of RSS.

ps aux | grep [h]ttpd
2009-02-10 02:59:20
User: abcde
Functions: grep ps
10

Surround the first letter of what you are grepping with square brackets and you won't have to spawn a second instance of grep -v. You could also use an alias like this (albeit with sed):

alias psgrep='ps aux | grep $(echo $1 | sed "s/^\(.\)/[\1]/g")'

kill -HUP ` ps -aef | grep -i firefox | sort -k 2 -r | sed 1d | awk ' { print $2 } ' `
pidof () { ps acx | egrep -i $@ | awk '{print $1}'; }
ps awwux|awk '{print $1}'|sort|uniq
ps aux | awk '{ print $8 " " $2 " " $11}' | grep -w Z
ps auxf
2009-02-05 18:07:16
User: systemj
Functions: ps
2

Shows a tree view of parent to child processes in the output of ps (linux). Similar output can be achieved with pstree (also linux) or ptree (Solaris).