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 /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
16

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.

cat $(ls -tr | tail -1) | awk '{ a[$1] += 1; } END { for(i in a) printf("%d, %s\n", a[i], i ); }' | sort -n | tail -25
2009-03-06 17:50:29
User: oremj
Functions: awk cat ls sort tail
7

This command is much quicker than the alternative of "sort | uniq -c | sort -n".

logs=$(find . -name *.log);for log in $logs; do cat /dev/null > $log;done
2009-03-04 10:05:48
Functions: cat find
-5

This find files of name like *.log and truncates them.

watch -n 5 -d cat /proc/mdstat
gunzip -c /var/log/auth.log.*.gz | cat - /var/log/auth.log /var/log/auth.log.0 | grep "Invalid user" | awk '{print $8;}' | sort | uniq -c | less
awk -F "=| "
2009-03-02 21:09:51
User: Bender
Functions: awk cat file
9

You can use multiple field separators by separating them with | (=or).

This may be helpful when you want to split a string by two separators for example.

#echo "one=two three" | awk -F "=| " {'print $1, $3'}

one three

cat /dev/zero > /dev/null &
2009-03-02 18:18:52
User: gustavold
Functions: cat
-2

Just waste some resources in a philosophical way

cat file1.txt | uniq > file2.txt
$ cat /etc/*-release
cat /etc/*-release
2009-02-26 08:22:01
User: sudopeople
Functions: cat
10

Works for most distributions, tested on Ubuntu, Fedora, CentOS, Gentoo, SUSE, RedHat.

Debian and Slackware:

cat /etc/*version
cat /dev/tty > FILE
2009-02-25 01:43:47
User: Jo
Functions: cat
1

Takes input from the connected terminal and dumps it to the specified file. Stop writing and close file with control + D or the end of line character. Useful for copying+pasting large blobs of text over SSH to a new machine.

N="filepath" ; P=/proc/$(lsof +L1 | grep "$N" | awk '{print $2}')/fd ; ls -l $P | sed -rn "/$N/s/.*([0-9]+) ->.*/\1/p" | xargs -I_ cat $P/_ > "$N"
2009-02-21 02:31:24
User: laburu
Functions: awk cat grep ls sed xargs
5

Note that the file at the given path will have the contents of the (still) deleted file, but it is a new file with a new node number; in other words, this restores the data, but it does not actually "undelete" the old file.

I posted a function declaration encapsulating this functionality to http://www.reddit.com/r/programming/comments/7yx6f/how_to_undelete_any_open_deleted_file_in_linux/c07sqwe (please excuse the crap formatting).

mkfifo /tmp/fifo; cat /tmp/fifo | nc -l -p 1234 | tee -a to.log | nc machine port | tee -a from.log > /tmp/fifo
2009-02-20 04:00:15
User: prutser
Functions: cat mkfifo tee
26

Forwards localhost:1234 to machine:port, running all data through your chain of piped commands. The above command logs inbound and outbound traffic to two files.

Tip: replace tee with sed to manipulate the data in real time (use "sed -e 's/400 Bad Request/200 OK/'" to tweak a web server's responses ;-) Limitless possibilities.

check_disk -w 15% -c 10% $(for x in $(cat /proc/mounts |awk '{print $2}')\; do echo -n " -p $x "\; done)
cat foo.csv bar.csv | sort -t "," -k 2 | uniq
2009-02-19 20:23:03
User: rafeco
Functions: cat sort
2

The value for the sort command's -k argument is the column in the CSV file to sort on. In this example, it sorts on the second column. You must use some form of the sort command in order for uniq to work properly.

cat somefilehere.txt | ssh-xfer nametocallfile.txt -
2009-02-19 18:35:33
User: scubacuda
Functions: cat
0

from http://matt.ucc.asn.au/ssh-xfer/

"ssh-xfer is a hackish but handy way of transferring files from remote hosts to your local computer. Firstly, you need to run a slightly modified SSH authentication agent program on your local computer. Patches are available for both OpenSSH and PuTTY, see below. If you haven't used a SSH agent program before, this article seems to be reasonable, or you can look at the OpenSSH/PuTTY docs.

You don't need any modifications to your ssh client or server programs - only the modified SSH authentication agent, and the extra ssh-xfer program."

for i in $(cat listofservers.txt); do konsole --new-tab -e ssh $i; done
2009-02-19 16:32:36
User: nottings
Functions: cat ssh
4

creates a new tab for each of N servers in listofservers.txt and ssh's to said servers

then, try the "send to all sessions" feature of konsole to do the same work on all servers at the same time. BIG time saver, but be careful!

cat .ssh/id_dsa.pub | ssh elsewhere "[ -d .ssh ] || mkdir .ssh ; cat >> .ssh/authorized_keys"
2009-02-18 22:13:04
User: jsiren
Functions: cat ssh
5

Enter your ssh public key in the remote end for future key-based authentication. Just type your password one last time. The next time you should be able to login with the public key. If you don't have a key, generate one with ssh-keygen.

Requires Bourne-compatible shell in the remote end.

cat /dev/urandom > /dev/dsp
2009-02-18 21:40:29
User: joem86
Functions: cat
4

This command will generate white noise through your speakers (assuming you have sound enabled). It's good for staying focused, privacy, coping with tinnitus, etc. I use it to test that the sound works.