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

All commands from sorted by
Terminal - All commands - 12,000 results
ssh-keygen -t rsa -b 4096 -f ~/.ssh/<ROLE>_rsa -C "Comment goes here"
2015-01-11 17:52:21
User: krizzo
Functions: ssh ssh-keygen
Tags: ssh-keygen
0

This is just a default ssh-keygen command. Prompts for a password that you can use to secure the keys more and uses a higher bit value than the default along with naming the key something other than id_rsa for better file identification.

avconv -i ka-ching.flv -acodec copy -vcodec copy ka-ching.mkv
printf "\e[7m%-`tput cols`s\e[0m\n" "Full width highlighted line"
2015-01-08 16:17:43
User: fr00tyl00p
Functions: printf
1

Show a full terminal line inverted with custom text.

zip -r /tmp/filename-`date +%Y%m%d_%H%M%S`.zip /directory/
find . -type f -exec echo -n "touch -t \`echo " \; -exec echo -n {} \; -exec echo -n " | sed -E 's/.*([[:digit:]]{8})_([[:digit:]]{4})([[:digit:]]{2}).*/\1\2.\3/g'\` " \; -exec echo {} \; | sh
clear;while true;sleep 1;do for((a=1;a<=$(tput cols)/3;a++));do tput cup 0 $a;echo " " $(date);done;sleep 1;for((a;a>=1;a--));do tput cup 0 $a;echo $(date) " ";done;done
cd(), do a ls (or whatever you can imagine) after a cd, func to long please refer to description
2015-01-01 20:50:19
User: Xk2c
Functions: cd ls
-6

some people on the net already use a cd(), but most of them break 'cd -' functionality,

that is "go back where you have been previosly", or 'cd' which is "go back home".

This cd() copes with that. Also when given a file name, go to the directory where this file is in.

cd()

{

if [[ -n ${*} ]]

then

if [[ s${*}e == s-e ]]

then

builtin cd -

elif [[ ! -d ${*} ]]

then

builtin cd "${*%/*}"

else

builtin cd "${*}"

fi

else

builtin cd ~

fi

ls -la

}

[ ~/temp/foo/bar/baz ] $ .. 3
2015-01-01 20:41:17
User: Xk2c
-4

many have aliases like:

alias ...="cd ../../"

alias ....="cd ../../../"

and so furth.

..() mitigates to need for those aliases, see sample output for an example

# .. -> go up 1 directory

# .. 4 -> go up 4 directories

..()

{

local DIR=''

declare -i NUM=0

if [[ ${1} =~ ^[1-9][0-9]*$ ]]

then

while (( ${NUM} < ${1:-1} ))

do

DIR="${DIR}../"

NUM=$(( ${NUM} + 1 ))

done

else

DIR=..

fi

cd "${DIR}"

}

$ if check4progs cp foo mv bar rsync; then echo "needed progs avail, lets do funky stuff"; else echo "oh oh better abort now"; fi
2015-01-01 16:16:00
User: Xk2c
Functions: cp echo mv
-6

Thanks to the great grml team for this func!

You really should look at their shell configs for further usefull things!

http://git.grml.org/?p=grml-etc-core.git;a=blob_plain;f=etc/grml/script-functions;h=4d6bcea8f9beae83abd08f44155d299ea54a4a9f;hb=HEAD

# {{{ check for availability of program(s)

# usage example:

# check4progs [-s,-q,--quiet,--silent] arg [arg .... argn]

#

# with option given either of:

# -s,-q,--quiet,--silent

#

# check for available progs but produce no output

check4progs() {

[ -n "${ZSH_VERSION}" ] && emulate -L sh

local RTN=0

local oldifs="${IFS}"

local ARG d found

local VERBOSE=1

case ${1} in

-q | -s | --quiet | --silent)

VERBOSE=0

shift 1

;;

*)

;;

esac

while [ $# -gt 0 ]

do

ARG="$1"

shift

found=0

IFS=:

for d in $PATH

do

if [ -x "${d}/${ARG}" ]

then

found=1

break

fi

done

IFS="${oldifs}"

# check for availability

if [ ${found} -eq 0 ]

then

if [ ${VERBOSE} -eq 1 ]

then

printf "%s: binary not found\n" "${ARG}" >&2

fi

RTN=1

fi

done

# return non zero, if at least one prog is missing!

return $RTN

}

# }}}

shopt-set() ... func to long, please refer to description
2015-01-01 03:20:52
User: Xk2c
-6

shopt-set()

{

declare -i RTN=0

local ARG=''

while (( ${#} > 0 ))

do

ARG="${1}"

shift 1

if ! builtin shopt -s "${ARG}" 1>/dev/null 2>&1

then

RTN=1

fi

done

return ${RTN}

}

findfile() { find . -type f -iname "*${*}*" ; }
2015-01-01 03:15:51
User: Xk2c
Functions: find
Tags: find function
-4

Actually your func will find both files and directorys that contain ${1}.

This one only find files.

..and to look only for dirs:

finddir() { find . -type d -iname "*${*}*" ; }

psgrep() ... func to long, please look under "description"
2015-01-01 02:58:48
User: Xk2c
Functions: look
Tags: grep function ps
-9

David thanks for that grep inside!

here is mine version:

psgrep()

{

case ${1} in

( -E | -e )

local EXTENDED_REGEXP=1

shift 1

;;

*)

local EXTENDED_REGEXP=0

;;

esac

if [[ -z ${*} ]]

then

echo "psgrep - grep for process(es) by keyword" >&2

echo "Usage: psgrep [-E|-e] ... " >&2

echo "" >&2

echo "option [-E|-e] enables full extended regexp support" >&2

echo "without [-E|-e] plain strings are looked for" >&2

return 1

fi

\ps -eo 'user,pid,pcpu,command' w | head -n1

local ARG=''

if (( ${EXTENDED_REGEXP} == 0 ))

then

while (( ${#} > 0 ))

do

ARG="${1}"

shift 1

local STRING=${ARG}

local LENGTH=$(expr length ${STRING})

local FIRSCHAR=$(echo $(expr substr ${STRING} 1 1))

local REST=$(echo $(expr substr ${STRING} 2 ${LENGTH}))

\ps -eo 'user,pid,pcpu,command' w | grep "[${FIRSCHAR}]${REST}"

done

else

\ps -eo 'user,pid,pcpu,command' w | grep -iE "(${*})"

fi

}

psg(){ ps aux | grep -E "[${1:0:1}]${1:1}|^USER"; }
2015-01-01 00:12:45
User: flatcap
Functions: grep ps
Tags: grep function ps
-1

Function that searchs for process by its name:

* Shows the Header for reference

* Hides the process 'grep' from the list

* Case sensitive

The typical problem with using "ps | grep" is that the grep process shows up the in the output.

The usual solution is to search for "[p]attern" instead of "pattern".

This function turns the parameter into just such a [p]attern.

${1:0:1} is the first character of $1

.

${1:1} is characters 2-end of $1
finame(){ find . -iname "*$1*"; }
2014-12-31 22:33:08
Functions: find
Tags: find function
1

It looks for files that contains the given word as parameter.

* case insensitive

* matches files containing the given word.

nohist(){ export HISTFILE=/dev/null; }
2014-12-31 22:30:08
Functions: export
0

Yo run the `nohist` command and after that the commands won't get stored in the history file for the current session.

This makes no permanent changes.

psg(){ ps aux | grep -v grep | egrep -e "$1|USER"; }
2014-12-31 22:27:27
Functions: egrep grep ps
Tags: grep function ps
0

Function that searchs a process by its name and shows in the terminal.

* Shows the Header for reference

* Hides the process 'grep' from the list

* Case sensitive

<ctrl>+o
2014-12-28 22:00:15
User: snipertyler
5

Similar to entering a command, but will not erase the command from the command line.

Basically a shortcut from entering command, then pushing the up arrow key.

printf "%s\t%s\t%s\n" {1..9} '*' 0 '#'
2014-12-27 11:27:24
User: flatcap
Functions: printf
Tags: printf
2

Draw a telephone keyboard, using just a shell built-in command.

echo {1..9} '* 0 #' | tr ' ' '\n' |paste - - -
nmap -sP 10.0.0.0/8 | grep -v "Host" | tail -n +3 | tr '\n' ' ' | sed 's|Nmap|\nNmap|g' | grep "MAC Address" | cut -d " " -f5,8-15
2014-12-26 18:31:53
User: jaimerosario
Functions: cut grep sed tail tr
0

In the field, I needed to script a process to scan a specific vendor devices in the network. With the help of nmap, I got all the devices of that particular vendor, and started a scripted netcat session to download configuration files from a tftp server.

This is the nmap loop (part of the script). You can however, add another pipe with grep to filter the vendor/manufacturer devices only. If want to check the whole script, check in http://pastebin.com/ju7h4Xf4

tail -f access_log | awk '{print $1 , $12}'
2014-12-24 14:15:52
User: tyzbit
Functions: awk tail
0

Use this command to watch apache access logs in real time to see what pages are getting hit.

find . -name '*.php' | xargs wc -l
2014-12-24 11:15:18
User: erez83
Functions: find wc xargs
Tags: count code
0

count all the lines of code in specific directory recursively

in this case only *.php

can be *.*

wget blah --progress=dot -e dotbytes=100M
grep -E -rn --color=always --exclude-dir=".svn" --exclude-dir="packages" --exclude="*.swp" "(emit|on)\([\'\"]leader" ~/project/ | less -R
2014-12-23 20:08:25
User: hochmeister
Functions: grep less
0

grep for specific function invocations in this case, wither "emit" or "on" with "leader".

FILE=somefile.js; LOG=~/changes.diff; truncate -s0 ${LOG}; for change in $(svn log ${FILE} | awk -F' | ' '/^r[0-9]+/{print $1}'); do svn log -c ${change} >> ${LOG}; printf "\n" >> ${LOG}; svn diff -c ${change} >> ${LOG}; printf "\n\n\n" >> ${LOG}; done
2014-12-23 20:00:54
User: hochmeister
Functions: awk diff printf
Tags: svn diff log
0

from a svn repo, print a log, with diff, of each commit touching a given file