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.

Universal configuration monitoring and system of record for IT.
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

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

Top Tags

Hide

Functions

Hide

Credits

Commands using cut from sorted by
Terminal - Commands using cut - 486 results
cat z.log | grep Timeout | cut -d ':' -f1 | sort | uniq -c
lsof -n -P|grep FlashXX|awk '{ print "/proc/" $2 "/fd/" substr($4, 1, length($4)-1) }'|while read f;do newname=$(exiftool -FileModifyDate -FileType -t -d %Y%m%d%H%M%S $f|cut -f2|tr '\n' '.'|sed 's/\.$//');echo "$f -> $newname";cp $f ~/Vids/$newname;done
2012-02-25 01:49:45
User: mhs
Functions: awk cp cut echo grep read sed tr
8

Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

sed -e 's/[;|][[:space:]]*/\n/g' .bash_history | cut --delimiter=' ' --fields=1 | sort | uniq --count | sort --numeric-sort --reverse | head --lines=20
netstat -Aan | grep .80 | grep -v 127.0.0.1 | grep EST | awk '{print $6}' | cut -d "." -f1,2,3,4 | sort | uniq
2012-02-03 13:54:11
Functions: awk cut grep netstat sort
0

See who is using a specific port. Especially when you're using AIX. In Ubuntu, for example, this can easily be seen with the netstat command.

/usr/sbin/asterisk -rx 'core show channels' | grep -m1 "call" | cut -d' ' -f1
while true; do curl -s http://sensiblepassword.com/?harder=1 | tail -n 15 | head -n 1 | sed 's;<br/>;;' | cut -c 5- | cb; sleep 1; done
2012-01-30 20:52:14
User: supervacuo
Functions: cut head sed sleep tail
1

Use the excellent sensiblepasswords.com to a generate random (yet easy-to-remember) password every second, and copy it to the clipboard. Useful for generating a list of passwords and pasting them into a spreadsheet.

This script uses "madebynathan"'s "cb" function (http://madebynathan.com/2011/10/04/a-nicer-way-to-use-xclip/); you could also replace "cb" with

xclip -selection c

Remove "while true; do" and "; done" to generate and copy only 1 password.

pv -s `fdisk -l /dev/sdX|grep "Disk /"|cut -d' ' -f5` /dev/zero >/dev/sdX
2012-01-25 08:25:00
User: qiet72
Functions: cut grep
0

Overwrite all sectors with zeros in one pass with ETA and speed status. If you wish to do more passes, encapsulate the line in a for loop (7 pass example):

for f in `seq 1 7`; do pv -s `fdisk -l /dev/sdX|grep "Disk /"|cut -d' ' -f5` /dev/zero >/dev/sdX ; done

Note: Substitute /dev/sdX with the correct drive you wish to erase. Also, you may have to be root to execute the command.

kde-open -v | grep Platform | cut -d' ' -f4-
2012-01-19 13:05:26
User: funollet
Functions: cut grep
0

Sample input:

kde-open -v

Qt: 4.7.4

KDE Development Platform: 4.7.3 (4.7.3)

KIO Client: 2.0

mount|grep -e '//'|cut -d ' ' -f3| xargs -I {} umount {}
2012-01-17 01:20:09
Functions: cut grep mount umount xargs
Tags: samba
0

This is a handy command to put into ~/.bash_logout to automatically un-mount windows shares whenever the user logs out. If you use this on as a non-root account then you'll need to append sudo before umount and the user will need to have the appropriate sudoer rights to run the /bin/umount command.

echo Faltan `curl http://www.elproximoferiado.com.ar/index.php?country=AR -silent | grep contador | cut -f2 -d">" | cut -f1 -d"<"` dias para el proximo feriado
comm -13 <(od -vw1 -tu1 dummy.txt|cut -c9-|sort -u) <(seq 0 127|sort)|perl -pe '$_=chr($_)'|od -c
2012-01-09 01:32:20
User: bazzargh
Functions: comm cut od perl seq sort
Tags: bash
0

Search in decimal rather than hex. od dumps the character list, cut to remove offsets, sort -u gives the used characters. seq gives the comparison list, but we need this sorted alphabetically for comm, which does the filtering. I drop to perl to convert back to characters (is there a better way?) and then use od to dump them in a print-safe format.

mysql -NBe 'show global status like "Threads_connected";' | cut -f2
history | cut -c8- | sort | uniq -c | sort -rn | head
cut -d: -f1 /etc/passwd | sort
cut -d: -f1 /etc/passwd | sort
xml2 < file.xml | grep ^/path/to/element | cut -f2- -d=
2011-12-19 18:51:17
User: bandie91
Functions: cut grep
Tags: cut xml
2

poor man's xml parser :)

$(($(hostname|sum|cut -f1 -d" ")%27+1))
2011-12-13 19:27:03
Functions: cut
0

Requirements:

Output: integer x , 1>=x

Input: hostname

Able to reproduce on the same host

Acceptable for output to be different among OSes (Solaris, Linux, BSD)

Useful for providing DayOfMonth splay in cron jobs. Capped at 28 for Febtober.

mplayer -endpos 0.1 -vo null -ao null -identify *.avi 2>&1 |grep ID_LENGTH |cut -d = -f 2|awk '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}'
while read l; do echo -e "$RANDOM\t$l"; done | sort -n | cut -f 2
$ lynx -useragent=Opera -dump 'http://www.facebook.com/ajax/typeahead_friends.php?u=100003119823986&__a=1' |gawk -F'\"t\":\"' -v RS='\",' 'RT{print $NF}' |grep -v '\"n\":\"' |cut -d, -f2
for i in `ps -ef | grep tracker | awk '{print $8}' | cut -d'/' -f4 | grep -v grep`; do killall -9 $i; done
2011-12-04 13:20:19
User: alienone
Functions: awk cut grep killall
-3

* ps -ef # list running processes

* grep string

* pull the process names from 8th field

* cut and delimiter '/'

* print 4th field

* get rid of trailing grep

* for loop killall -9 $i which is the process name

lynx -useragent=Opera -dump 'http://www.facebook.com/ajax/typeahead_friends.php?u=Bilal Butt&__a=1' |gawk -F'\"t\":\"' -v RS='\",' 'RT{print $NF}' |grep -v '\"n\":\"' |cut -d, -f2
read -s pass; echo $pass | md5sum | base64 | cut -c -16
2011-11-24 20:23:47
User: bugmenot
Functions: cut echo md5sum read
22

Why remember? Generate!

Up to 48 chars, works on any unix-like system (NB: BSD use md5 instead of md5sum)

curl -s 'http://whatthecommit.com/' | grep '<p>' | cut -c4-
date -d @$(echo $(($(date +%s)-$(cat /proc/uptime|cut -d. -f1))))