Mahesh
Mahesh

Reputation: 115

How can I add certain commands to .bash_history permanently?

I use bash and I sometimes have to type some very long commands for certain tasks. These long commands are not regularly used and generally gets overwritten in .bash_history file by the time I use them again. How can I add certain commands to .bash_history permanently?

Thanks.

Upvotes: 3

Views: 1270

Answers (4)

tripleee
tripleee

Reputation: 189387

The canonical answer is to create scripts containing these commands.

Edit Supposing you have the following history entries;

find /var/www -name '*.html' -exec fgrep '<title>' {} /dev/null \;
find ~/public_html -name '*.php' -exec fgrep include {} /dev/null \;

... you can try to isolate the parameters into a function something like this;

r () {
  find "$1" -name "*.$2" -exec fgrep "$3" {} /dev/null \;
}

... which you could use like this, to repeat the history entries from above:

r /var/www      html '<title>'
r ~/public_html php  include

Obviously, the step is then not very long to create a proper script with defaults, parameter validation, etc. (Hint: you could usefully default to the current directory for the path, and no extension for the file name, and add options like --path and --ext to override the defaults when you want to; then there will be only one mandatory argument to the script.)

Typically, you would store the script in $HOME/bin and make sure this directory is added to your PATH from your .profile or similar. For functions, these are usually defined in .profile, or a separate file which is sourced from this file.

Having it in a central place also helps develop it further; for example, for precision and perhaps some minor added efficiency, you might want to add -type f to the find command; now there is only one place to remember to edit, and you will have it fixed for good.

Upvotes: 0

Shawn Chin
Shawn Chin

Reputation: 86864

As others have mentioned, the usual way to do that would be to store your long commands as either aliases or bash functions. A nice way to organise them would the to put them all in a file (say $HOME/.custom_funcs) then source it from .bashrc.

If you really really want to be able to load commands into your bash history, you can use the -r option of the history command.

From the man page:

-r    Read the current history file and append its contents to the history list. 

Just store all your entries in a file, and whenever you need your custom history loaded simply run history -r <your_file>.

Here's a demo:

[me@home]$ history | tail  # see current history
 1006  history | tail
 1007  rm x
 1008  vi .custom_history
 1009  ls
 1010  history | tail
 1011  cd /var/log
 1012  tail -f messages
 1013  cd
 1014  ls -al
 1015  history | tail  # see current history

[me@home]$ cat $HOME/.custom_history  # content of custom history file
echo "hello world"
ls -al /home/stack/overflow
(cd /var/log/messages; wc -l *; cd -)

[me@home]$ history -r $HOME/.custom_history   # load custom history

[me@home]$ history | tail  # see updated history
 1012  tail -f messages
 1013  cd
 1014  ls -al
 1015  history | tail  # see current history
 1016  cat .custom_history
 1017  history -r $HOME/.custom_history
 1018  echo "hello world"
 1019  ls -al /home/stack/overflow
 1020  (cd /var/log/messages; wc -l *; cd -)
 1021  history | tail  # see updated history

Note how entries 1018-1020 weren't actually run but instead were loaded from the file.

At this point you can access them as you would normally using the history or ! commands, or the Ctrl+r shortcuts and the likes.

Upvotes: 4

sdmythos_gr
sdmythos_gr

Reputation: 5634

How about just extending the size of your bash history file with the shell variable

HISTFILESIZE

Instead of the default 500 make it something like 2000

Upvotes: 1

user247702
user247702

Reputation: 24212

You can create an alias for your command.

alias myalas='...a very long command here...'

Upvotes: -1

Related Questions