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 - 267 results
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
for file in * ; do mv "$file" `echo "$file" | tr ' ' '_' | tr '[A-Z]' '[a-z]'`; done
2012-05-06 17:54:06
User: cengztr
Functions: file mv tr
0

All files in the directory will be renamed replacing every space in the filename by "_" (underline) and converting upper case characters to lower case characters.

e.g. Foo Bar.txt --> foo_bar.txt

printf "%$(tput cols)s\n"|tr ' ' '='
2012-04-21 23:26:55
Functions: printf tr
9

Use tput cols to find the width of the terminal and set it as the minimum field width.

dd if=/dev/zero bs=64K count=1 | tr "\0" "\377" > all_ones
2012-04-17 18:01:59
User: anduril462
Functions: dd tr
1

dd can be used with /dev/zero to easily create a file of all zero-bytes. Pipe that through tr and use octal conversions to change the byte values from zero to 0xff (octal 0377). You can replace 0377 with the byte of your choice. You can also use \\0 and \\377 instead of the quoted version.

cal | egrep -e '^ [0-9]|^[0-9]' | tr '\n' ' ' | awk '{print $NF}'
2012-04-16 11:58:09
User: gouverney
Functions: awk cal egrep tr
0

Returns last day of current month. Useful to implement a bash script backup based on a GFS strategy.

paste <(seq 7 | shuf | tr 1-7 A-G) <(seq 7 | shuf) | while read i j; do play -qn synth 1 pluck $i synth 1 pluck mix $2; done
dng(){ local a;a=$(sed '/'"$1"'/!d' /etc/hosts |sed '=;'"${2-1,$}"'!d'|sed '/ /!d');echo $a|tr '\040' '\n'|nl -bp'[0-9]$'|less -E;export dn=$(echo $a|sed 's,.* ,,');export ip=$(echo $a|sed 's, .*,,');echo \$dn=$dn;echo \$ip=$ip;}
2012-04-01 23:57:09
User: argv
Functions: echo export less nl sed tr
0

usage: dng BRE [selection]

default selection is the last match

DNS is ok, but although domainnames may be easier to remember than IP numbers, it still requires typing them out. This can be error-prone. Even more so than typing IPv4 numbers, depending on the domainname, its length and complexity.

find /some/path -type f -and -iregex '.*\.mp3$' -and -print0 | tr -d -c '\000' |wc -c
2012-03-31 21:57:33
User: kyle0r
Functions: find tr wc
0

In this example, the command will recursively find files (-type f) under /some/path, where the path ends in .mp3, case insensitive (-iregex).

It will then output a single line of output (-print0), with results terminated by a the null character (octal 000). Suitable for piping to xargs -0. This type of output avoids issues with garbage in paths, like unclosed quotes.

The tr command then strips away everything but the null chars, finally piping to wc -c, to get a character count.

I have found this very useful, to verify one is getting the right number of before you actually process the results through xargs or similar. Yes, one can issue the find without the -print0 and use wc -l, however if you want to be 1000% sure your find command is giving you the expected number of results, this is a simple way to check.

The approach can be made in to a function and then included in .bashrc or similar. e.g.

count_chars() { tr -d -c "$1" | wc -c; }

In this form it provides a versatile character counter of text streams :)

scanelf --nobanner --recursive --quiet --needed --format "+n#F" $1 | tr ',' '\n' | sort -u
2012-03-29 18:30:45
User: Flameeyes
Functions: sort tr
1

This works in combination with http://www.commandlinefu.com/commands/view/10496/identify-exported-sonames-in-a-path as it reports the NEEDED entries present in the files within a given path. You can then compare it with the libraries that are exported to make sure that, when cross-building a firmware image, you're not bringing in dependencies from the build host.

The short version of it as can be seen in the same output is

scanelf -RBnq -F "+n#f" $1 | tr ',' '\n' | sort -u
for w in $(tr 'A-Z ,."()?!;:' 'a-z\n' < sample.txt); do echo ${#w} $w; done | sort -u | sort -n
2012-03-15 14:14:11
User: flatcap
Functions: echo sort tr
Tags: bash sort tr
0

Take a file and ,."()?!;: give a list of all the words in order of increasing length.

First of all use tr to map all alphabetic characters to lower case and also strip out any puntuation.

A-Z become a-z

,."()?!;: all become \n (newline)

I've ignored - (hyphen) and ' (apostrophe) because they occur in words.

Next use bash to print the length ${#w} and the word

Finally sort the list numerically (sort -n) and remove any duplicates (sort -u).

Note: sort -nu performs strangely on this list. It outputs one word per length.

echo "Click a window to start recording"; read x y W H <<< `xwininfo | grep -e Width -e Height -e Absolute | grep -oE "[[:digit:]]{1,}" | tr "\n" " "`; ffmpeg -f alsa -ac 1 -i pulse -f x11grab -s ${W}x${H} -r 25 -i :0.0+${x},${y} -sameq output.mkv
2012-03-14 19:42:28
User: joseCanciani
Functions: echo grep read tr
1

The script gets the dimensions and position of a window and calls ffmpeg to record audio and video of that window. It saves it to a file named output.mkv

tr a-zA-Z A-Za-z < input.txt
tr -d '\r' <dos_file_to_be_converted >converted_result
2012-02-29 22:43:26
Functions: tr
Tags: tr dos CR
1

just deletes to rogue CR from dos files, and tr is always available.

tr ' ' '\n' < <filename> > <output>
lsof -n -P|grep FlashXX|awk '{ print "/proc/" $2 "/fd/" substr($4, 1, length($4)-1) }'|while read f;do newname=$(exiftool -FileModifyDate -FileType -t -d %Y%m%d%H%M%S $f|cut -f2|tr '\n' '.'|sed 's/\.$//');echo "$f -> $newname";cp $f ~/Vids/$newname;done
2012-02-25 01:49:45
User: mhs
Functions: awk cp cut echo grep read sed tr
8

Certain Flash video players (e.g. Youtube) write their video streams to disk in /tmp/ , but the files are unlinked. i.e. the player creates the file and then immediately deletes the filename (unlinking files in this way makes it hard to find them, and/or ensures their cleanup if the browser or plugin should crash etc.) But as long as the flash plugin's process runs, a file descriptor remains in its /proc/ hierarchy, from which we (and the player) still have access to the file. The method above worked nicely for me when I had 50 tabs open with Youtube videos and didn't want to have to re-download them all with some tool.

ls|grep .mp3 >list.txt; while read line; do newname=`echo $line|sed 's/\ /-/g'|sort`; newname=`echo $newname|tr -s '-' `; echo $newname; echo $newname>> tracklist.txt;mv "$line" "$newname"; done <list.txt; rm list.txt
genRandomText() { cat /dev/urandom|tr -dc 'a-zA-Z'|head -c $1 }
2012-01-21 00:51:34
User: thomasba
Functions: cat head tr
Tags: random urandom
0

Using urandom to get random data, deleting non-letters with tr and print the first $1 bytes.

pwd | tr -d '\n' | xsel -b
cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 32
tr -d '\\' | tr -d '\n'
cat table-mv.txt | perl -pe 's{([^;]+);([^;]+)}{tbl$1/tbl$2}' | perl -pe 's{(\S+)}{perl -i -pe #s/$1/g# xxx.sql}' | tr "#" "\'" | bash
2011-10-05 15:55:34
User: hute37
Functions: cat perl tr
0

with a semicolon text file map, apply multiple replace to a single file

ps ewwo command PID | tr ' ' '\n' | grep \=
cat /proc/PID/environ | tr '\0' '\n'
echo 'fOo BaR' | tr '[A-Z]' '[a-z]' | sed 's/\(^\| \)\([a-z]\)/\1\u\2/g'