Bash History
Any new commands that have been issued in the active terminal can be appended to the .bash_history
file with the following command:
history -a
The only tricky concept to understand is that each terminal has its own bash history list (loaded from the .bash_history
file when you open the terminal)
If you want to pull any new history that's been written by other terminals during the lifetime of this active terminal, you can append the contents of the .bash_history
file to the active bash history list
history -c;history -r
This will clear the current history list so we don't get a repeated list, and append the history file to the (now empty) list.
Solution
You can use the bash variable PROMPT_COMMAND
to issue a command with each new prompt (every time you press enter in the terminal)
export PROMPT_COMMAND='history -a'
This will record each command to the history file as it is issued.
Result
Now any new terminal you open will have the history of other terminals without having to exit
those other terminals. This is my preferred workflow.
More Precision
Let's say (for some reason) you have two terminals that you're using simultaneously and you want the history to reflect between both for each new command.
export PROMPT_COMMAND='history -a;history -c;history -r'
The main drawback here is that you may need to press enter to re-run the PROMPT_COMMAND in order to get the latest history from the opposite terminal.
You can see why this more precise option is probably overkill, but it works for that use case.
Look into installing sshfs if you're on Linux. You can remotely connect to a machine over ssh, but it will show it as part of the file system on your local machine, so you can edit files in your local editor, and treat it as if it were a local file. You don't even have to install anything on the server side, it all just works off of ssh.
Similarly, there is win-sshfs for windows, which takes a similar approach to sshfs, but instead mounts the file system as a Windows network drive. Thanks for the recommendation Lexi R!
Another alternative would be using FTP to grab the files and put them onto your local machine, view and edit them as you'd like, then use FTP to put them back in place. You'd need a FTP server on the CentOS box, and you can use something like Filezilla to connect.
Best Answer
You can remove just the offending line from
bash
's history, instead of clearing the entire history. Simply remove the line with the-d
flag, then save (write) the new history with the-w
flag: