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,175 results
python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, allow_unicode=True)' < foo.json > foo.yaml
2015-01-20 15:58:29
User: renoirb
Functions: python
1

If you tried the other Python version of Convert JSON to YAML and you end up with lines that has "!!python/unicode", this version of the command is for you.

(pv -n centos-7.0-1406-x86_64-DVD.img | dd of=/dev/disk4 bs=1m conv=notrunc,noerror) 2>&1 | dialog --gauge "Copying CentOS to USB Stick in /dev/disk4" 10 70 0
hexdump -n6 -e '/1 ":%02X"' /dev/random|sed s/^://g
2015-01-19 03:09:43
User: rubo77
Functions: hexdump sed
3

Generate a random MAC address with capital letters

while ( true ) ; do if [ $(expr $RANDOM % 2 ) -eq 0 ] ; then echo -ne "\xE2\x95\xB1" ; else echo -ne "\xE2\x95\xB2" ; fi ; done
2015-01-17 12:46:37
User: tobi
Functions: echo expr true
1

Generates labyrinth-like pattern on UTF-8 terminal in bash.

For fun ;)

\[command]
2015-01-15 18:31:50
User: tyzbit
1

Most distributions alias cp to 'cp -i', which means when you attempt to copy into a directory that already contains the file, cp will prompt to overwrite. A great default to have, but when you mean to overwrite thousands of files, you don't want to sit there hitting [y] then [enter] thousands of times.

Enter the backslash. It runs the command unaliased, so as in the example, cp will happily overwrite existing files much in the way mv works.

ssh hostname nc -l 9876
svn propset svn:mergeinfo "/trunk:4" .
mkfifo fifo; while true ; do echo "Waiting for new event"; nc -l 8080 < fifo | tee -a proxy.txt /dev/stderr | nc 192.168.0.1 80 > fifo ; done
2015-01-14 09:26:54
User: ynedelchev
Functions: echo mkfifo tee true
5

If you have a client that connects to a server via plain text protocol such as HTTP or FTP, with this command you can monitor the messages that the client sends to the server. Application level text stream will be dumped on the command line as well as saved in a file called proxy.txt.

You have to change 8080 to the local port where you want your client to connect to. Change also 192.168.0.1 to the IP address of the destination server and 80 to the port of the destination server.

Then simply point your client to localhost 8080 (or whatever you changed it to).

The traffic will be redirected to host 192.168.0.1 on port 80 (or whatever you changed them to).

Any requests from the client to the server will be dumped on the console as well as in the file "proxy.txt".

Unfortunately the responses from the server will not be dumped.

if [ "${SSH_CLIENT%% *}" == "ipaddr" ]; then command; fi
2015-01-13 22:09:38
User: snipertyler
Tags: ssh
4

Place in ~/.bashrc

If you login to a ssh server from different ips, sometimes you want to do something specific for each.

e.g., quickly go into screen -x session from a phone, but not your desktop.

tar -cvj --exclude-vcs -f archive.tar.bz2 somedirectory
2015-01-13 14:55:41
User: fr00tyl00p
Functions: tar
0

You can create a backup of a directory which does not contain disturbing .svn and similar directories with that command.

xset -b
2015-01-13 14:45:24
User: fr00tyl00p
1

Execute this command in a terminal to disable the system-bell during X-session lifetime.

less -S somefile
ffmpeg -i "$fin" -c copy -copyts "${fin%.*}.mp4"
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
-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

}