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 - 274 results
for i in ./*.$1; do mv "$i" `echo $i | tr ' ' '_'`; done for i in ./*.$1; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done for i in ./*.$1; do mv "$i" `echo $i | tr '-' '_'`; done for i in ./*.$1; do mv "$i" `echo $i | tr -s '_' `; done
echo `lcg-infosites --vo lhcb ce | cut -f 1| grep [[:digit:]]| tr '\n' '+' |sed -e 's/\ //g' -e 's/+$//'`|bc -l
2010-11-10 15:06:00
User: kbat
Functions: bc cut echo grep sed tr
-2

Of course, this command must be executed at a GRID User Interface

lhcb - name of your VO, substitute it with the one you are interested it.

od -N 4 -t uL -An /dev/random | tr -d " "
2010-11-09 07:57:16
User: hfs
Functions: od tr
Tags: random
2

Reads 4 bytes from the random device and formats them as unsigned integer between 0 and 2^32-1.

for f in * ; do mv "$f" $( echo $f | tr ' ' '-' ) ; done
tr -cs A-Za-z '\n' | sort | uniq -ci
2010-10-20 04:12:58
Functions: sort tr uniq
Tags: sort uniq tr
0

Gives the same results as the command by putnamhill using nine less characters.

tr A-Z a-z | tr -cs a-z '\n' | sort | uniq -c
tr A-Z a-z | tr -d "[[:punct:]][[:digit:]]" | tr ' /_' '\n' | sort | uniq -c
tr A-Z a-z | tr -d 0-9\[\],\*-.?\:\"\(\)#\;\<\>\@ | tr ' /_' '\n' | sort | uniq -c
curl -s $dellurl$1 | tr "\"" "\n" | grep "</td></tr><tr><td class=" -m 2 | grep -v "Service Tag" | sed 's/>//g' | sed 's/<\/td<\/tr<tr<td class=//g'
du --max-depth=1|sort -n|cut -f2|tr '\n' '\0'|xargs -0 du -sh 2>/dev/null
wget -qO - http://i18n.counter.li.org/ | grep 'users registered' | sed 's/.*\<font size=7\>//g' | tr '\>' ' ' | sed 's/<br.*//g' | tr ' ' '\0'
tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1
netstat -n | grep '^tcp.*<IP>:<PORT>' | tr " " | awk 'BEGIN{FS="( |:)"}{print $6}' | sort | uniq -c | sort -n -k1 | awk '{if ($1 >= 10){print $2}}'
2010-09-16 21:06:30
User: guptavi
Functions: awk grep netstat sort tr uniq
1

This command is primarily going to work on linux boxes.

and needs to be changed, for example

IP=10\.194\.194\.2

PORT=389

tr '\000' '\377' < /dev/zero | dd bs=512 count=200000 status=noxfer | pipebench | sudo dd of=/dev/sdx
2010-08-31 15:38:27
User: Gliktch
Functions: dd sudo tr
3

Note: Replace 200000 with drive bytes/512, and /dev/sdx with the destination drive/partition. ;)

Note: You may need to install pipebench, this is easy with "sudo apt-get install pipebench" on Ubuntu.

The reason I hunted around for the pieces to make up this command is that I wanted to specifically flip all of the bits on a new HDD, before running an Extended SMART Self-Test (actually, the second pass, as I've already done one while factory-zeroed) to ensure there are no physical faults waiting to compromise my valuable data. There were several sites that came up in a Google search which had a zero-fill command with progress indicator, and one or two with a fill-with-ones command, but none that I could find with these two things combined (I had to shuffle around the dd command(s) to get this to happen without wasting speed on an md5sum as well).

For reference, these are the other useful-looking commands I found in my search:

Zero-fill drive "/dev/sdx", with progress indicator and md5 verification (run sudo fdisk -l to get total disk bytes, then divide by 512 and enter the resulting value into this command for a full wipe)

dd if=/dev/zero bs=512 count=<size/512> | pipebench | sudo tee /dev/sdx | md5sum

And this command for creating a file filled with ones is my other main source (besides the above command and man pages, that is - I may be a Linux newbie but I do read!):

tr '\000' '\377' < /dev/zero | dd of=allones bs=1024 count=2k

Hope someone finds this useful! :)

Cheers,

- Gliktch

echo "tee can split a pipe in two"|tee >(rev) >(tr ' ' '_')
2010-08-14 20:38:59
User: axelabs
Functions: echo tee tr
33

Tee can be used to split a pipe into multiple streams for one or more process to work it. You can add more " >()" for even more fun.

ls | while read -r FILE; do mv -v "$FILE" `echo $FILE | tr -d ' '`; done
2010-08-14 14:10:48
User: IgnitionWeb
Functions: ls mv read tr
Tags: space echo while tr
-3

all files in the directory get moved, in doing so the new name of the file is the original name with out spaces (using translate command)

echo 'K5B!C%@NC[4\CMK54(C^)7PP)7}$RVPNE-FGNAQNEQ-NAGVIVEHF-GRFG-SVYR!$U+U*' | tr '[A-Za-z]' '[N-ZA-Mn-za-m]' > /tmp/eicar.com
2010-08-13 21:39:35
User: cyberscribe
Functions: echo tr
1

Test whether real-time virus detection is working by running this command and checking for eicar.com in /tmp. Requires real-time scanning to be enabled and active on the /tmp directory. If scanning is active, the file should be quarantined/deleted (depending on your settings) moments after running this command. If not, the (harmless) test file should remain in your /tmp directory.

echo "hello::::there" | tr -s ':'
2010-08-12 16:23:06
User: randy909
Functions: echo tr
1

This can be particularly useful used in conjunction with a following cut command like

echo "hello::::there" | tr -s ':' | cut -d':' -f2

which prints 'there'. Much easier that guessing at -f values for cut. I know 'tr -s' is used in lots of commands here already but I just figured out the -s flag and thought it deserved to be highlighted :)

echo StrinG | tr 'A-Z' 'a-z'
2010-08-12 15:42:56
User: randy909
Functions: echo tr
0

Another way to do it with slightly fewer characters. It doesn't work on Russian characters; please don't vote down because of that. :p It's very handy for those of us working in ascii :)

netstat -rn | grep UG | tr -s " " | cut -d" " -f2
TTY=$(tty | cut -c 6-);who | grep "$TTY " | awk '{print $6}' | tr -d '()'
curl -s http://whatthecommit.com/ | tr -s '\n' ' ' | grep -so 'p>\(.*\)</p' | sed -n 's/..\(.*\)..../\1/p'
2010-08-02 03:20:32
User: sragu
Functions: grep sed tr
-2

#Sample Usage:

# git commit -m"Jira #404 - `whatthecommit`"

#

printf $(( echo "obase=16;$(echo $$$(date +%s%N))"|bc; ip link show|sed -n '/eth/ {N; p}'|grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'|head -c 17 )|tr -d [:space:][:punct:] |sed 's/[[:xdigit:]]\{2\}/\\x&/g')|sha1sum|head -c 32; echo
2010-07-14 14:04:53
User: camocrazed
Functions: echo grep head link printf sed tr
Tags: uuid
0

first off, if you just want a random UUID, here's the actual command to use:

uuidgen

Your chances of finding a duplicate after running this nonstop for a year are about the same as being hit by a meteorite before finishing this sentence

The reason for the command I have is that it's more provably unique than the one that uuidgen creates. uuidgen creates a random one by default, or an unencrypted one based on time and network address if you give it the -t option.

Mine uses the mac address of the ethernet interface, the process id of the caller, and the system time down to nanosecond resolution, which is provably unique over all computers past, present, and future, subject to collisions in the cryptographic hash used, and the uniqueness of your mac address.

Warning: feel free to experiment, but be warned that the stdin of the hash is binary data at that point, which may mess up your terminal if you don't pipe it into something. If it does mess up though, just type

reset
objdump -d ./PROGRAM|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
qlist --exact "$pkg" | sudo scanelf --needed --quiet --format '%n#F' | tr ',' '\n' | sort -u | qfile --from -
2010-07-06 14:39:15
User: Flameeyes
Functions: sort sudo tr
2

The output is only partial because runtime dependencies should count in also commands executed via system() and libraries loaded with dlopen(), but at least it gives an idea of what a package directly links to.

Note: this is meaningful *only* if you're using -Wl,--as-needed in your LDFLAGS, otherwise it'll bring you a bunch of false positives.