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

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

Top Tags

Hide

Functions

Hide

Credits

All commands from sorted by
Terminal - All commands - 12,083 results
zenity --info --text "Your welcome! Lunch?" --display=:0
clear; while sleep 1; do d=$(date +"%H:%M:%S"); e=$(echo "toilet -t -f mono12 $d");tput setaf 1 cup 0; eval $e; tput setaf 4 cup 8; eval "$e -F flop";tput cup 0; done
find . -path "*/any_depth/*" -exec grep "needle" {} +
mysms='xxx0001234@messaging.sprintpcs.com' ; expect -c "log_user 0 ; set timeout -1 ; spawn usbmon -i usb0 ; expect -re \"C.*Ii.*-2:128\" { spawn sendmail $mysms ; send \"Smart Home Sensor Triggered\n.\n\" ; expect }"
2

An old USB A/B cable is all you need to make your own Smart Home hardware!

Cut off and discard the B-portion of the USB cable. On the A side, connect the RED (+) and WHITE (D-) wires via a 1 kiloohm resistor.

Picture:

http://imgur.com/dJGVlAU

Now plug the cable into a USB port on your Linux computer. Your hardware is ready!

Run the above command after changing variable mysms to your personal email-to-SMS gateway info as required by your cellular service provider.

The command uses the amazing usbmon tool (see link below) to detect the cable.

For the curious, to view the raw usbmon output, run this command: (Also see the sample output)

usbmon -i usb0

How does it work? When the red and white wires are connected (via the 1 kiloohm resistor) the USB hardwere is tricked into thinking that a new USB device is trying to start up.

We then use the usbmon utility to capture the host USB events as it tries to talk to the cable.

The expect utility watches the usbmon stream and waits for the disconnect text "-2:128" before sending the SMS message.

Finally, the sendmail tool is used to email the SMS message to your smartphone via your cellular provider's SMS-to-email gateway.

As a result, when the electrical connection between the red and white wire is interrupted, or the USB cable is unplugged from your computer, you get an SMS notification of the disconnect event on your smartphone.

Could this be the cheapest smart home gadget ever? What are YOU going to sense with it?

Please let me know in the comments and please don't forget to click it up!

Links:

http://www.linuxcertif.com/man/8/usbmon/

http://en.wikipedia.org/wiki/USB#Pinouts

http://imgur.com/dJGVlAU

function summaryIP() { < $1 awk '{print $1}' | while read ip ; do verifyIP ${ip} && echo ${ip}; done | awk '{ip_array[$1]++} END { for (ip in ip_array) printf("%5d\t%s\n", ip_array[ip], ip)}' | sort -rn; }
2015-05-01 16:45:05
User: mpb
Functions: awk echo read sort
1

Working with lists of IP addresses it is sometimes useful to summarize a count of how many times an IP address appears in the file.

This example, summarizeIP, uses another function "verifyIP" previously defined in commandlinefu.com to ensure only valid IP addresses get counted. The summary list is presented in count order starting with highest count.

echo "DISPLAY=$DISPLAY xmessage call the client" | at 10:00
2015-05-01 14:57:15
User: op4
Functions: at echo
Tags: echo at xmessage
1

This command will create a popup reminder window to assist in remembering tasks

http://i.imgur.com/2n7viiA.png is how it looks when created

function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; ip4="^$octet\.$octet\.$octet\.$octet$"; [[ ${1} =~ $ip4 ]] && return 0 || return 1; }
2015-05-01 12:22:57
User: mpb
Functions: return
1

When processing IP addresses in the shell (or shell script) it is useful to be able to verify that the value of data is an IP address (an not some random string or non-sensible IP address).

git for-each-ref --sort=-committerdate refs/heads/
openssl rsa -in key.priv -pubout > key.pub
2015-04-28 19:10:17
User: krizzo
2

This will extract the public key that is stored in the private key using openssl.

for a in $(ls /usr/sbin /usr/bin); do ps -fC $a;done|grep -v PPID
2015-04-27 18:15:56
User: knoppix5
Functions: grep ls ps
0

Thanks to pooderbill for the idea :-)

find . -type f -name '*' -exec md5sum '{}' + > hashes.txt
input=a.pdf ; pages=`pdftk $input dump_data | grep -i numberofpages | cut -d" " -f 2`; pdftk A=$input shuffle A1-$[$pages/2] A$pages-$[$pages/2+1] output "${input%.*}.rearranged.${input##*.}"
2015-04-26 20:05:20
User: kobayashison
Functions: cut grep
0

Rearrange pdf document coming from a simplex document feed scanner, feeded first with odd pages, then with even pages from the end. Needs pdftk >1.44 w/ shuffle.

Similar to http://www.commandlinefu.com/commands/view/7965/pdf-simplex-to-duplex-merge where there are 2 separate documents, odd and even

perl -e 'for(;;sleep 1){printf"\r"."%.4b "x6,split"",`date +%H%M%S`}'
timeDNS() { parallel -j0 --tag dig @{} "$*" ::: 208.67.222.222 208.67.220.220 198.153.192.1 198.153.194.1 156.154.70.1 156.154.71.1 8.8.8.8 8.8.4.4 | grep Query | sort -nk5; }
du -sk -- * | sort -n | perl -pe '@SI=qw(K M G T P); s:^(\d+?)((\d\d\d)*)\s:$1." ".$SI[((length $2)/3)]."\t":e'
2015-04-26 08:07:27
Functions: du perl sort
2

Tested on MacOS and GNU/Linux.

It works in dirs containing files starting with '-'.

It runs 'du' only once.

It sorts according to size.

It treats 1K=1000 (and not 1024)

sudo npm cache clean -f | sudo npm install -g n | sudo n stable
sudo mtr -s 1472 -B 0 -oLDRSWNBAWVJMXI <ip address>
2015-04-24 13:46:36
User: bort
Functions: sudo
0

Uses packet size 1472, padded with zeroes, and shows more columns with jitter statistics to the right. This is a greatly improved version of traceroute.

du -sc .[!.]* * |grep '^[0-9]{4}'
2015-04-24 10:51:13
User: gander
Functions: du grep
0

thanks to GREP_COLOR the output will highlite the first 4 digits. if all files are few MB only, this gives a quick overview of how many powers of 10 bigger than 1MB they really are, a logarithmic scale. same works if files are more than 1GB when you replace the "4" by a "7", I usually use "5" in order to manually decide what files to delete...

test "$(md5sum /local/file | cut -d' ' -f1)" == "$(ssh root@xen -- md5sum /remote/file | cut -d' ' -f1)" && echo "Match" || echo "Differ"
2015-04-24 03:09:33
User: gnif
Functions: echo test
1

This method does not transfer the contents of the entire file, instead it computes a md5 sum of the file on each end so that large files can be compared without transferring them across the net.

git rev-list --reverse --topo-order master... | while read rev; do git checkout preview; git cherry-pick $rev || break; done
2015-04-23 14:28:06
User: shadyvb
Functions: read
Tags: git preview
-2

Creating feature-branches off master, and trying to merge them in an integration branch (preview), sometimes causes conflicts because the feature-branch might hold changes from 'master' that aren't on preview yet. So this ensures only the commits added to the feature-branch are moved to integration (preview).

Note: This assumes you're currently on the feature-branch. Adjust 'master/preview' branch names to suit your environment.

tail +### MYFILE
2015-04-23 11:49:15
User: pooderbill
Functions: tail
1

Useful for finding newly added lines to a file, tail + can be used to show only the lines starting at some offset. A syslog scanner would look at the file for the first time, then record the end_of_file record number using wc -l. Later (hours, days), scan only at the lines that were added since the last scan.

Blocking ip: arp -s ip_of_host 0, Unblocking ip: arp -d ip_blocked
2015-04-22 19:00:13
User: andregyn62
Functions: arp
0

When you block any hosts using this method, the hosts can't do anything in the network.

The block is applied on firewall or gateway of the network.

view + LOGFILE
2015-04-21 11:23:01
User: pooderbill
0

view is the command: vi -r which opens a file in read-only mode. The + character jumps to the bottom of the logfile where the most relevant information starts. Other aliases can be created for commonly viewed logfiles.

git diff --word-diff --color-words | aha > index.html && firefox index.html
ps -fC PROCESSNAME
2015-04-20 13:09:44
User: pooderbill
Functions: ps
Tags: grep function ps
9

ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.