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 tr from sorted by
Terminal - Commands using tr - 265 results
nmap -sP 192.168.0.* | grep Host | tr "(" ")" | cut -d\) -f2
while true; do (date | tr "\n" ";") && ping -q -c 1 www.google.com|tail -1|cut -d/ -f5 ;sleep 1; done >> uptime.csv
2013-02-06 22:06:09
User: fanchok
Functions: cut date ping sleep tail tr
0

Used in OS X.

tr "\n" ";"

may be replaced by

echo ";"

with linux versions of date.

I reused

ping -q -c 1 www.google.com|tail -1|cut -d/ -f5

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.

ls -l /dev/disk/by-id | egrep ata-.*`hdparm -i /dev/sda | grep SerialNo | sed 's/.*SerialNo=//' | tr -d "\n"`.*sda$ | sed -e 's/.*ata-/ata-/' -e 's|[ ].*||' | tr -d "\n"
2013-01-07 10:20:25
Functions: egrep grep ls sed tr
Tags: Ubuntu
-2

This was tested on Ubuntu 12.04 (Precise) LTS Server. It returns the name of the symlink within /dev/disk/by-id for the physical drive you specify. Change /dev/sda to the one you want, and replace ata- with scsi- or the appropriate type for your drive.

I used this to pre-configure grub-pc during a non-interactive install because I had to tell it which disk to install grub on, and physical disks don't have a UUID such as that blkid provides.

tr -s ' \t' <1.txt >2.txt
2012-12-19 07:36:00
User: knoppix5
Functions: tr
2

Remove empty lines additionally:

tr -s ' \t\n' <1.txt >2.txt

identical with:

tr -s '[:space:]' <1.txt >2.txt

To "clean perfectly" a text or code file, You can combine this command with

another one:

while read l; do echo -e "$l"; done <1.txt >2.txt

(= remove all leading and trailing spaces or tabs from all lines of a text file)

export DISPLAY=$(tr '\000' '\n' < /proc/`pidof Xorg`/cmdline | egrep '^:[0-9]+')
2012-12-18 07:26:16
User: oernii2
Functions: egrep export tr
-2

Not always does Xorg run on :0. For times like those, this script allows you to find out which it is.

dig @resolver1.opendns.com myip.opendns.com | grep ^myip.opendns.com | tr '\t' : | cut -d: -f5
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
tr -dc '[:print:]' < <file>
2012-11-22 06:29:26
User: seb1245
Functions: tr
Tags: printing tr
3

tr has some predefined sets of characters that are more convenient to use than characters codes

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.

tr -d '\r' < <input> > <output>
echo "your string here" | tr '\!-~' 'P-~\!-O'
2012-11-06 12:49:00
User: DevEd
Functions: echo tr
1

This command will encode a string using the ROT47 cipher.

java -jar app.jar -cp $(echo lib/*.jar | tr ' ' ':')
2012-10-29 12:51:46
User: xymor
Functions: echo tr
Tags: java cli
0

Lauching an app including jars in an adjacent lib folder to its classpath

tr -dc '[:graph:]' </dev/urandom | head -c30; echo
head -c7 /dev/urandom|base64|tr -d '='
tr -dc A-Za-z0-9_ < /dev/urandom | head -c 10 | xargs
echo $(</dev/urandom tr -dc 1-6 | head -c1)
echo $(find <directory> -name '*.<extension>' -exec du -s {} \; | tee $(tty) | cut -f1 | tr '\n' '+') 0 | bc
2012-09-17 22:46:50
User: ysangkok
Functions: cut du echo find tee tr
-1

Also shows files as they are found. Only works from a tty.

tcpdump -ntr NAME_OF_CAPTURED_FILE.pcap 'tcp[13] = 0x02 and dst port 80' | awk '{print $4}' | tr . ' ' | awk '{print $1"."$2"."$3"."$4}' | sort | uniq -c | awk ' {print $2 "\t" $1 }'
lsof -p `pidof pidgin` | awk '{ print $9 }'|egrep `hostname` | grep -o ">[^:]\+:" | tr -d ":>" | while read line; do host $line; done;
pbpaste | tr '\r\n' '\n' | tr '\r' '\n' | pbcopy
2012-07-31 10:50:14
User: adambrenecki
Functions: tr
0

Copying and pasting from Office documents open in Office:mac can dirty your files with Windows CRLF and (inexplicably) Classic Mac OS LF newlines, which can break some tools. This snippet replaces them with good ol' Unix LF newlines.

export PATH= $(echo $PATH | tr ':' '\n' | awk '!/matching string/' | paste -sd:)
ssh-keygen -l -f [pubkey] | cut -d ' ' -f 2 | tr -ds '\n:' ''
tr -s ' '
2012-06-26 19:34:18
User: ankush108
Functions: tr
Tags: tr
0

Convert long list of ' ' to a single space. Compress space and other characters.

tr a-z A-Z < file.txt