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 - 50 results
quickscript () { filename="$1"; history | cut -c 8- | sed -e '/^###/{h;d};H;$!d;x' | sed '$d' > ${filename:?No filename given} }
2014-02-09 12:19:29
User: joedhon
Functions: cut sed
1

In order to write bash-scripts, I often do the task manually to see how it works. I type ### at the start of my session.

The function fetches the commands from the last occurrence of '###', excluding the function call. You could prefix this with a here-document to have a proper script-header.

Delete some lines, add a few variables and a loop, and you're ready to go.

This function could probably be much shorter...

function hgr() { grep --color -i "${1}" ~/.bash_history | sed -e 's/^ *//g' -e 's/ *$//g' | sort | uniq; }
echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
2013-09-19 03:25:14
Functions: echo
0

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character.

!:n
2013-09-15 03:41:13
User: hackerb9
10

Bash's history expansion character, "!", has many features, including "!:" for choosing a specific argument (or range of arguments) from the history. The gist is any number after !: is the number of the argument you want, with !:1 being the first argument and !:0 being the command. See the sample output for a few examples. For full details search for "^HISTORY EXPANSION" in the bash(1) man page.

 Note that this version improves on the previous function in that it handles arguments that include whitespace correctly.

<Meta-p> (aka <ALT+P>)
2013-09-10 17:13:02
User: hackerb9
Tags: history bash tcsh
9

[Click the "show sample output" link to see how to use this keystroke.]

Meta-p is one of my all time most used and most loved features of working at the command line. It's also one that surprisingly few people know about. To use it with bash (actually in any readline application), you'll need to add a couple lines to your .inputrc then have bash reread the .inputrc using the bind command:

echo '"\en": history-search-forward' >> ~/.inputrc

echo '"\ep": history-search-backward' >> ~/.inputrc

bind -f ~/.inputrc

  I first learned about this feature in tcsh. When I switched over to bash about fifteen years ago, I had assumed I'd prefer ^R to search in reverse. Intuitively ^R seemed better since you could search for an argument instead of a command. I think that, like using a microkernel for the Hurd, it sounded so obviously right fifteen years ago, but that was only because the older way had benefits we hadn't known about.

  I think many of you who use the command line as much as I do know that we can just be thinking about what results we want and our fingers will start typing the commands needed. I assume it's some sort of parallel processing going on with the linguistic part of the brain. Unfortunately, that parallelism doesn't seem to work (at least for me) with searching the history. I realize I can save myself typing using the history shortly after my fingers have already started "speaking". But, when I hit ^R in Bash, everything I've already typed gets ignored and I have to stop and think again about what I was doing. It's a small bump in the road but it can be annoying, especially for long-time command line users. Usually M-p is exactly what I need to save myself time and trouble.

  If you use the command line a lot, please give Meta-p a try. You may be surprised how it frees your brain to process more smoothly in parallel. (Or maybe it won't. Post here and let me know either way. ☺)

function garg () { tail -n 1 ${HISTFILE} | awk "{ print \$$1 }" }
2013-09-10 04:07:46
User: plasticphyte
Functions: awk tail
0

This gets the Nth argument in the last line of your history file. This is useful where history is being written after each command, and you want to use arguments from the previous command in the current command, such as when doing copies/moving directories etc.

I wrote this after getting irritated with having to continually type in long paths/arguments.

You could also use $_ if all you want is the last argument.

export HISTCONTROL=ignorespace
2013-07-25 08:31:10
User: gorynka
Functions: export
2
<space>secret_command;export HISTCONTROL=

This will make "secret_command" not appear in "history" list.

history | awk '{$1="";print substr($0,2)}'
2013-07-07 08:00:26
User: Fagood
Functions: awk
Tags: history awk
0

alias h="history | awk '{\$1=\"\";print substr(\$0,2)}'"

# h

[ 07/07/2013 10:04:53 ] alias h="history | awk '{\$1=\"\";print substr(\$0,2)}'"

cat $HISTFILE | grep command
history | tail -100 | grep cmd
2013-04-22 03:49:43
User: datamining
Functions: grep tail
0

this also can find the old command you used before

cat .bash_history | tail -100 | grep {command}
2013-04-10 10:40:52
User: techie
Functions: cat grep tail
-9

I know how hard it is to find an old command running through all the files because you couldn't remember for your life what it was. Heres the solution!! Grep the history for it. depending on how old the command you can head or tail or if you wanted to search all because you cannot think how long ago it was then miss out the middle part of the command. This is a very easy and effective way to find that command you are looking for.

export HISTFILE=/dev/null
2013-02-18 16:37:01
User: sonic
Functions: export
Tags: history bash
0

just an alternative to setting the size, this allows you to scroll up and see your previous commands in a given session but when you logout the history is not saved. That's the only advantage to doing it this way..

_ls
2013-02-02 00:44:01
User: marcusEting
Tags: history
4

just use a space to prevent commands from being recorded in bash's history on most systems

history -d $((HISTCMD-1)) && command_to_run
gzip -c ~/.bash_history > ~/.backup/history-save-$(date +\%d-\%m-\%y-\%T).gz
2013-01-11 17:31:07
User: tictacbum
Functions: date gzip
Tags: history backup
0

this one works on user crontab

export HISTFILESIZE=99999
2013-01-02 09:25:06
User: totti
Functions: export
1

set how many commands to keep in history

Default is 500

Saved in /home/$USER/.bash_history

Add this to /home/$USER/.bashrc

HISTFILESIZE=1000000000

HISTSIZE=1000000

export HISTSIZE=0
history > ~/history-save-$(date +%d-%m-%y-%T)
2012-08-18 07:40:33
Functions: date
Tags: history
2

simple and easy backup your history with timestamp

(cat ~/.bash_history;U='curl -s www.commandlinefu.com';$U/users/signin -c/tmp/.c -d'username=<USER>&password=<PASS>&submit=1'|$U/commands/favourites/json -b/tmp/.c|grep -Po 'nd":.*?[^\\]",'|sed -re 's/.*":"(.*)",/\1/g')>~/.h;HISTFILE=~/.h bash --login
2012-08-17 12:31:51
User: xenomuta
Functions: bash cat grep sed
5

This makes your commandlinefu.com's favorites appear as most recent commands in your history.

ssh user@hostname.domain "> ~/.bash_history"
2012-07-09 14:29:22
User: maxadamo
Functions: ssh
1

Only from a remote machine:

Only access to the server will be logged, but not the command.

The same way, you can run any command without loggin it to history.

ssh user@localhost will be registered in the history as well, and it's not usable.

<ctrl+p> for previous command; <ctrl+n> for next command
2012-06-01 11:25:09
Tags: history
0

Sometimes easier to just hit these keys to access previous / next commands in history instead of moving your hands all the way to the cursor keys

stty -ixon
2012-05-28 19:04:19
User: ricardofunke
Functions: stty
1

This command disable sending of start/stop characters.

It's useful when you want to use incremental reverse history search forward shortcut (Ctrl+s).

To enable again, type:

stty -ixoff
<ctrl+r>
2012-04-15 16:42:32
User: moollaza
1

"What it actually shows is going to be dependent on the commands you've previously entered.

When you do this, bash looks for the last command that you entered that contains the substring "ls", in my case that was "lsof ...". If the command that bash finds is what you're looking for, just hit Enter to execute it. You can also edit the command to suit your current needs before executing it (use the left and right arrow keys to move through it).

If you're looking for a different command, hit Ctrl+R again to find a matching command further back in the command history. You can also continue to type a longer substring to refine the search, since searching is incremental.

Note that the substring you enter is searched for throughout the command, not just at the beginning of the command." - http://www.linuxjournal.com/content/using-bash-history-more-efficiently

history | awk '{$1=""; print $0}' > install_pkg.sh
2012-01-23 06:46:25
User: cfunz
Functions: awk
Tags: history
0

If you are installing some new package. You can first go through the step by step install and then take the commands that you ran from history to create shell script which can used to install the package on other machines say test or production.

!$
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...