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 cut from sorted by
Terminal - Commands using cut - 457 results
for a in $(date +"%H%M"|cut -b1,2,3,4 --output-delimiter=" ");do case "$a" in 0)echo "....";;1)echo "...*";;2)echo "..*.";;3)echo "..**";;4)echo ".*..";;5)echo ".*.*";;6)echo ".**.";;7)echo ".***";;8)echo "*...";;9)echo "*..*";;esac;done
server=8.8.8.8; host="apple.com"; queries=128; for i in `seq $queries`; do let x+=`dig @${server} $host | grep "Query time" | cut -f 4 -d " "`; done && echo "scale=3;($x/${queries})" | bc
dpkg -l | grep ^rc | cut -d' ' -f3 | xargs dpkg -P
svn add `svn status | grep ? | cut -c9-80`
slow2() { ionice -c3 renice -n 20 $(pstree `pidof $1` -p -a -u -A|gawk 'BEGIN{FS=","}{print $2}'|cut -f1 -d " ") ; }
deadlib() { lsof | grep 'DEL.*lib' | cut -f 1 -d ' ' | sort -u; }
2010-11-17 12:53:30
User: Naib
Functions: cut grep sort
6

emerge,apt-get,yum... all update your system. This will at some point replace either a runtime dependency or a process (which is still running).

This oneliner will list what processes need to be restarted

ps ax -L -o pid,tid,psr,pcpu,args | sort -nr -k4| head -15 | cut -c 1-90
echo `lcg-infosites --vo lhcb ce | cut -f 1| grep [[:digit:]]| tr '\n' '+' |sed -e 's/\ //g' -e 's/+$//'`|bc -l
2010-11-10 15:06:00
User: kbat
Functions: bc cut echo grep sed tr
-2

Of course, this command must be executed at a GRID User Interface

lhcb - name of your VO, substitute it with the one you are interested it.

find / -type f -size +100M -exec du {} \; | sort -n | tail -10 | cut -f 2
find / -type f 2>/dev/null | xargs du 2>/dev/null | sort -n | tail -n 10 | cut -f 2 | xargs -n 1 du -h
2010-11-09 13:45:11
User: mxc
Functions: cut du find sort tail xargs
Tags: disk usage
1

Often you need to find the files that are taking up the most disk space in order to free up space asap. This script can be run on the enitre filesystem as root or on a home directory to find the largest files.

let NOW=`date +%s`/86400 ; PASS_LAST_CHANGE=`grep $USER /etc/shadow | cut -d: -f3` ; PASS_LIFE=`grep $USER /etc/shadow | cut -d: -f5`; DAYS_LEFT=$(( PASS_LAST_CHANGE + PASS_LIFE - NOW)) ; echo $DAYS_LEFT
2010-11-05 23:03:48
User: EBAH
Functions: cut echo
0

This works only with GNU date.

In solaris the command:

date +%s

doesn't work.

You can try using the following instead:

nawk 'BEGIN {print srand()}'

should give the same output as date +%s under Solaris.

for i in $(file * | grep broken | cut -d : -f 1); do rm $i; done
curl -s -O http://s3.amazonaws.com/alexa-static/top-1m.csv.zip ; unzip -q -o top-1m.csv.zip top-1m.csv ; head -1000 top-1m.csv | cut -d, -f2 | cut -d/ -f1 > topsites.txt
2010-11-01 01:25:53
User: chrismccoy
Functions: cut head
Tags: curl unzip cut
-4

this will dump a list of domains one per line into a text file

for lib in `readelf -d /usr/bin/abiword | grep NEEDED | cut -f2 -d[ | cut -f1 -d]`; do [ -e /usr/lib/$lib ] && j=/usr/lib/$lib || j=`locate -l 1 $lib`; readlink -f $j ; done
host foo.com|grep " has address "|cut -d" " -f4
2010-10-29 17:01:37
User: dinomite
Functions: cut grep host
-1

Get just the IP address for a given hostname. For best results, make this a function in your shell rc file so that it can be used for things like traceroute:

Titus:~$ traceroute `getip foo.com`

traceroute to 64.94.125.138 (64.94.125.138), 64 hops max, 52 byte packets

grep xxxx messages | cut -d ' ' -f 1,2,3
2010-10-22 00:01:00
User: sumudu
Functions: cut grep
0

first greps syslog for certain events (grep xxxx messages) then displays the 1st 2nd and 3rd fields (-f 1,2,3) seperated by space(-d ' ')

cut -f5 --complement
2010-10-21 20:21:07
User: alperyilmaz
Functions: cut
Tags: cut
22

Show all columns except 5th. This might help you save some typing if you are trying to exclude some columns from the output.

hg diff -r$((`hg -q par | cut -d":" -f1`-1))
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
2

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;

done

}

for p in `grep -v deinstall /var/mobile/Library/Preferences/aptbackup_dpkg-packages.txt | cut --fields=1`; do apt-get -y --force-yes install $p; done
2010-10-11 22:41:55
User: darkfader
Functions: cut install
1

Use when aptbackup will not start or you just want to see what's going on.

links2 -dump http://checkip.dyndns.com | cut -d ' ' -f7
du --max-depth=1|sort -n|cut -f2|tr '\n' '\0'|xargs -0 du -sh 2>/dev/null
svn log -q | grep -v "^-" | cut -d "|" -f 2 | sort -u
grep -i "aborted login" /var/log/maillog | awk 'BEGIN{FS="="}{print substr($4,8)}' | cut -d"," -f1
2010-09-29 21:05:31
User: cicatriz
Functions: awk cut grep
0

Prints a list of ip that tried to login on SMTP/IMAP/POP3/etc.

for net in $(ip route show | cut -f1 -d\ | grep -v default); do ipcalc $net | grep Broadcast | cut -d\ -f 2; done
2010-09-28 13:21:31
User: svg
Functions: cut grep route
0

Can be used in a firewall script, e.g. to avoid logging broadcast packtes.