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 - 470 results
$(($(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
21

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))))
lsusb -s :`lsusb | grep iPhone | cut -d ' ' -f 4 | sed 's/://'` -v | grep iSerial | awk '{print $3}'
2011-11-03 20:39:43
User: dakira
Functions: awk cut grep lsusb sed
Tags: iphone
3

Display the serial of the iPhone (aka UDID).

cat /var/log/auth.log | grep -i "pam_unix(sshd:auth): authentication failure;" | cut -d' ' -f14,15 | cut -d= -f2 | sort | uniq
2011-10-25 04:58:09
User: JohnQUnknown
Functions: cat cut grep sort
0

This command shows a sorted list of the IP addresses from which there have been authentication errors via SSH (possible script kiddies trying to gain access to your server), it eliminates duplicates so it's easier to read, but you can remove the "uniq" command at the end, or even do a "uniq -c" to have a count of how many times each IP address shows in the log (the path to the log may vary from system to system)

uptime | cut -d "," -f 1 | cut -d " " -f 4-
cat /etc/passwd | grep "bash" | cut -d: -f1
alias busy='rnd_file=$(find /usr/include -type f -size +5k | sort -R | head -n 1) && vim +$((RANDOM%$(wc -l $rnd_file | cut -f1 -d" "))) $rnd_file'
2011-10-16 00:05:59
User: frntn
Functions: alias cut find head sort vim wc
0

Enhancement for the 'busy' command originally posted by busybee : less chars, no escape issue, and most important it exclude small files ( opening a 5 lines file isn't that persuasive I think ;) )

This makes an alias for a command named 'busy'. The 'busy' command opens a random file in /usr/include to a random line with vim.

IFS=$'\n' && for f in `find . -type f -exec md5sum "{}" \;`; do echo $f | sed -r 's/^[^ ]+/Checking:/'; echo $f | cut -f1 -d' ' | netcat hash.cymru.com 43 ; done
2011-10-15 03:38:47
User: Neo23x0
Functions: cut echo md5sum sed
11

Command makes use of the Malware Hash Registry (http://www.team-cymru.org/Services/MHR/).

It parses the current directory and subdirectories and calculates the md5 hash of the files, then prints the name and sends the hash to the MHR for a lookup in their database.

The 3rd value in the result is the detection percentage across a mix of AV packages.

wget -qO - --post-data "data[Row][cripted]=1cb251ec0d568de6a929b520c4aed8d1" http://md5-decrypter.com/ | grep -A1 "Decrypted text" | tail -n1 | cut -d '"' -f3 | sed 's/>//g; s/<\/b//g'
2011-10-13 03:48:54
User: samhagin
Functions: cut grep sed tail wget
Tags: md5
0

Decrypt MD5 , replace 1cb251ec0d568de6a929b520c4aed8d1 with the MD5 string you want to decrypt

wget -qO - --post-data "data[Row][clear]=text" http://md5-encryption.com | grep -A1 "Md5 encrypted state" | tail -n1 | cut -d '"' -f3 | sed 's/>//g; s/<\/b//g'
2011-10-13 03:44:48
User: samhagin
Functions: cut grep sed tail wget
Tags: md5
0

Encrypt any text to MD5 , replace text with the string you want to convert to MD5

find . -exec `alias foo | cut -d"'" -f2` {} \;
2011-10-05 08:41:31
User: ztank1013
Functions: cut find
5

An alias cannot be executed as command in a find -exec line. This form will trick the command line and let you do the job.

cut -c -N
2011-09-26 04:46:35
User: ken_fallon
Functions: cut
Tags: cut
1

Saves one character, the original is probably clearer

pacman -Q | grep -v pacman | cut -d' ' -f1 > packages.txt && pacman -Sy `cat packages.txt` --noconfirm
cut -c 1-N
2011-09-24 14:00:59
User: dudko
Functions: cut
Tags: cut
1

You can also cut charactes starting from X to N.

echo "command lines" | rev | cut -c 2- | rev
2011-09-21 11:27:52
User: ztank1013
Functions: cut echo ping rev
Tags: sed awk cut rev
0

In case sed and awk are not available you may use this to remove the last character from a string with "rev" and "cut".

wget -O - -q http://checkip.dyndns.org/ | cut -d':' -f2 | cut -d'<' -f1| cut -c2-
2011-09-17 13:42:01
User: ztank1013
Functions: cut wget
-2

This is just a "cut" addicted variant of the previous unixmonkey24730 command...

fn=$(find . -type f -printf "%T@\t%p\n"|sort -n|tail -1|cut -f2); echo $(date -r "$fn") "$fn"
svn st -q | cut -c 2- | tr -d ' ' | xargs tar -czvf ../backup.tgz
2011-08-31 10:30:29
User: titus
Functions: cut tar tr xargs
1

This works more reliable for me ("cut -c 8-" had one more space, so it did not work)