Poor Technical Solution
Learn the problems we get when storing a Bash command in the history for the long term.
Saving our commands
Our following backup command became long and complex after applying all improvements:
(tar -cjf ~/photo.tar.bz2 ~/photo &&
echo "tar - OK" > results.txt ||
! echo "tar - FAILS" > results.txt) &&
(cp -f ~/photo.tar.bz2 ~/backup &&
echo "cp - OK" >> results.txt ||
! echo "cp - FAILS" >> results.txt)
Therefore, we should store it somewhere. Otherwise, we have to type the command in the terminal window each time. Typing is ultimately a bad idea because we can make a mistake or forget something.
Bash has an option to store frequently-used commands. The history file saves everything we executed in the terminal. The file is unique for each user and has the ~/.bash_history
path. When we press the “Ctrl+R” (or “control+R” in macOS) keystroke in the terminal window, Bash calls the quick search over the history. We can quickly find the required command there.
Can we store the backup command permanently in the history file?
This solution seems to be reliable and convenient. However, we should not jump to conclusions. Let’s take a look at its potential problems.
Using history file
First, the history file has a limited size. It saves 500 most recently executed commands by default. When this number exceeds, the oldest command is purged from history to make room for each new command. Therefore, we can accidentally lose a backup command.
Unlimited size of the history file
We can think about increasing the capacity of the history file. But, how much would be enough? Whatever capacity we choose, there is a risk of exceeding it. This problem leads to the idea of making the history file unlimited. Then, it could save all commands without overwriting anything.
We need to find a way to effectively store the backup commands. The history file with unlimited size can do it. However, could this decision lead to any problems?
Let’s suppose we use Bash for a couple of years. All the commands we execute during this time are stored in the .bash_history
file. If we run the same command twice, it appears twice in the file. Therefore, the history size will reach hundreds of megabytes in two years. We do not need most of these commands. Only a small portion of them are significant for regular usage. So, unlimited capacity leads to inefficient use of our disk drive space.
Get hands-on with 1400+ tech skills courses.