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 - 282 results
find -maxdepth 3 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" | wc -l; done
2012-10-15 15:00:09
User: brainstorm
Functions: find printf read wc
1

Counts the files present in the different directories recursively. One only has to change maxdepth to have further insight in the directory hierarchy.

Found at unix.stackexchange.com:

http://unix.stackexchange.com/questions/4105/how-do-i-count-all-the-files-recursively-through-directories

cat /tmp/commit_list | { while read old_commit ; do msg="`git log --pretty=oneline $old_commit'^'..$old_commit | sed 's/[0-9a-f]* //' | sed 's/[^A-Za-z0-9]/./g'`"; git log --pretty=oneline HEAD@'{100}'..HEAD | grep "$msg" ; done ; }
2012-10-11 11:06:40
User: plexus
Functions: cat grep read
0

Given a file with the format of 'git log --pretty=short', search in last 100 commits for one with the same description. I used this when after a rebase I had to find out the new commit ids. The second sed replaces all special characters with dots so they don't mess up the grep later on.

alias ls='if [[ -f .hidden ]]; then while read l; do opts+=(--hide="$l"); done < .hidden; fi; ls --color=auto "${opts[@]}"'
2012-08-12 13:10:23
User: expelledboy
Functions: alias ls read
Tags: hidden ls alias
1

Sometimes I would like to see hidden files, prefix with a period, but some files or folders I never want to see (and really wish I could just remove all together).

lsof -p `pidof pidgin` | awk '{ print $9 }'|egrep `hostname` | grep -o ">[^:]\+:" | tr -d ":>" | while read line; do host $line; done;
ls *.png | while read line; do pngcrush -brute $line compressed/$line; done
2012-07-17 20:20:49
User: waffleboi9
Functions: ls read
0

Nothing too magical here, just uses pngcrush to losslessly compress all your pngs!

svn status | grep "^M" | while read entry; do file=`echo $entry | awk '{print $2}'`; echo $file; svn revert $file; done
2012-06-17 16:01:06
User: wsams
Functions: awk echo grep read
0

This command allows you to revert every modified file one-by-one in a while loop, but also after "echo $file;" you can do any sort of processing you might want to add before the revert happens.

read -s p && echo -n $p | md5sum;p=
2012-06-08 13:50:50
User: hoodie
Functions: echo read
1

create and md5 sum of your password without it showing up in your terminal or history.

Afterwards we overwrite the $p variable (thx to bazzargh)

#!/bin/zsh SHL='\\e[0;31m' EHL='\\e[0m' while read line; do TEXT=$line for SSTR in $*; do TEXT=$(echo $TEXT | sed -e "s:$SSTR:${SHL}${SSTR}${EHL}:g") done echo -e $TEXT done
find /proc/sys/vm -maxdepth 1 -type f | while read i ; do printf "%-35s\t%s\n" "$i" "$(<$i)" ; done | sort -t/ -k4
2012-05-25 16:34:16
User: SEJeff
Functions: find printf read sort
0

Sometimes you want to see all of the systcls for a given $thing. I happened to need to easily look at all of the vm sysctls between two boxes and compare them. This is what I came up with.

members () { dscl . -list /Users | while read user; do printf "$user "; dsmemberutil checkmembership -U "$user" -G "$*"; done | grep "is a member" | cut -d " " -f 1; };
2012-05-20 11:34:33
User: eduo
Functions: cut grep printf read
-1

Group membership in OS X is a mish-mash of standards that end up meaning there's almost a half-dozen of ways to belong to a group, what with group inheritance and automatic assignment. This means there's no easy command to find out all groups a user belongs to. The only sensible way then is to list all users and then query each user for membership.

NOTE: This is a function. Once input you can execute it by calling with a groupname.

while true; do sudo cat /dev/input/mouse0|read -n1;streamer -q -o /tmp/cam.jpeg -s 640x480 > /dev/null 2>&1; sleep 10;done
2012-04-22 01:51:30
User: SQUIIDUX
Functions: cat read sleep sudo
Tags: cat streamer
4

This takes a webcam picture every everytime the mouse is moved (waits 10 seconds between checking for movement) and stores the picture wherever you want it.

Ideas:

Use in conjunction with a dropbox type application to see who is using your computer

Use /dev/input/mice if /dev/input/mouse* doesn't work

Use the bones of this to make a simple screensaver

read -p "Question that you want an answer to?" yn
jobs | grep -o "[0-9]" | while read j; do kill %$j; done
2012-04-12 17:29:58
User: haggen
Functions: grep jobs kill read
0

List background jobs, grep their number - not process id - and then kill them

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
yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done
_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:46:19
User: argv
Functions: grep printf ps read sed
0

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?

_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:45:17
User: argv
Functions: grep printf ps read sed
0

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?

svnll(){svn log "$@"|( read; while true; do read h||break; read; m=""; while read l; do echo "$l" | grep -q '^[-]\+$'&&break; [ -z "$m" ] && m=$l; done; echo "$h % $m" | sed 's#\(.*\) | \(.*\) | \([-0-9 :]\{16\}\).* % \(.*\)#\1 \2 (\3) \4#'; done)}
2012-03-25 20:39:05
User: vhotspur
Functions: echo grep read sed
Tags: log subversion
0

Emulate (more or less) Git equivalent of

git log --format='tformat:%h %an (%cr) %s'
find . -iname '*.zip' | while read file; do unzip -l "$file" | grep -q [internal file name] && echo $file; done
2012-03-23 18:08:35
User: ricardofunke
Functions: echo file find grep read
1

This command find which of your zip (or jar) files (when you have lots of them) contains a file you're searching for. It's useful when you have a lot of zip (or jar) files and need to know in which of them the file is archived.

It's most common with .jar files when you have to know which of the .jar files contains the java class you need.

To find in jar files, you must change "zip" to "jar" in the "find" command. The [internal file name] must be changed to the file name you're searching that is archived into one of the zip/jar files.

Before run this command you must step into the directory that contains the zip or jar files.

lsgrp() { read GID USERS <<< "$(grep "^$1:" /etc/group | cut -d: -f3,4 | tr ':,' ' ')" ; echo -e "${USERS// /\n}" | egrep -v "^($1)?$" ; egrep :[0-9]+:$GID: /etc/passwd | cut -d: -f1 ; }
2012-03-16 09:57:33
User: livibetter
Functions: cut echo egrep read
0

I can't find the lid command on my system, there is also another complied program: http://xyne.archlinux.ca/projects/lsgrp/

seq 1 1000000 | while read i; do echo -en "\r$i"; done
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

while read l; do echo -e "$l"; done <1.txt >2.txt
2012-03-13 14:27:49
User: knoppix5
Functions: echo read
Tags: bash read
5

Bash only, no sed, no awk. Multiple spaces/tabs if exists INSIDE the line will be preserved. Empty lines stay intact, except they will be cleaned from spaces and tabs if any available.

mysql -BNe "SELECT id FROM processlist WHERE user = 'redmine';" information_schema | while read id; do mysqladmin kill $id; done
2012-03-09 17:37:23
User: anarcat
Functions: kill read
Tags: mysql kill BOFH
0

Kills all the threads from the user provided in the WHERE request.

Can be refined through the SQL request, of course, see http://dev.mysql.com/doc/refman/5.1/en/processlist-table.html for the available columns.

for i in $(seq 1 20); do while read line; do echo "$i: $line"; done<$i.py; done