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 read from sorted by
Terminal - Commands using read - 288 results
get_duration () { IFS=.: read -r _ h m s _ < <(ffmpeg -i "$1" 2>&1 | grep Duration);echo $(( h * 3600 + m * 60 + s )); }
SCALE=3; WIDTHL=10; WIDTHR=60; BAR="12345678"; BAR="${BAR//?/==========}"; while read LEFT RIGHT rest ; do RIGHT=$((RIGHT/SCALE)); printf "%${WIDTHL}s: %-${WIDTHR}s\n" "${LEFT:0:$WIDTHL}" "|${BAR:0:$RIGHT}*"; done < dataset.dat
2011-08-22 19:35:21
User: andreasS
Functions: printf read
0

WIDTHL=10 and WIDTHR=60 are setting the widths of the left and the right column/bar. BAR="12345678" etc. is used to create a 80 char long string of "="s. I didn't know any shorter way.

If you want to pipe results into it, wrap the whole thing in ( ... )

I know that printing bar graphs can be done rather easily by other means. Here, I was looking for a Bash only variant.

while IFS= read -r -u3 -d $'\0' file; do file "$file" | egrep -q 'executable|ELF' && chmod +x "$file"; done 3< <(find . -type f -print0)
2011-08-18 15:37:23
User: keymon
Functions: chmod egrep file find read
0

If you make a mess (like I did) and you removed all the executable permissions of a directory (or you set executable permissions to everything) this can help.

It supports spaces and other special characters in the file paths, but it will work only in bash, GNU find and GNU egrep.

You can complement it with these two commands:

1. add executable permission to directories:

find . type d -print0 | xargs -0 chmod +x

2. and remove to files:

find . type d -print0 | xargs -0 chmod -x

Or, in the same loop:

while IFS= read -r -u3 -d $'\0' file; do case $(file "$file" | cut -f 2- -d :) in :*executable*|*ELF*|*directory*) chmod +x "$file" ;; *) chmod -x "$file" ;; esac || break done 3< <(find . -print0)

Ideas stolen from Greg's wiki: http://mywiki.wooledge.org/BashFAQ/020

git branch | cut -c3- | grep -v "^master$" | while read line; do git branch -d $line; done | grep 'Deleted branch' | awk '{print $3;}' | while read line; do git push <target_remote> :$line; done
2011-08-13 16:58:34
User: gocoogs
Functions: awk cut grep read
0

attempts to delete all local branches. git will fail on any branches not fully merged into local master, so don't worry about losing work. git will return the names of any successfully deleted branches. Find those in the output with grep, then push null repositories to the corresponding names to your target remote.

assumes:

- your local and remote branches are identically named, and there's nothing extra in the remote branch that you still want

- EDIT: you want to keep your local master branch

find . -type l | (while read FN ; do test -e "$FN" || ls -ld "$FN"; done)
read day month year < <(date +'%d %m %y')
2011-07-30 06:06:29
User: frans
Functions: date read
Tags: bash read
10

No command substitution but subshell redirection

read day month year <<< $(date +'%d %m %y')
read VAR1 VAR2 VAR3 <<< aa bb cc; echo $VAR2
find src/ -name "*.java" | while read f; do echo -n "$f "; cat "$f" | tr -dc '{}'; echo; done | awk '{ print length($2), $1 }' | sort -n
read -s -p"Password: " USER_PASSWORD_VARIABLE; echo
2011-07-20 20:22:42
User: h3nr1x
Functions: read
Tags: read
14

You can ask repeatedly for a non-blank password using this function:

function read_password() {

while [ ! -n "$USER_PASSWORD" ]; do

read -s -p"Password: " USER_PASSWORD

if [ ! -n "$USER_PASSWORD" ]; then

echo "ERROR: You must specify a valid password, please try again"

fi

echo

done

}

Also you can set a time out (in seconds) to write the password

read -t 10 -s -p"Password: " USER_PASSWORD_VARIABLE

if [ ! $? -eq 0 ]; then

echo "Time out!"

fi

tail -n0 -f /var/log/messages | while read line; do notify-send "System Message" "$line"; done
2011-07-11 22:33:24
User: hukketto
Functions: read tail
Tags: notify-send
1

It willl popup a message for each new entry in /var/log/messages

found on the notify-send howto page on ubuntuforums.org.

Posted here only because it is one of the favourites of mine.

find ./ $1 -name "* *" | while read a ; do mv "${a}" "${a//\ /_}" ; done
while ping -c 1 127.0.0.1 > /dev/null; do acpi -t -f | while read tem; do notify-send "$tem"; done; sleep 300; done
2011-07-02 06:47:25
User: c0de
Functions: acpi ping read sleep
2

works best in a shell script run at startup. It will ping localhost once and output to null, after it does that, acpi is called for temperature in fahrenheit and piped through to another loop that feeds notify-send for a tooltip. After waiting five minutes, it will start over.

fdupes -r -1 path | while read line; do j="0"; for file in ${line[*]}; do if [ "$j" == "0" ]; then j="1"; else ln -f ${line// .*/} $file; fi; done; done
find . | grep ".*\[[Church|CPYAF].*" | while read f; do mv "$f" ../emails;done
read -t 0.1 -N 255
ls * | while read fin;do fout=$(echo -n $fin | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e);if [ "$fout" != "$fin" ];then echo "mv '$fin' '$fout'";fi;done | bash -x
2011-05-18 07:24:54
User: pawelb1973
Functions: bash echo ls read sed xargs
0

urldecode files in current directrory

cat search_items.txt | while read i; do surfraw google -browser=firefox $i; done
2011-05-12 09:27:08
User: bubo
Functions: cat read
-1

tired of opening tabs and fill in search forms by hand? just pipe the search terms you need into this surfraw loop. you can use any browser you have installed, but a graphical browser with a tabbed interface will come in handy. surfraw can be found here:

http://surfraw.alioth.debian.org

while :; do sensors|grep ^Core|while read x; do printf '% .23s\n' "$x"; done; sleep 1 && clear; done;
2011-04-20 06:41:57
Functions: grep printf read sleep
1

Watch the temperatures of your CPU cores in real time at the command line. Press CONTROL+C to end.

GORY DETAILS: Your computer needs to support sensors (many laptops, for example, do not). You'll need to install the lm-sensors package if it isn't already installed. And it helps to run the `sensors-detect` command to set up your sensor kernel modules first. At the very end of the sensors-detect interactive shell prompt, answer YES to add the new lines to the list of kernel modules loaded at boot.

find $PWD -type d | while read "D"; do cd "$D"; for filename in *.epub;do ebook-convert "$filename" "${filename%.epub}.mobi" --prefer-author-sort --output-profile=kindle --linearize-tables --smarten-punctuation --asciiize;done ;done
2011-04-19 15:51:50
User: rsimpson
Functions: cd find read
0

finds all epub files in the current directory and all child directories and converts them to .mobi format.

all of the ebook-convert -options are optional; the only parameters you are required to pass are the incoming file and the outgoing file, with the extension.

Has been tested on Ubuntu 10.10

find . -type f -name \*.php | while IFS="" read i; do expand -t4 "$i" > "$i-"; mv "$i-" "$i"; done
2011-04-08 12:53:14
User: flatcap
Functions: expand find mv read
-4

Recursively find php files and replace tab characters with spaces.

Options:

"\*.php" -- replace this with the files you wish to find

"expand" -- replace tabs with spaces (use "unexpand" to replace spaces with tabs)

"-t4" -- tabs represent 4 spaces

Note: The IFS="" in the middle is to prevent 'read' from eating leading/trailing whitespace in filenames.

LANG=fr_FR.iso8859-1 find . -name '*['$'\xe9'$'\xea'$'\xeb'$'\xc9'']*'|while read f; do a="$(echo $f|iconv -f iso8859-1 -t ascii//TRANSLIT)"; echo "move $f => $a"; done
2011-04-06 17:03:31
User: gibboris
Functions: echo find read
-1

Warn: use convmv or detox if you can: they are the right tools.

But if you want to do it manually, you can use this command to find the problematic files and transliterate their accented characters to their ascii equivalent.

(Useful when doing cd backup: growisofs may fail on files which come from the old iso8859-* days.)

se(){ while read a;do [ "$a" != "${a#*$@*}" ]&&echo $a;done ;} # usage: se pattern # use in place of sed /pat/!d where RE are overkill
2011-04-06 03:37:40
User: argv
Functions: read sed
1

POSIX requires this "string truncating" functionality.

might as well use it, at least for very small tasks where invoking sed and using RE is overkill.

lynx --dump http://www.seeon.tv/channels| grep "/channels"|awk '{print $2}'|sort -u|while read links; do lynx --dump "$links"|awk '/view/ {print $2}'|sort -u; done
2011-04-01 05:58:20
User: Bonster
Functions: awk grep read sort
-1

This shows a list of channels from seeon.tv website to watch shows and movies

df -h | grep -v ^none | ( read header ; echo "$header" ; sort -rn -k 5)
2011-03-16 14:25:45
User: purpleturtle
Functions: df echo grep read sort
Tags: sort headers df
0

Show disk space info, grepping out the uninteresting ones beginning with ^none while we're at it.

The main point of this submission is the way it maintains the header row with the command grouping, by removing it from the pipeline before it gets fed into the sort command. (I'm surprised sort doesn't have an option to skip a header row, actually..)

It took me a while to work out how to do this, I thought of it as I was drifting off to sleep last night!