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 - 424 results
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>
cat /var/lib/dpkg/info/*.list > /tmp/listin ; ls /proc/*/exe |xargs -l readlink | grep -xvFf /tmp/listin; rm /tmp/listin
2009-09-09 18:09:14
User: kamathln
Functions: cat grep ls readlink rm xargs
Tags: Debian find dpkg
11

This helped me find a botnet that had made into my system. Of course, this is not a foolproof or guarantied way to find all of them or even most of them. But it helped me find it.

cat testfile | hexdump -C
echo capitalize | { dd bs=1 count=1 conv=ucase 2> /dev/null; cat ;}
cat /etc/passwd | openssl aes-256-cbc -a -e -pass pass:password | netcat -l -p 8080
2009-09-04 20:04:24
User: flip387
Functions: cat
10

Using OpenSSL we can encrypt any input we wish and then use Netcat to create a socket which can be connected to from an externally source (even using a Web Browser)

vimdiff <(svn cat "$1") "$1"
2009-09-04 18:41:40
User: plasticboy
Functions: cat
Tags: svn vim diff color
2

This will diff your local version of the file with the latest version in svn. I put this in a shell function like so:

svd() { vimdiff <(svn cat "$1") "$1"; }
cat /service/solr/log/main/current | tai64nlocal | grep "\(`date '+%F %H'`\|`date '+%F %H %M' | awk '{print $1" 0"$2-1":"$3}'`\)" | grep QTime | awk '{print $NF}' | awk -F\= '{ s += $2} END {print s/NR}'
cat nicescript |ssh middlehost "cat | ssh -a root@securehost 'cat > nicescript'"
2009-08-25 08:11:12
User: syladmin
Functions: cat ssh
Tags: ssh scp
4

middlehost allows ssh access from where you are but not securehost.

Use nice ssh piping to simulate scp through A => B => C

setting up the shell function if left as an exercise for the reader. ;-)

Agent forwarding should avoid password typing.

echo `date +%m/%d/%y%X |awk '{print $1;}' `" => "` cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2, $3;}'` >> datetmp.log
2009-08-24 21:26:29
User: ninadsp
Functions: awk cat echo
7

Uses the data in the /proc system, provided by the acpid, to find out the CPU temperature. Can be run on systems without lm-sensors installed as well.

cat filename | tr '\n' '\0' | du -hsc ?files0-from=-
2009-08-21 18:36:49
User: Diluted
Functions: cat du tr
0

du only accepts lines ending with a NUL, which can be a pain to create. This solves that issue.

cat /proc/cpuinfo
cat filename | grep .
2009-08-09 01:00:59
User: fraktil
Functions: cat grep
Tags: cat Linux grep
2

Pipe any output to "grep ." and blank lines will not be printed.

cat files.txt | xargs tar -cv | tar -x -c $DIR/
2009-08-06 22:55:21
User: lingo
Functions: cat tar xargs
0

If you want certain files out of a directory hierarchy, this will copy just the listed files, but will create the directory hierarchy in the new location ($DIR/)

sudo cat /usr/share/icons/*/*/* > /dev/dsp
2009-08-06 03:12:27
User: 12345678
Functions: cat sudo
0

Plays the sound of the file, should sound like *some* kind of music, most files sound like static but some are really cool.

variations:

sudo cat /dev/sda > /dev/dsp sudo cat /dev/sda5 | aplay

Check out http://bbs.archlinux.org/viewtopic.php?id=70937 for more variations!

semi-dupe--like http://www.commandlinefu.com/commands/view/985/generate-white-noise but with different syntax and program.

cat /etc/debian_version
2009-08-05 14:47:05
User: caiosba
Functions: cat
-5

Easy way to find out what Debian version your machine is running

cat file-that-failed-to-download.zip | curl -C - http://www.somewhere.com/file-I-want-to-download.zip >successfully-downloaded.zip
2009-08-05 13:33:06
Functions: cat
-1

If you are downloading a big file (or even a small one) and the connection breaks or times out, use this command in order to RESUME the download where it failed, instead of having to start downloading from the beginning. This is a real win for downloading debian ISO images over a buggy DSL modem.

Take the partially downloaded file and cat it into the STDIN of curl, as shown. Then use the "-C -" option followed by the URL of the file you were originally downloading.

cat frame/*.mpeg | ffmpeg -i $ID.mp3 -i - -f dvd -y track/$ID.mpg 2>/dev/null
2009-08-04 06:31:50
User: pamirian
Functions: cat
5

This is an extract from a larger script which formats the video for DVD. The videos I use have no audio track so I need to add one. Tweak as you like...

find . -type f -name "*.c" -exec cat {} \; | wc -l
2009-07-30 10:06:51
User: foremire
Functions: cat find wc
1

use find to grep all .c files from the target directory, cat them into one stream, then piped to wc to count the lines

cat file | tee >> file
2009-07-30 07:34:03
User: GeckoDH
Functions: cat file tee
0

The command `cat file >> file` failes with the following error message:

cat: file: input file is output file

`tee` is a nice workaround without using any temporary files.

cat /proc/net/ip_conntrack | grep ESTABLISHED | grep -c -v ^#