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 export from sorted by
Terminal - Commands using export - 90 results
export PS1="[\u@`curl icanhazip.com` \W]$ "
export PS1="[\u@`hostname -I` \W]$ "
2011-10-25 18:22:55
User: voyeg3r
Functions: export
7

when working with many machines in a computer lab need to know the IP addr is very large, this is a simplistic solution to make things easier

export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'
2011-10-18 19:58:39
User: mpb
Functions: cd export
1

I was surprised to find that with RedHat bash, I could not find any comment lines (begining with #) in my bash shell history. Surprised because in Mageia Linux this works. It turns out that RedHat's bash will keep comment lines if in my .bashrc, I define:

export HISTIGNORE=' cd "`*: PROMPT_COMMAND=?*?'

Why have comment lines in shell history? It's a handy and convenient way to make proto-commands (to be completed later) and for storing brief text data that is searchable in shell history.

export GREP_OPTIONS='--color=auto'
2011-07-24 01:32:10
User: h3xx
Functions: export
Tags: color grep
1

This will affect all invocations of grep, even when it is called from inside a script.

export PS1="C:\\>"; clear
2011-06-18 17:52:42
User: ThePCKid
Functions: export
-1

Is there somebody that uses Windows a lot that keeps messing up your Linux machine? Press Ctrl+Alt+F1-F6 and run this command after logging into a text shell!

glu() { (local IFS="$1"; shift && echo "$*") }; repath() { ( _E=`echo "${PATH//:/$'\n'}" | awk '!x[$0]++'`; glu ":" $_E ) ; } ; PATH=`repath` ; export PATH
2011-06-09 12:11:18
User: Timothy
Functions: awk echo export shift
0

Thanks to the authors of:

awk '!x[$0]++' <file>

and the author of:

joinargs() { (local IFS="$1"; shift && echo "$*") }

and others, we can have a fast Linux or android.

IMPORTANT if you find a priority order problem in PATH you can push a path directory to the front without duplication as follows:

PATH=/bin:$PATH

then ...

Check duplication with:

echo $PATH|tr : '\n'|sort|uniq -d

Finally do a very neat line by line list of $PATH:

echo "${PATH//:/$'\n'}

The speed up is very noticeable for android, and builds on Linux Ubantu are much faster with make and scripts.

I will update the command on request. Timothy from SONY

export PS1='[\[\e[36;1m\]\u@\[\e[32;1m\]\h \[\e[31;1m\]\w]# \[\e[0m\]'
export PATH=$PATH$(find "$PWD" -name '.*' -prune -o -type f -a -perm /u+x -printf ':%h\n' | sort -u | tr -d '\n'); echo $PATH
2011-03-11 19:31:17
User: dexterhu
Functions: echo export find sort tr
-1

(1) don't run twice, or the same folder will occur in $PATH.

(2) otherwise you need to start a new terminal

CLASSPATH=.; export CLASSPATH=$CLASSPATH$(find "$PWD" -name '*.jar' -type f -printf ':%p\n' | sort -u | tr -d '\n'); echo $CLASSPATH
export EMAIL=caiogore@domain.com && mutt -s "chave webmail" destination@domain.com < /dev/null
export PATH=$PATH:`pwd`
2011-02-05 06:29:54
User: YogiBare
Functions: export
-3

This will temporarily add the current directory to your $PATH. Allowing you to execute from that directory for the duration of the session.

export TMOUT=10
2010-11-24 00:04:35
User: strzel_a
Functions: export
7

Useful in root's .profile - will auto-logout after TMOUT seconds of inactivity.

Close after `seconds` inactive.

export TMOUT=seconds

(unefunge)

export PS1="(\@) $PS1"
2010-11-22 04:50:21
User: cmdq
Functions: export
Tags: bash time ps1
1

Adds the time in 12hr AM/PM format to the beginning of a prompt. Change \@ to \t for 24-hour time or \T for 12hr without AM/PM.

To keep the time the next time you open a terminal, edit ~/.bashrc and stick the command at the bottom.

bm() { export BM${1?"bookmark name missing"}="$PWD" ; }; forget() { unset BM${1?"bookmark name missing"} ; }
2010-11-19 12:15:11
User: unefunge
Functions: export unset
2

pushd and popd are your friends, but sometimes they're just incompatible with the way one works...

Two shell functions:

bm bookmarkname - "bookmarks" the current directory, just 'cd $BMbookmarkname' to return to it.

forget bookmarkname - unsets the 'bookmarkname' variable. It isn't mandatory, they cease to exist when the session ends.

export HISTSIZE=0
export GREP_COLOR='1;32'; cat /dev/urandom | hexdump -C | grep --color=auto "ca fe"
export PROMPT_COMMAND='if (($? > 0)); then echo -ne "\033[1;31m"; fi'; export PS1='[\[\]\u\[\033[0m\] \[\033[1;34m\]\w\[\033[0m\]]\$ '
2010-08-25 21:19:30
User: quintic
Functions: echo export
3

If the return code from the last command was greater than zero, colour part of your prompt red. The commands give a prompt like this:

[user current_directory]$

After an error, the "[user" part is automatically coloured red.

Tested using bash on xterm and terminal. Place in your .bashrc or .bash_profile.

export PS1="\[\017\033[m\033[?9l\033[?1000l\]$PS1"
2010-07-15 19:18:05
User: cout
Functions: export
5

This will cause bash to fix a garbled terminal before the prompt is printed. For example, if you cat a file with nonprintable character sequences, the terminal sometimes ends up in a mode where it only prints line drawing characters. This sequence will return the terminal to the standard character set after every command.

rp() { local p; eval p=":\$$1:"; export $1=${p//:$2:/:}; }; ap() { rp "$1" "$2"; eval export $1=\$$1$2; }; pp() { rp "$1" "$2"; eval export $1=$2:\$$1; }
2010-07-15 18:52:01
User: cout
Functions: eval export
0

I used to do a lot of path manipulation to set up my development environment (PATH, LD_LIBRARY_PATH, etc), and one part of my environment wasn't always aware of what the rest of the environment needed in the path. Thus resetting the entire PATH variable wasn't an option; modifying it made sense.

The original version of the functions used sed, which turned out to be really slow when called many times from my bashrc, and it could take up to 10 seconds to login. Switching to parameter substitution sped things up significantly.

The commands here don't clean up the path when they are done (so e.g. the path gets cluttered with colons). But the code is easy to read for a one-liner.

The full function looks like this:

remove_path() { eval PATHVAL=":\$$1:" PATHVAL=${PATHVAL//:$2:/:} # remove $2 from $PATHVAL PATHVAL=${PATHVAL//::/:} # remove any double colons left over PATHVAL=${PATHVAL#:} # remove colons from the beginning of $PATHVAL PATHVAL=${PATHVAL%:} # remove colons from the end of $PATHVAL export $1="$PATHVAL" } append_path() { remove_path "$1" "$2" eval PATHVAL="\$$1" export $1="${PATHVAL}:$2" } prepend_path() { remove_path "$1" "$2" eval PATHVAL="\$$1" export $1="$2:${PATHVAL}" }

I tried using regexes to make this into a cleaner one-liner, but remove_path ended up being cryptic and not working as well:

rp() { eval "[[ ::\$$1:: =~ ^:+($2:)?((.*):$2:)?(.*):+$ ]]"; export $1=${BASH_REMATCH[3]}:${BASH_REMATCH[4]}; };
export MANPAGER='most'
2010-07-02 08:35:36
User: mrwill
Functions: export
1

you should have the "most" package installed. I like it because it is colorful and easier to read. alternatively you can use "less" instead of "most".

you can also add this to your ~/.bashrc to make it permanent.

addman () { export MANPATH=`find $1 -xdev -type d -name man -printf %p:`${MANPATH}; }
2010-06-12 17:47:20
User: zoomgarden
Functions: export man
Tags: man function
0

Prepends paths containing man directories to your MANPATH variable for the given top level directory. If you build or install software with non-standard documentation locations, you can just add them to your MANPATH with this little function. -xdev prevents crossing filesystem boundaries when searching for man dirs.

export HISTFILE=/dev/null/
2010-06-10 18:48:43
User: thund3rpants
Functions: export
-4

This command is great for dumping your history so that when you're working on a system that is under a shared account, you can keep others from reviewing your history file. I use this when I'm just visiting a system to diagnose a problem, or to make a quick fix.

export PSOA='user,pid,time,state,command' ; function _ps { /bin/ps $@ ; } ; alias psa='_ps ax -o $PSOA'
2010-05-27 02:07:56
User: mattmartini
Functions: alias export
-1

#ps aliases

PSO='user,pid,ppid,%cpu,%mem,time,start,state,command'

PSOA='user,pid,time,state,command'

PSOL='user,pid,ppid,%cpu,%mem,nice,pri,etime,time,tt,state,ucomm'

export PSO PSOA PSOL

function _ps { /bin/ps $@ ; }

alias ps='_ps ax -o $PSO'

alias psa='_ps ax -o $PSOA'

alias psl='_ps ax -o $PSOL'

alias psm='_ps -U $USER -o $PSOA'

[ -n "$SSH_CLIENT" ] && export DISPLAY=$(echo $SSH_CLIENT | awk '{ print $1 }'):0.0
2010-04-14 08:19:37
User: GouNiNi
Functions: awk echo export
0

In some case, you need to use remote gui on servers or simple machines and it's boring to see "cannot open display on ..." if you forgot to export your display. Juste add this line in .bashrc on remote machine. Dont forget to allow remote client on your local X server :

xhost +
export FIGNORE=.svn
2010-04-09 15:26:00
User: peter0081
Functions: export
13

When browsing java source code (for example) it's really annoying having to type the first letter of the package when there is only one package in the subdir.

man bash for more info about FIGNORE