Saving commands permanently in history

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
HI,
there are some commands which i often need to type and are important on my mac terminal.
But sometimes when you dont use it for some days it gets deleted from the history.
How do i add some commands permanently to the history so that i can alaways reverse search on command line ?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bernie BsenChef de cuisine

Commented:
just create a text file (for example cpfle) containing the command:
#/bin/bash
cp file.pdf /destination/
exit

Open in new window

Place it in directory /usr/local/bin and set it executable:
sudo chmod +x /usr/local/bin/cpfle

Open in new window

after that you can simply enter "cpfle" and the game starts...
David FavorFractional CTO
Distinguished Expert 2018

Commented:
If I understand, I think the solution will be to set...

#
 First, dedup your history
export HISTCONTROL=erasedups

# Make history retention unlimited...
export HISTSIZE=-1
export HISTFILESIZE=-1

# Make history retention large...
export HISTSIZE=10000
export HISTFILESIZE=10000

Open in new window


Tip: These variables work slightly different in different shells + also different shell versions.

Read https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html carefully or better man bash on your machine, if you're using bash.

By playing with HISTCONTROL + HISTIGNORE you can dramatically reduce the size of your history file.

Big Note: If you issue many commands your history file can grow to a massive size over time. This is why setting HISTSIZE and HISTFILESIZE to some non-infinite value is highly recommended.
If it's something complex and easy to forget, it's probably easier to save it in .bashrc and .bash_profile as an alias.

echo "alias My_command= long command string" >> ~/.bashrc
echo "alias My_command= long command string" >> ~/.bash_profile

Then open a new terminal or source the file and load the new settings.
source ~/.bash_rc

That way, you can just run the alias and you can also view your .bash_rc to see the command or just type alias to see the newly set alias.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

serialband is spot-on once again .. create the commands in your bash_profile or bash_rc file and using an alias you can execute them using a shortcut.

I've about 100 setup and they are an absolute dream to use.  If you ever need to change just edit the file add or edit your aliases and re-run the source command to refresh them.
.bash_profile is for login terminals.
.bashrc is for non-login terminals  (generally scripts).

You'll only need to source the file when you don't reload the terminal.  All new terminals will source the file as part of its initialization.

Most people don't know the difference and just need them to be the same.  Some systems behave differently on which is loaded when you ssh to a terminal, compared to when you are at the console.  You can just link them together as a single file if you don't need them to be different.
David FavorFractional CTO
Distinguished Expert 2018

Commented:
serialband is correct, if the commands you're issuing are 100% the same each time.

My guess is you've already explored this option + your commands are unique for each invocation.

Might be good if you give an example of your commands, so people can provide best answers.
Duncan RoeSoftware Developer

Commented:
I suggest a system I've been using for a while: have a text file called,say, lines with your 1-line commands in it.
Have aliases and environment variables to access this file, e.g.
alias HL='head -n30 /home/dunc/lines'
alias TL='tail -n30 /home/dunc/lines'
export L=' /home/dunc/lines'

Open in new window

(The leading space in L means you can enter e.g. cat$L).
Populate your file with your command lines. As well as commands, I put (at the front) items I frequently have to enter into web pages to log in: first name, last name, email address, account numbers, (street) address, postcode and so on.
To use a line, triple-click on it to copy then middle-click in the destination window to paste.
I have a separate file for entering search strings into less (they used to be in lines but having a separate file makes them easier to find). For instance, when looking through the output from diff
/^(Only in|diff|Binary)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial