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 cat from sorted by
Terminal - Commands using cat - 418 results
cat infile | while read str; do echo "$((++i)) - $str" ; done;
2009-12-09 14:05:09
User: glaudiston
Functions: cat echo read
-1

Yep, is hard, but is a way more flexible using pipe.

cat -n
cat </dev/tcp/time.nist.gov/13
2009-12-03 21:40:14
User: drewk
Functions: cat
Tags: cat tcp
8

The format is JJJJJ YR-MO-DA HH:MM:SS TT L DUT1 msADV UTC(NIST) OTM

and is explained more fully here: http://tf.nist.gov/service/acts.htm

cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | sed 1q
cat ~/.viminfo | sed -n '/^:[0-9]\+,\([0-9]\+\|\$\)s/p'
2009-11-29 01:54:57
User: jyf
Functions: cat sed
1

i want to count how many regex code i have used in vim in a long time

so i make a directory in svn host and post record to this directory

of course i dont want to post manually so i worte a script to do that

and this is the core thing to do

while true; do wget -r -l1 --no-clobber -A.txt http://911.wikileaks.org/files/index.html; done; cat *.txt | grep pass
[ $1 == "client" ] && hostname || cat $0 | ssh $1 /bin/sh -s client
2009-11-25 22:24:31
User: a8ksh4
Functions: cat hostname ssh
6

Now put more interesting stuff on the script in replacement of hostname, even entire functions, etc, and stuff.

hosta> cat myScript.sh

#!/bin/sh

[ $1 == "client" ] && hostname || cat $0 | ssh $1 /bin/sh -s client

hosta> myScript.sh hostb

hostb

hosta>

cat /var/log/httpd/access_log | grep q= | awk '{print $11}' | awk -F 'q=' '{print $2}' | sed 's/+/ /g;s/%22/"/g;s/q=//' | cut -d "&" -f 1 | mail youremail@isp.com -s "[your-site] search strings for `date`"
2009-11-22 03:03:06
User: isma
Functions: awk cat grep sed strings
-2

It's not a big line, and it *may not* work for everybody, I guess it depends on the detail of access_log configuration in your httpd.conf. I use it as a prerotate command for logrotate in httpd section so it executes before access_log rotation, everyday at midnight.

cat *.c | { printf "se te du\nplot '-' t '' w dots\n"; tr '[[:upper:]]' '[[:lower:]]' | tr -s [[:punct:][:space:]] '\n' | sort | uniq -c | sort -nr | head -n 100 | awk '{print $1}END{print "e"}'; } | gnuplot
2009-11-20 14:53:26
User: taliver
Functions: awk cat head printf sort tr uniq
5

Uses the dumb terminal option in gnuplot to plot a graph of frequencies. In this case, we are looking at a frequency analysis of words in all of the .c files.

pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf
cat /dev/hda > ~/hda.iso
cat /path/to/some/file.txt | tee /dev/pts/0 | wc -l
2009-11-07 22:24:28
User: atoponce
Functions: cat tee wc
Tags: tee
2

This is a cool trick to view the contents of the file on /dev/pts/0 (or whatever terminal you're using), and also send the contents of that file to another program by way of an unnamed pipe. All the while, you've not bothered saving any extra data to disk, like you might be tempted to do with sed or grep to filter output.

exec 3<>/dev/tcp/whatismyip.com/80; echo -e "GET /automation/n09230945.asp HTTP/1.0\r\nHost: whatismyip.com\r\n" >&3; a=( $(cat <&3) ); echo ${a[${#a[*]}-1]};
cat > list -
2009-10-29 20:20:11
Functions: cat
3

Quick write some notes to a file with cat. Ctrl+C when you have finish.

TIMEUNIT=$( cat a | grep -n "timescale" | awk -F ":" '{ print $1 } ' )
teste.txt < cut -d : -f 1,5 /etc/passwd | tr : \\t | tr a-z A-Z | cat teste.txt
cat private-file | gpg2 --encrypt --armor --recipient "Disposable Key" | mailx -s "Email Subject" user@email.com
2009-10-19 20:38:37
User: slashdot
Functions: cat mailx
2

This is a quick and easy way of encrypting files in a datastream, without ever really creating an output file from gpg. Useful with cron also, when file(s) have to be sent based on a set schedule.

for f in *.html; do head -n -1 $f > temp; cat temp > $f; rm temp; done
2009-10-12 12:49:18
User: Sunng
Functions: cat head rm
-1

Some malicious program appends a iframe or script tag to you web pages on some server, use this command to clean them in batch.

cat file.txt | while read line; do printf "%7.2f -> %7.2f\n" $line; done
cat ~/SortedFile.txt | perl -wnl -e '@f=<>; END{ foreach $i (reverse 0 .. $#f) { $r=int rand ($i+1); @f[$i, $r]=@f[$r,$i] unless ($i==$r); } chomp @f; foreach $line (@f){ print $line; }}'
2009-09-24 15:42:43
User: drewk
Functions: cat perl
0

The sort utility is well used, but sometimes you want a little chaos. This will randomize the lines of a text file.

BTW, on OS X there is no

| sort -R

option! There is also no

| shuf

These are only in the newer GNU core...

This is also faster than the alternate of:

| awk 'BEGIN { srand() } { print rand() "\t" $0 }' | sort -n | cut -f2-
host A: cat /proc/dev/ttyS0 host B: echo hello > /dev/ttyS0
2009-09-24 13:22:23
User: flart
Functions: cat echo host
2

If the connection works you should see a "hello" on host A. If not: check your cabeling etc :-)

cat -n file or cat -b file
2009-09-24 12:18:40
User: eastwind
Functions: cat file
0

cat -n file : number all line

cat -b file : number only non empty line

see man cat

cat filename | uuencode filename | mail -s "Email subject" user@example.com
2009-09-21 04:13:50
User: amaymon
Functions: cat mail uuencode
Tags: cat mail
0

uuencode the file to appear as an attachment

cat filename | mail -s "Email subject" user@example.com
2009-09-20 01:38:23
Functions: cat mail
Tags: cat mail
2

This just reads in a local file and sends it via email. Works with text or binary. *Requires* local mail server.

cat *.sql | mysql <db_name>