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 - 417 results
yes '' | cat -n
sudo cat /dev/mem > /dev/dsp
( cat badfile.log ; tac badfile.log | tac ) > goodfile.log
2009-04-21 22:06:05
User: flux
Functions: cat tac
15

man tac

When there is a bad block in the middle of your file, you can see its begninning with `cat' and its end with `tac'. But both commands terminates with an error. So this sequence rebuilds a new file without badblock.

cat [ENTER]^V^O[ENTER]^D
2009-04-17 13:16:56
User: minliu2k
Functions: cat
-3

^V means CTRL-V

[ENTER] means ENTER key

cat <filename> | perl -e '$/ = ""; $_ = <>; s/<!--.*?-->//gs; print;'
2009-04-15 20:29:11
User: unixx
Functions: cat perl
0

xml with verbose commenting can be difficult to read. remove comments from xml.

sudo cat /dev/vcs1 | fold -w 80
2009-04-15 08:49:48
User: animoid
Functions: cat fold sudo
16

This will view the console and assumes the screen is 80 characters wide.

Use /dev/vcs2 for the next virtual console.. etc.

command > tmp && cat logfile.txt >> tmp && tmp > logfile.txt && rm tmp
2009-04-05 22:00:32
User: akoumjian
Functions: cat command rm
-2

Adds the stdout (standard output) to the beginning of logfile.txt. Change "command" to whatever command you like, such as 'ls' or 'date', etc. It does this by adding the output to a temporary file, then adding the previous contents of logfile.txt to the temp file, then copying the new contents back to the logfile.txt and removing the temp file.

diff <(ssh alice cat /etc/apt/sources.list) <(ssh bob cat /etc/apt/sources.list)
cat /var/log/secure | grep smtp | awk '{print $9}' | cut -f2 -d= | sort | uniq -c | sort -n | tail
2009-03-30 15:49:54
User: empulse
Functions: awk cat cut grep sort uniq
-2

Searches /var/log/secure for smtp connections then lists these by number of connections made and hosts.

cat /var/log/secure | grep sshd | grep Failed | sed 's/invalid//' | sed 's/user//' | awk '{print $11}' | sort | uniq -c | sort -n
2009-03-30 15:48:24
User: empulse
Functions: awk cat grep sed sort sshd uniq
8

Searches the /var/log/secure log file for Failed and/or invalid user log in attempts.

cat *.mpg > all.mpg
2009-03-27 04:49:18
User: smcpherson
Functions: cat
2

Good old cat & output redirection. Using this method you can combine all kinds of things - even mpeg files. My video camera makes a series of .mpeg files that are broken into 4gb chunks. Using this command I can easily join them together. Even better, combined with the cp command the files can be copied and joined in one step.

watch -n1 --differences cat /proc/meminfo
cat /dev/cdrom > ~/mydisk.iso
2009-03-26 05:54:41
User: sata
Functions: cat
Tags: Linux
-10

Generate the iso from the disk, easily.

same as "dd if=/dev/cdrom of=~/mydisk.iso"

xclip -o > /tmp/spell.tmp; aspell check /tmp/spell.tmp ; cat /tmp/spell.tmp | xclip
2009-03-26 00:49:59
User: alperyilmaz
Functions: cat
2

xclip -o > /tmp/spell.tmp # Copy clipboard contents to a temp file

aspell check /tmp/spell.tmp # Run aspell on that file

cat /tmp/spell.tmp | xclip # Copy the results back to the clipboard, so that you can paste the corrected text

I'm not sure xclip is installed in most distributions. If not, you can install x11-apps package

cat -v -t -e
2009-03-24 19:29:03
User: alperyilmaz
Functions: cat
Tags: cat
4

Useful to detect number of tabs in an empty line, DOS newline (carriage return + newline).

A tool that can help you understand why your parsing is not working.

vimdiff foo.c <(bzr cat -r revno:-2 foo.c)
cat schema.sql data.sql test_data.sql | mysql -u user --password=pass dbname
2009-03-24 08:39:40
User: tristan_ph
Functions: cat
Tags: mysql cat
-1

Be aware of using the --password argument as it will appear your password in plain text on the screen. You may use -p argument instead, it will prompt you to enter you password in hidden mode.

cat file.txt | sort | uniq -dc
2009-03-21 18:15:14
User: Vadi
Functions: cat sort uniq
1

Displays the duplicated lines in a file and their occuring frequency.

cat /proc/acpi/battery/BAT1/info
2009-03-21 06:09:37
User: Vlad003
Functions: cat
Tags: bash battery
-1

Displays all information about your battery. for just capacity, try replacing cat with

grep -F capacity:

Battery number might be BAT0 instead of BAT1. Just run

cd /proc/acpi/battery; ls

and find out what folder is in that directory and replace that name with BAT1

for file in `find . -type f`; do cat $file; done | wc -l
for i in `find /sys/devices/*/*/usb* -name level` ; do echo -n "$i: " ; cat $i ; done
cat count.txt | awk '{ sum+=$1} END {print sum}'
2009-03-16 00:22:13
User: duxklr
Functions: awk cat
Tags: awk
15

Takes a input file (count.txt) that looks like:

1

2

3

4

5

It will add/sum the first column of numbers.

sudo cat /proc/kcore | strings | awk 'length > 20' | less
2009-03-09 02:19:47
User: nesquick
Functions: awk cat strings sudo
Tags: cat ram strings
15

This command lets you see and scroll through all of the strings that are stored in the RAM at any given time. Press space bar to scroll through to see more pages (or use the arrow keys etc).

Sometimes if you don't save that file that you were working on or want to get back something you closed it can be found floating around in here!

The awk command only shows lines that are longer than 20 characters (to avoid seeing lots of junk that probably isn't "human readable").

If you want to dump the whole thing to a file replace the final '| less' with '> memorydump'. This is great for searching through many times (and with the added bonus that it doesn't overwrite any memory...).

Here's a neat example to show up conversations that were had in pidgin (will probably work after it has been closed)...

sudo cat /proc/kcore | strings | grep '([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\})'

(depending on sudo settings it might be best to run

sudo su

first to get to a # prompt)

cat /path/to/7z.sfx /path/to/archive > archive.exe
2009-03-09 00:54:33
User: asmoore82
Functions: cat
4

using `cat` under *NIX - just because you help manage M$ Windoze

*doesn't* mean you should have to resort to using it!

You can also make custom win32 installers with the 7zip "extras" package:

cat /path/to/7zSD.sfx /path/to/config.txt /path/to/archive > setup.exe
cat <( command1 arg arg ) <( command2 arg ) ...
2009-03-07 04:33:12
User: Pistos
Functions: cat
2

Concatenate the stdout of multiple commands.