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
ls | grep *.txt | while read file; do cat $file >> ./output.txt; done;
if cat /proc/cpuinfo | grep " lm " &> /dev/null; then echo "Got 64bit" ; fi
2010-04-10 15:31:58
User: xeor
Functions: cat echo grep
Tags: cpuinfo
4

Check if you have 64bit by looking for "lm" in cpuinfo. lm stands for "long mem". This can also be used without being root.

cat /etc/apache2/sites-enabled/* | egrep 'ServerAlias|ServerName' | tr -s ' ' | sed 's/^\s//' | cut -d ' ' -f 2 | sed 's/www.//' | sort | uniq
2010-04-08 15:50:34
User: chronosMark
Functions: cat cut egrep sed sort tr
2

Get a list of all the unique hostnames from the apache configuration files. Handy to see what sites are running on a server. A slightly shorter version.

cat /etc/apache2/sites-enabled/* | egrep 'ServerAlias|ServerName' | tr -s " " | sed 's/^[ ]//g' | uniq | cut -d ' ' -f 2 | sed 's/www.//g' | sort | uniq
2010-04-08 08:51:17
User: chronosMark
Functions: cat cut egrep sed sort tr uniq
0

Get a list of all the unique hostnames from the apache configuration files. Handy to see what sites are running on a server.

cat /proc/acpi/thermal_zone/*/temperature
2010-03-28 01:30:14
User: starchox
Functions: cat
0

A quick way of know the temperature info.

curl -sL xkcd.com | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd.pdf
2010-03-03 03:41:31
User: matthewbauer
Functions: cat cut grep sed
Tags: pdf xkcd caption
6

Saves to a PDF with title and alt text of comic.

As asked for on http://bbs.archlinux.org/viewtopic.php?id=91100

Change xkcd.com to dynamic.xkcd.com/comics/random for a random comic.

pdftk A=chapters.pdf B=headings.pdf C=covers.pdf cat C1 B1 A1-7 B2 A8-10 C2 output book.pdf
2010-03-02 16:04:50
User: bw
Functions: cat
6

The command shows the real power of the pdftk tool, you can do basically everything you want with multiple pdf's.

In the command a book is created from chapters, headings and covers.

cat /proc/$(lsof -ti:8888)/cmdline | tr "\0" " "
for file in `find . -iname "FILENAME"`; do cat $file | sed "s/SEARCH_STRING/REPLACE_STRING/" > $file.tmp; mv $file.tmp $file; done
cat authorized_keys_with_broken_lines | sed 's,^ssh,%ssh,' | tr '\n' '\0' | tr '%' '\n' | sed '1d' | sed "/^$/d" > authorized_keys
2010-02-19 08:32:35
User: pepin
Functions: cat sed tr
0

when someone mail you his ssh public key, and the lines are broken with '\n', you can reconstruct a new file with one key by line with this command.

echo $(cat file)
cat file | tr '\n' ''
#Client# cat "The Meters - People Say.mp3" | nc -vv 192.168.1.100 8080; #Server# nc -vv -l -s 192.168.1.100 -p 8080 | mpg123 -v -
2010-01-30 08:50:17
User: Abiden
Functions: cat mpg123
-3

I think I picked this one up from Hak5 (yeah I know.. kinda lame)

echo $(( $( cat count.txt | tr "\n" "+" | xargs -I{} echo {} 0 ) ))
2010-01-27 10:02:30
User: glaudiston
Functions: cat echo tr xargs
0

if you, like me, do not have the numsum, this way can do the same.

for host in $(cat ftps.txt) ; do if echo -en "o $host 21\nquit\n" |telnet 2>/dev/null |grep -v 'Connected to' >/dev/null; then echo -en "FTP $host KO\n"; fi done
2010-01-26 15:34:18
User: vlan7
Functions: cat echo grep host telnet
1

I must monitorize a couple of ftp servers every morning WITHOUT a port-scanner

Instead of ftp'ing on 100 ftp servers manually to test their status I use this loop.

It might be adaptable to other services, however it may require a 'logout' string instead of 'quit'.

The file ftps.txt contains the full list of ftp servers to monitorize.

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