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 - 428 results
cat /proc/meminfo
nmap -sP <subnet>.* | egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' > results.txt ; for IP in {1..254} ; do echo "<subnet>.${IP}" ; done >> results.txt ; cat results.txt | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 | uniq -u
for i in $(ps -ef | awk '{print $2}') ; { swp=$( awk '/Swap/{sum+=$2} END {print sum}' /proc/$i/smaps ); if [[ -n $swp && 0 != $swp ]] ; then echo -n "\n $swp $i "; cat /proc/$i/cmdline ; fi; } | sort -nr
cat -n <file> | sort -k 2 | uniq -f 1 | sort -n | cut -f 2-
2010-01-21 18:55:58
User: fpunktk
Functions: cat cut sort uniq
4

i wanted to delete all duplicate lines from .bash_history and keep the order of the other lines.

the command cat's the file and adds line numbers, then sorts by the second column. afterwards uniq omits repeated lines, but skips the first field (the line number). then it sorts by the line numbers and at the end cuts the numbers off.

# cat file1.mp3 file2.mp3 > file3.mp3
2010-01-17 13:18:34
User: svnlabs
Functions: cat
-4

cat - concatenate MP3 files and save it...

(bzcat BZIP2_FILES && cat TEXT_FILES) | grep -E "Invalid user|PAM" | grep -o -E "from .+" | awk '{print $2}' | sort | uniq >> /etc/hosts.deny
2010-01-03 04:41:51
User: jayhawkbabe
Functions: awk cat grep sort uniq
3

Searches all log files (including archived bzip2 files) for invalid user and PAM authentication errors, both of which are indicative of brute force attempts at logging into computer. A list of all unique IP addresses and domain names is appended to hosts.deny. The command (and grep error messages) will work on Mac OS X 10.6, small adjustments may be needed for other OSs.

cat | gcc -x c -o a.out - && ./a.out && rm a.out
2009-12-27 04:37:24
User: dgalling
Functions: c++ cat gcc rm
-2

This should work on any unix platform running bash. Just type the program into cat and give it a ^D when you're done, at which time it will compile, run, and remove the program. Obviously, you can run it without the "rm a.out" if you'd like to keep the binary. If you want to keep the source, well, you might as well just write it in vi or emacs first then.

ssh HOST cat < LOCALFILE ">" REMOTEFILE
ps aux | grep [h]ttpd | cat -n
2009-12-17 20:45:44
User: putnamhill
Functions: cat grep ps
Tags: cat
0

If you're on a system that doesn't have nl, you can use cat -n.

cat ~/.ssh/id_rsa.pub | ssh <remote_host> "xargs --null echo >> ~/.ssh/authorized_keys"
2009-12-17 15:12:11
User: koushik
Functions: cat ssh
0

Well its just appending your public key to the remote hosts authorized_keys, but can get messy logging in and out

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 } ' )