Saving commands permanently in history

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
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bernie BsenChef de cuisineCommented:
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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
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.
serialbandCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Eoin OSullivanConsultantCommented:
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.
serialbandCommented:
.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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 DeveloperCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.