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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

All commands from sorted by
Terminal - All commands - 12,158 results
[ ~/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
-10

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
-2

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
-1

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
8

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
3

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

awk '{ total += gsub(/yourstring/,"") } END { print total }' yourfile
2014-12-16 21:00:45
User: bugmenot
Functions: awk
0

Count how many times a pattern is present into a file. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.

echo something | awk '{ total += gsub(/yourstring/,"") } END { print total }'
2014-12-16 20:58:42
User: bugmenot
Functions: awk echo
0

Count how many times a pattern is present into a stream. It can be one or more lines. No overlapping. It means searching for aa on aaa will output 1 not 2.

cd -P .
curl -s "http://smacie.com/randomizer/borat.txt" | shuf -n 1 -
2014-12-16 04:18:48
User: benjabean1
2

Turns out smacie.com has a text file containing every single one of the borat quotes, each one on a newline. This makes it very convenient, as this can be done without any sed-parsing, and uses less bandwitdth!

Note that borate quotes are quite offensive, much more so than "fortunes-off"!

ls -l | head -n 65535 | awk '{if (NR > 1) total += $5} END {print total/(1024*1024*1024)}'
rsync -v --ignore-existing `ls | head -n 40` root@localhost:/location
scp -r `ls | grep -vE "(Pattern1|Pattern2)"` user@remote_host:/location