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 - 416 results
infile=$1 for i in $(cat $infile) do echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//" echo done
2009-07-12 21:23:37
User: iframe
Functions: cat echo sed sort tr
Tags: cat bash sort sed tr
0

Save the script as: sort_file

Usage: sort_file < sort_me.csv > out_file.csv

This script was originally posted by Admiral Beotch in LinuxQuestions.org on the Linux-Software forum.

I modified this script to make it more portable.

cat /dev/clipboard; $(somecommand) > /dev/clipboard
2009-07-10 18:48:21
User: sud0er
Functions: cat
Tags: windows cygwin
12

I spent a bunch of time yesterday looking for the xsel package in Cygwin- turns out you can use the /dev/clipboard device to do the same thing.

cat <<.>> somefilename
2009-07-10 17:45:42
User: tomlouie
Functions: cat
Tags: text
4

If you just want to write or append some text to a file without having to run a text editor, run this command. After running the command, start typing away. To exit, type . on a line by itself.

Replacing the >> with a single > will let you overwrite your file.

cat large.xml | xclip
2009-07-08 16:30:07
User: copremesis
Functions: cat
0

avoid mouse abuse and the constant struggle of balancing scroll velocity ... not to mention that burning sensation in your upper right shoulder ....

test `uname` = Linux && lsb_release -a || ( test `uname` = SunOS && cat /etc/release || uname -rms )
2009-07-07 20:51:30
User: virtualshock
Functions: cat test uname
-7

Found in comments section works on most Linux flavors.

cat /var/log/auth.log | logtool -o HTML > auth.html
2009-07-03 18:17:22
Functions: cat
3

Logtool is a nice tool that can export log file to various format, but its strength lies in the capacity of colorize logs. This command take a log as input and colorize it, then export it to an html file for a more confortable view. Logtool is part of logtool package.Tested on Debian.

cat myfile.txt | tr -d '\n'
pdftk $* cat output $merged.pdf
find . -type f -name *.ext -exec cat {} > file.txt \;
2009-06-17 11:33:14
User: realgt
Functions: cat find
2

Useful if you have to put together multiple files into one and they are scattered across subdirectories. For example: You need to combine all .sql files into one .sql file that would be sent to DBAs as a batch script.

You do get a warning if you create a file by the same extension as the ones your searching for.

find . -type f -name *.sql -exec cat {} > BatchFile.txt \;

mysql -uadmin -p` cat /etc/psa/.psa.shadow` -Dpsa -e"select mail_name,name,password from mail left join domains on mail.dom_id = domains.id inner join accounts where mail.account_id = accounts.id;"
function my_irc { tmp=`mktemp`; cat > $tmp; { echo -e "USER $username x x :$ircname\nNICK $nick\nJOIN $target"; while read line; do echo -e "PRIVMSG $target :$line"; done < $tmp; } | nc $server > /dev/null ; rm $tmp; }
2009-06-11 22:14:48
User: Josay
Functions: cat echo read rm
Tags: netcat irc nc
1
command | my_irc

Pipe whatever you want to this function, it will, if everything goes well, be redirected to a channel or a user on an IRC server.

Please note that :

- I am not responsible of flood excesses you might provoke.

- that function does not reply to PINGs from the server. That's the reason why I first write in a temporary file. Indeed, I don't want to wait for inputs while being connected to the server. However, according to the configuration of the server and the length of your file, you may timeout before finishing.

- Concerning the server, the variable content must be on the form "irc.server.org 6667" (or any other port). If you want to make some tests, you can also create a fake IRC server on "localhost 55555" by using

netcat -l -p 55555

- Concerning the target, you can choose a channel (beginning with a '#' like "#chan") or a user (like "user")

- The other variables have obvious names.

ssh $HOST -l$USER cat /REMOTE/FILE | sdiff /LOCAL/FILE -
cat somefile.css | awk '{gsub(/{|}|;/,"&\n"); print}' >> uncompressed.css
2009-06-02 15:51:51
User: lrvick
Functions: awk cat
0

Ever compress a file for the web by replacing all newline characters with nothing so it makes one nice big blob?

It is a great idea, however what about when you want to edit that file? ...Serious pain in the butt.

I ran into this today in that my only copy of a CSS file was "compressed" with no newlines.

I whipped this up and it converted back into nice human readable CSS :-)

It could be nicer, but it does the job.

while [ i != 0 ]; do sleep 1 | dialog --clear --gauge "Quality: " 0 0 $(cat /proc/net/wireless | grep $WIRELESSINTERFACE | awk '{print $3}' | tr -d "."); done
2009-05-31 16:09:23
User: ncaio
Functions: awk cat grep sleep tr
1

The variable WIRELESSINTERFACE indicates your wireless interface

less -Rf <( cat <(ls -l --color=always) <(ls -ld --color=always .*) )
2009-05-30 23:51:16
User: asmoore82
Functions: cat less ls
2

To sort hidden files first, simply switch the two inner `ls` commands.

I have this aliased to `dira`

`dir` is aliased to the simpler version with no hidden files:

ls -l --color=always | less -R
cat typescript | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > typescript-processed
cat /etc/SuSE-release
2009-05-20 17:28:12
User: sharfah
Functions: cat
Tags: SuSE
-3

Displays SuSE release information

count() { find $@ -type f -exec cat {} + | wc -l; }
VARNAMES='ID FORENAME LASTNAME ADDRESS CITY PHONE MOBILE MAIL' ; cat customer.csv | while read LINE ; do COUNT=1 ; for VAR in $VARNAMES ; do eval "${VAR}=`echo $LINE | /usr/bin/awk {'print $'$COUNT''}`" ; let COUNT=COUNT+1 ; done ; done
2009-05-19 11:23:00
User: GeckoDH
Functions: cat eval read
-1

VARNAMES='ID FORENAME LASTNAME ADDRESS CITY PHONE MOBILE MAIL ...'

cat customer.csv | while read LINE ; do

COUNT=1

for VAR in $VARNAMES ; do

eval "${VAR}=`echo $LINE | /usr/bin/awk {'print $'$COUNT''}`"

let COUNT=COUNT+1

done

done

Maybe you have a CSV-File with addresses, where you have to process each contact (one per line, write each value to own variable). Of course you can define every variable, but this way is more simple and faster (to write).

VARNAMES includes the variable names. Pay attention: the number of names in VARNAMES have to be the same than in the CSV-file the fields. If the CSV is not seperated with ";", you can set the seperator after the awk-binary with -F"_" for example.

cat -n FILE | grep -C3 "^[[:blank:]]\{1,5\}NUMBER[[:blank:]]"
2009-05-17 18:19:55
User: lv4tech
Functions: cat grep
-1

This is useful for displaying a portion of a FILE that contains an error at line NUMBER

while true; do cat /usr/src/linux/kernel/signal.c > /dev/dsp; done
2009-05-16 14:44:57
Functions: cat
1

replace "/usr/src/linux/kernel/signal.c" with any file you want and listen to its output ! :P

you can also replace "cat" with "echo" or anything you can come up with

have fun :-}

cat /etc/*issue
cat /etc/psa/.psa.shadow
2009-04-30 18:08:12
User: jigglebilly
Functions: cat
-1

I'm sure almost everybody knows this by now. This command will pull the password for the admin login of any plesk machine.

p=$(netstat -nate 2>/dev/null | awk '/LISTEN/ {gsub (/.*:/, "", $4); if ($4 == "4444") {print $8}}'); for i in $(ls /proc/|grep "^[1-9]"); do [[ $(ls -l /proc/$i/fd/|grep socket|sed -e 's|.*\[\(.*\)\]|\1|'|grep $p) ]] && cat /proc/$i/cmdline && echo; done
2009-04-30 12:39:48
User: j0rn
Functions: awk cat grep ls netstat sed
-5

Ok so it's rellay useless line and I sorry for that, furthermore that's nothing optimized at all...

At the beginning I didn't managed by using netstat -p to print out which process was handling that open port 4444, I realize at the end I was not root and security restrictions applied ;p

It's nevertheless a (good ?) way to see how ps(tree) works, as it acts exactly the same way by reading in /proc

So for a specific port, this line returns the calling command line of every thread that handle the associated socket

yes '' | cat -n