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 - 474 results
grep -Po '^sub (\w+)' file.pm | cut -d' ' -f2
2013-03-07 12:31:34
Functions: cut grep
0

instead of printing the whole line, print just the capture matched, but with the "cut" pipe :( I'm so sad with grep.

nmap -sP 192.168.0.* | grep Host | tr "(" ")" | cut -d\) -f2
sudo ifconfig wlan0 | grep inet | awk 'NR==1 {print $2}' | cut -c 6-
2013-02-18 14:10:07
User: mouths
Functions: awk cut grep ifconfig sudo
-1

On wired connections set 'eth0' instead of 'wlan0'

while true; do (date | tr "\n" ";") && ping -q -c 1 www.google.com|tail -1|cut -d/ -f5 ;sleep 1; done >> uptime.csv
2013-02-06 22:06:09
User: fanchok
Functions: cut date ping sleep tail tr
0

Used in OS X.

tr "\n" ";"

may be replaced by

echo ";"

with linux versions of date.

I reused

ping -q -c 1 www.google.com|tail -1|cut -d/ -f5

dpkg-query -Wf '${Package}\n' | xargs dpkg --status | sed '/^Conffiles:/,/^Description:/!d;//d' | awk '{print $2 " " $1}' | md5sum -c 2>/dev/null | grep FAILED$ | cut -f1 -d':'
2013-01-31 16:52:38
User: hallmarc
Functions: awk cut grep md5sum sed xargs
0

This functionality seems to be missing from commands like dpkg. Ideally, I want to duplicate the behavior of rpm --verify, but it seems difficult to do this in one relatively short command pipeline.

largest() { dir=${1:-"./"}; count=${2:-"10"}; echo "Getting top $count largest files in $dir"; du -sx "$dir/"* | sort -nk 1 | tail -n $count | cut -f2 | xargs -I file du -shx file; }
2013-01-21 09:45:21
User: jhyland87
Functions: cut du echo file sort tail xargs
1

You can simply run "largest", and list the top 10 files/directories in ./, or you can pass two parameters, the first being the directory, the 2nd being the limit of files to display.

Best off putting this in your bashrc or bash_profile file

FILE='mp3.list';LNNO=`wc -l $FILE|cut -d' ' -f 1`;LIST=( `cat $FILE` );for((;;)) do SEED=$((RANDOM % $LNNO));RNNO=$(python -c "print int('`openssl rand -rand ${LIST[$SEED]} 8 -hex 2>/dev/null`', 16) % $LNNO");mplayer ${LIST[$RNNO]};sleep 2s; done
apt-get --ignore-hold --allow-unauthenticated -s dist-upgrade | grep ^Inst | cut -d ' ' -f2
svn diff . -r43:HEAD --summarize | cut -c9-99999 | cpio -pvdmu ~/destination
2012-12-26 05:02:59
User: Sebasg
Functions: cpio cut diff
Tags: svn
0

Get the list of changed files between revision 43 and HEAD revision: svn diff . -r43:HEAD --summarize

Strip extra 8 characters from every line: cut -c9-99999

Copy the listed files to home/me/destination: cpio -pvdmu ~/destination

Make a plain copy (-p), list files being copied (-v), create needed directories (-d), preserve modification time (-m), overwrite unconditionally (-u)

/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
cut -c 2- < <file>
find . -type f -printf "%T@ %Tc %p\n" |sort -n |cut -d' ' -f2- |tail -n20
ifconfig eth0 | grep HW | cut -d " " -f 11
tail -1000 `ls -ltr /var/log/CF* |tail -1|awk '{print $9}'`|cut -d "," -f 17|sort|uniq -c |sort -k2
2012-11-30 16:30:41
User: raindylong
Functions: awk cut sort tail uniq
0

count & sort one field of the log files , such as nginx/apache access log files .

dig @resolver1.opendns.com myip.opendns.com | grep ^myip.opendns.com | tr '\t' : | cut -d: -f5
cd ~/.msf4/loot && cat *mscache* | cut -d '"' -f 2,4 | sed s/\"/\:/g | tr -cd '\11\12\40-\176' | grep -v Username | cut -d : -f 1,2 | awk -F':' '{print $2,$1}' | sed 's/ /:/g' > final.dcc.hash
pacman -Qi | grep 'Name\|Size\|Description' | cut -d: -f2 | paste - - - | awk -F'\t' '{ print $2, "\t", $1, "\t", $3 }' | sort -rn
2012-11-20 03:40:55
Functions: awk cut grep paste sort
0

This, like the other commands listed here, displays installed arch packages. Unlike the other ones this also displays the short description so you can see what that package does without having to go to google. It also shows the largest packages on top. You can optionally pipe this through head to display an arbitrary number of the largest packages installed (e.g. ... | head -30 # for the largest 30 packages installed)

nmap -sP 192.168.1.0/24 | grep "Nmap scan report for"| cut -d' ' -f 5 > ips.txt
vim $(grep [REGULAR_EXPRESSION] -R * | cut -d":" -f1 | uniq)
tail -f /var/log/messages.log | while read line ; do echo $line | cut -d \ -f5- | sed s/\\[[0-9]*\\]// | espeak ; done
dpkg --list | rgrep ii | cut -d" " -f3 | sed ':a;N;$!ba;s/\n/ /g' | sed 's/^\(.\)/apt-get install \1/'
curl -s mi-ip.net | grep '"ip"' | cut -f2 -d ">" | egrep -o '[0-9.]+'
2012-10-15 16:48:20
User: dragonauta
Functions: cut egrep grep
0

looks at html for "ip" (it's a CSS class), then a little of cut and egrep to get IPv4 address.

I use this oneliner into conky.

find . \( -iname '*.cpp' -o -iname '*.h' \) -exec wc -l {} \; | sort -n | cut --delimiter=. -f 1 | awk '{s+=$1} END {print s}'
2012-09-19 15:21:01
User: jecxjoopenid
Functions: awk cut find sort wc
0

Searches for *.cpp and *.h in directory structure, counts the number of lines for each matching file and adds the counts together.

lpstat -p | cut -d' ' -f2 | xargs -I{} lpadmin -x {}
2012-09-18 02:11:53
User: bmeehan
Functions: cut lpadmin lpstat xargs
0

This is the closest you can get to "reset printing system" from the command line. Giving credit back to J D McIninch from an apple forum back in 2009.

echo $(find <directory> -name '*.<extension>' -exec du -s {} \; | tee $(tty) | cut -f1 | tr '\n' '+') 0 | bc
2012-09-17 22:46:50
User: ysangkok
Functions: cut du echo find tee tr
-1

Also shows files as they are found. Only works from a tty.