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 cpu from sorted by
Terminal - Commands tagged cpu - 17 results
top -bn2|awk -F, '/Cpu/{if (NR>4){print 100-gensub(/.([^ ]+).*/,"\\1","g",$4)}}'
2014-04-18 17:48:05
User: ichbins
Functions: awk top
Tags: top cpu cpuinfo

This version is precise and requires one second to collect statistics. Check sample output for a more generic version and also a remote computer invocation variant. It doesn't work with the busybox version of the 'top' command but can be adjusted

prlimit --cpu=10 sort -u hugefile
2013-02-27 15:59:11
User: mhs
Functions: sort
Tags: cpu util-linux

Similar to `cpulimit`, although `prlimit` can be found shipped with recent util-linux.

Example: limit CPU consumption to 10% for a math problem which ordinarily takes up 100% CPU:


bc -l <(echo "1234123412341234^12341234")

See the difference `prlimit` makes:

prlimit --cpu=10 bc -l <(echo "1234123412341234^12341234")

To actually monitor the CPU usage, use `top`, `sar`, etc.. or:

pidstat -C 'bc' -hur -p ALL 1
NUMCPUS=`grep ^proc /proc/cpuinfo | wc -l`; FIRST=`cat /proc/stat | awk '/^cpu / {print $5}'`; sleep 1; SECOND=`cat /proc/stat | awk '/^cpu / {print $5}'`; USED=`echo 2 k 100 $SECOND $FIRST - $NUMCPUS / - p | dc`; echo ${USED}% CPU Usage
2012-10-02 03:57:51
User: toxick
Functions: awk echo sleep wc

Using the output of 'ps' to determine CPU usage is misleading, as the CPU column in 'ps' shows CPU usage per process over the entire lifetime of the process. In order to get *current* CPU usage (without scraping a top screen) you need to pull some numbers from /proc/stat. Here, we take two readings, once second apart, determine how much IDLE time was spent across all CPUs, divide by the number of CPUs, and then subtract from 100 to get non-idle time.

sudo cpulimit -p pid -l 50
2011-11-21 08:43:57
User: Dhinesh
Functions: sudo
Tags: cpu

This will limit the average amount of CPU it consumes.

ps -ef --sort=-%cpu
ps aux | awk {'sum+=$3;print sum'} | tail -n 1
while :; do sensors|grep ^Core|while read x; do printf '% .23s\n' "$x"; done; sleep 1 && clear; done;
2011-04-20 06:41:57
Functions: grep printf read sleep

Watch the temperatures of your CPU cores in real time at the command line. Press CONTROL+C to end.

GORY DETAILS: Your computer needs to support sensors (many laptops, for example, do not). You'll need to install the lm-sensors package if it isn't already installed. And it helps to run the `sensors-detect` command to set up your sensor kernel modules first. At the very end of the sensors-detect interactive shell prompt, answer YES to add the new lines to the list of kernel modules loaded at boot.

taskset 0x00000001 yes > /dev/null &
2011-04-03 07:23:53
User: kerim
Functions: taskset yes

For each cpu set mask and then monitor your cpu infos. Temp,load avg. etc.

For example for 2nd cpu or 2nd core

taskset 0x00000002 yes > /dev/null &

For example for 3rd cpu or 3rd core

taskset 0x00000004 yes > /dev/null &

For example for 4th cpu or 4th core

taskset 0x00000008 yes > /dev/null &

Monitor your cpu temp with this command if you want

watch -n1 "acpi -t"

Load avg. from top command




while sleep 1; do acpi -t | osd_cat -p bottom; done &
2011-01-14 23:22:57
User: linuts
Functions: acpi sleep

No need for a colon, and one less semicolon too. Also untested.

while :; do acpi -t | osd_cat -p bottom ; sleep 1; done &
2011-01-14 13:57:45
User: John_W
Functions: acpi sleep

There is no need for variables. I also added sleep to reduce cpu usage, however I didn't test it.

slow2() { ionice -c3 renice -n 20 $(pstree `pidof $1` -p -a -u -A|gawk 'BEGIN{FS=","}{print $2}'|cut -f1 -d " ") ; }
sysctl dev.cpu.0.freq_levels
2010-09-18 11:19:46
User: bugmenot
Functions: sysctl
Tags: cpu powerd

Once you know the available frequencies for your CPU, they can be used to do things like set minimum CPU frequency for powerd so that it doesn't ramp down too slow on a server :

/etc/sysctl.conf or /boot/loader.conf:

debug.cpufreq.lowest=DESIRED FREQ HERE

or at terminal

sysctl debug.cpufreq.lowest=DESIRED FREQ HERE

echo 'some command' | batch
2010-07-14 03:08:31
User: kniht
Functions: echo

If shell escaping of the command is problematic, you can write the command to a file first:

batch <somefile

Or read it:

read -re && echo "$REPLY" | batch

Or, if your shell supports it, you can eliminate echo:

read -re && batch <<<$REPLY

("man batch" lists 1.5 for me, but I don't know how widely it differs.)

( ( while [ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] || [ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ]; do sleep 10; done; my-command > output.txt ) & )
2010-07-13 09:12:11
User: michelsberg
Functions: echo sleep

[ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] returns true if less than 2000 MB of RAM are available, so adjust this number to your needs.

[ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ] returns true if the current machine load is at least equal to the number of CPUs.

If either of the tests returns true we wait 10 seconds and check again. If both tests return false, i.e. 2GB are available and machine load falls below number of CPUs, we start our command and save it's output in a text file.

The ( ( ... ) & ) construct lets the command run in background even if we log out. See http://www.commandlinefu.com/commands/view/3115/ .

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"; }
sudo lshw -C cpu|grep width
taskset -c 0 your_command
2009-02-28 22:38:02
User: Alanceil
Functions: taskset

This is useful if you have a program which doesn't work well with multicore CPUs. With taskset you can set its CPU affinity to run on only one core.