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 tagged history from sorted by
Terminal - Commands tagged history - 51 results
!$
2011-12-06 18:21:09
User: anarcat
Tags: history
0

!$ will be expanded to the last argument on the previous command. There are also positionnal parameters like !:1, !:2...

$ history -a #in one shell , and $ history -r #in another running shell
2011-11-05 01:19:30
User: b_t
Tags: history bash
10

By default bash history of a shell is appended (appended on Ubuntu by default: Look for 'shopt -s histappend' in ~/.bashrc) to history file only after that shell exits.

Although after having written to the history file, other running shells do *not* inherit

that history - only newly launched shells do.

This pair of commands alleviate that.

echo "shopt -s histappend" >> ~/.bashrc ; . ~/.bashrc
shopt -s histverify
2011-10-27 00:33:34
User: b_t
Tags: history bash
13

Bash history commands are those that begin with the character !

(eg. the most popular 'sudo !!' Explained here => http://www.commandlinefu.com/commands/view/13).

By default bash immediately executes the history command.

Setting this shell option will make bash first allow you to verify/edit an

history command before executing it.

To set this option permanently, put this command in ~/.profile or ~/.bashrc file.

To unset this option issue following command.

shopt -u histverify
<space> secret -p password
2011-09-16 12:41:16
User: pcholt
0

Put a space in front of your command on the command line and it will not be logged as part of your command line history.

!<number>
2011-08-18 01:08:57
User: dbbolton
Tags: history bash zsh
0

You can find a command's history event number via the `history` command.

You can also put the history event number in your prompt: \! for bash, or %h for zsh.

Finally, I would like to point out that by "number", I mean POSITIVE INTEGER. Not, say, a letter, such as 'm'. Examples:

!1

or

!975
history -c
alias cdd="history -a && grep '^ *[0-9]* *cd ' ~/.bash_history| tail -10 >>~/.bash_history && history -r ~/.bash_history"
2011-07-13 09:44:16
User: knoppix5
Functions: alias
1

This alias is meant to append n (here is n=10) most recently used cd commands to the bottom of history file. This way you can easily change to one of previous visited directories simply by hitting 1-10 times arrow up key.

Hint: You can make more aliases implying the same rule for any set of frequently used long and complex commands like: mkisof, rdesktop, gpg...

history | tail -(n+1) | head -(n) | sed 's/^[0-9 ]\{7\}//' >> ~/script.sh
2011-06-08 13:40:58
Functions: head sed tail
1

Uses history to get the last n+1 commands (since this command will appear as the most recent), then strips out the line number and this command using sed, and appends the commands to a file.

unset HISTFILE
2010-11-15 09:16:11
User: Delian
Functions: unset
6

Unsetting HISTFILE avoid getting current session history list saved.

export HISTSIZE=0
bind '"\C-h": "\`fc\ \-s\`"'
2010-08-16 17:58:16
User: rthemocap
0

This is similar to using `!!` or

In bash 4.1 it seems you can bind directly to a shell command, but I'm not running that version.

svn up -r PREV # revert
2010-07-07 23:09:00
1

* Add comment with # in your command

* Later you can search that command on that comment with CTRL+R

In the title command, you could search it later by invoking the command search tool by first typing CTRL+R and then typing "revert"

alias histdel='history -d $((HISTCMD-2)) && history -d $((HISTCMD-1))'
2010-07-02 00:20:44
Functions: alias
0

I rarely need this, but I have a hard time remembering the command when I need it.

Admit it. This has happened to you. Yes this is bad, and you better clean up now.

Borrowed from http://thoughtsbyclayg.blogspot.com/2008/02/how-to-delete-last-command-from-bash.html

proceed_sudo () { sudor_command="`HISTTIMEFORMAT=\"\" history 1 | sed -r -e 's/^.*?sudor//' -e 's/\"/\\\"/g'`" ; sudo sh -c "$sudor_command"; }; alias sudor="proceed_sudo # "
2010-06-29 14:56:29
User: mechmind
Functions: alias sh sudo
Tags: history sudo
3

USAGE: $ sudor your command

This command uses a dirty hack with history, so be sure you not turned it off.

WARNING!

This command behavior differ from other commands. It more like text macro, so you shouldn't use it in subshells, non-interactive sessions, other functions/aliases and so on. You shouldn't pipe into sudor (any string that prefixes sudor will be removed), but if you really want, use this commands:

proceed_sudo () { sudor_command="`HISTTIMEFORMAT=\"\" history 1 | sed -r -e 's/^.*?sudor//' -e 's/\"/\\\"/g'`" ; pre_sudor_command="`history 1 | cut -d ' ' -f 5- | sed -r -e 's/sudor.*$//' -e 's/\"/\\\"/g'`"; if [ -n "${pre_sudor_command/ */}" ] ; then eval "${pre_sudor_command%| *}" | sudo sh -c "$sudor_command"; else sudo sh -c "$sudor_command" ;fi ;}; alias sudor="proceed_sudo # "
!:1-3
!:n
2010-06-12 02:48:27
User: dbbolton
Tags: history bash zsh
8

'n' is a non-negative integer. Using 0 will expand to the name of the previous command.

history | awk '{a[$'$(echo "1 2 $HISTTIMEFORMAT" | wc -w)']++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
git log --all --pretty=format:" " --name-only | sort -u
2010-05-11 16:06:42
Functions: sort
Tags: history git
2

What was the name of that module we wrote and deleted about 3 months ago? windowing-something?

git log --all --pretty=format:" " --name-only | sort -u | grep -i window
history | awk '{a[$'$(echo "1 2 $HISTTIMEFORMAT" | wc -w)']++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
2010-05-02 21:48:53
User: bandie91
Functions: awk echo sort wc
Tags: history awk wc
0

If you use HISTTIMEFORMAT environment e.g. timestamping typed commands, $(echo "1 2 $HISTTIMEFORMAT" | wc -w)

gives the number of columns that containing non-command parts per lines.

It should universify this command.

history | perl -F"\||<\(|;|\`|\\$\(" -alne 'foreach (@F) { print $1 if /\b((?!do)[a-z]+)\b/i }' | sort | uniq -c | sort -nr | head
2010-04-08 13:46:09
User: alperyilmaz
Functions: perl sort uniq
4

Most of the "most used commands" approaches does not consider pipes and other complexities.

This approach considers pipes, process substitution by backticks or $() and multiple commands separated by ;

Perl regular expression breaks up each line using | or < ( or ; or ` or $( and picks the first word (excluding "do" in case of for loops)

note: if you are using lots of perl one-liners, the perl commands will be counted as well in this approach, since semicolon is used as a separator

<(!!)
2010-02-06 18:35:10
User: drewk
7

Bash has a great history system of its commands accessed by the ! built-in history expansion operator (documented elsewhere on this site or on the web). You can combine the ! operator inside the process redirection

Very handy.

export HISTCONTROL=ignoreboth
2009-07-15 16:05:03
User: ioggstream
Functions: export
Tags: history
4

Don't track in history commands starting with whitespace.

Moreover ignore duplicates from history.

To be set in .bashrc

ex.

$ export HISTCONTROL=ignoreboth

$   echo antani

$   history|grep -c antani

kill -9 $$
2009-03-27 23:13:53
User: stu
Functions: kill
Tags: history exit
29

this exits bash without saving the history. unlike explicitly disabling the history in some way, this works anywhere, and it works if you decide *after* issuing the command you don't want logged, that you don't want it logged

... $$ ( or ${$} ) is the pid of the current bash instance

this also works perfectly in shells that don't have $$ if you do something like

kill -9 `readlink /proc/self`
history | perl -lane '$lsize{$_} = scalar(@F); if($longest<$lsize{$_}) { $longest = $lsize{$_}; print "$_"; };' | tail -n1