- How can I see all of the bash history?
- 5 Answers 5
- search through the command history in the terminal [closed]
- 2 Answers 2
- How to View Your Command History in Linux
- Scrolling Through Your Previous Commands in Linux
- Viewing Your Command History in Linux
- Reissue a Previous Command
- Enhanced Linux History Search using Grep
- Using a Reverse Search of Linux Command History
- Quickly Reissuing the Previous Linux Command
- Hiding Your Commands from Linux History
- Related Tutorials
- Stay on the Cutting Edge
How can I see all of the bash history?
In this case, the shell can not see the history executed by shell(1), but I want to see all of the bash history in every shell. So my question is how can I see all of the bash history? Does anybody know how to hack? Thank you very much in advance!
5 Answers 5
would also work, although I tend to just use
How to do it when I am working in a virtual environment (venv)? ~/.bash_history shows only the commands outside of the virtual environment.
@Raif — you would need access to the terminal within the virtual environment as the «user» running the app commands (root or the equivalent).
You should look into the histappend shell option and the -a flag to history :
histappend
If set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file.
history
-a Append the «new» history lines (history lines entered since the beginning of the current bash session) to the history file.
If you put history -a into your PROMPT_COMMAND , you’ll get an always-up-to-date .bash_history file.
Edit your .bashrc and append this to it’s end:
shopt -s histappend PROMPT_COMMAND="history -n; history -a" unset HISTFILESIZE HISTSIZE=2000
You can install something like Advanced Shell History, which will log each command to a sqlite3 database. It comes with a tool for querying the database from the command line. https://github.com/barabo/advanced-shell-history
With this setup, you will have a unified view of command history across all sessions. You also get things like command history for the current working directory (or subtree), command exit code, command duration, etc.
Full disclosure: I wrote and maintain the tool.
As several have noted, you need to use shopt -s histappend . Check by running shopt and verifying that histappend is ‘on’.
To ensure that each command (across multiple concurrent shells) appears in the history for each of those shells, add this at the end of your .bashrc file:
# Skip if not an interactive shell if [ -z "$" ]; then return; fi export PROMPT_COMMAND="history -a; history -c; history -r; $"
-a: appends the new history lines (history lines entered since the beginning of the current Bash session) to the history file.
-c: clears the history list.
-r: reads the current history file and append its contents to the history list.
Run source .bashrc or create new sessions and in several terminal windows enter the comment #Tn in each. Then on one terminal, enter history | tail -N to see the last N lines. You should see all of the comments entered on the different terminals.
It may be helpful to add the following to /etc/profile.d/bashrc.sh in order to get a timestamp on each line of the history:
if [ -z "$" ]; then return; fi export HISTTIMEFORMAT='%F %T '
The result looks like this:
[moi@laBoheme ~]$ history | tail -4 3292 2019-01-22 12:41:25 # T1 3293 2019-01-22 12:41:32 # T2 3294 2019-01-22 12:41:44 # T3 3295 2019-01-22 12:41:50 history | tail -4
search through the command history in the terminal [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
- This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
- This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Have you ever used a command in the Terminal, but then later forgot what you typed , and wished there was some way that you could view the last used commands?
2 Answers 2
There are many ways to search through your command history. Here are 3:
- use the up and down arrows on your keyboard when at the command prompt.
- use the history command.
- Simply press : Control + R when at the command prompt.
E.g : Control + R , then write php
If you need to see all of the commands history just type : history , but if you forgot a command that you typed before use Control + R
Sure, but omitting history would make this answer incomplete. Generally speaking, this question is also off-topic for StackOverflow since it does not directly relate to programming.
History command is best option for review all previous commands, using of this command all previous commands are showing in one page, so that you can easy check logs of command and you can also use up and down arrow for review the last command studentthadi.com/news/post/history-command-in-linux/11
How to View Your Command History in Linux
Save time by looking back in history to what you typed before.
Remember that one command that solved your problem? Was it cat, less, more, wc or something else? When we’re at the terminal, we can issue dozens of commands to solve a problem and in the background our Linux OS is recording these commands to a history file.
In this how-to we’ll look at various ways of searching and re-using our command history. Whilst you become accustomed to these commands it’s important to double check you don’t unintentionally reissue a command that could cause problems. Take your time using these new techniques and double check the details before pressing enter!
All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Scrolling Through Your Previous Commands in Linux
The simplest way to look through your recent commands is to use the up and down arrow keys on your keyboard to scroll through the previous commands. If you want to reissue a found command simply press the enter key.
Viewing Your Command History in Linux
The history command, in its most basic use case, lists and annotates the last 1000 commands issued in the terminal emulator. Each command has a number associated with it.
1. Run the history command to see a list of the last 1000 commands. You’ll see that all the listed historical commands are given a unique reference number.
2. Reissue the history command but constrain the amount of results to a specific number. This is useful if you know roughly when a command you are looking for was issued. You should see only the last 20 results listed.
Reissue a Previous Command
Now we can use history to view our previous commands, we can choose and reissue a command using the number assigned to the history results.
1. Run history 20 to create a list of commands, choose a command to reissue making sure that the chosen command is safe to run. Choosing a simple command like cd Music (1660) is a good safe example. Note there is no space between the exclamation mark and the command number.
Enhanced Linux History Search using Grep
By piping the output of history into grep we can perform a search of our command history returning results for a specified term or string. This is an excellent approach for finding a partially remembered command.
1. Search for a specific term using history and grep. We used the example search term “silhouette” as we recalled we had issued some commands to rectify a problem with a silhouette vinyl cutter. Replace that search term with something suited to your machine.
Using a Reverse Search of Linux Command History
Another handy approach to retrieve previous Linux commands is to use the reverse search function built into the terminal. To enter this mode you simply press ctrl and r. You can then enter a search term and use repeat presses of ctrl and r to step back through the list of previous commands containing that term. When you find a command you want to reissue press enter.
1. Press ctrl and r enters the reverse search mode, you should see the prompt now reads (reverse I search)`’:
2. Type a search term and you should see the last command issued that contained this term. For example we added the search term sudo to show the previous commands issued with sudo privileges.
3. Repeat pressing ctrl and r to step through other results.
4. Run a previous command by pressing enter or quit the reverse search by pressing esc .
Quickly Reissuing the Previous Linux Command
Often we will want to simply rerun the last command we issued. We can achieve this simply using the !! command.
1. Run the ls command to set this as the example to test.
2. Reissue the last command using . Note that the previous command is listed and performed.
Sometimes we may try to reuse a command that requires elevated privileges, for example editing a file outside of our home directory. To do this we can preface the previous command with sudo. In the following example we append the first ls command to be reissued with sudo.
Hiding Your Commands from Linux History
There may come a time where you need to keep a command out of your history, and if that scenario ever occurs, all you need to do is preface the command with a single press of the spacebar.
For example here are two ls commands, the second has a single space, hiding it from the history file.
With a little practice all the above approaches become quite instinctive to use and can make your terminal session more powerful and efficient. The ability to locate and reissue commands is extremely useful, especially when recovering a rarely used command or a command that was hard to create in the first instance.
Related Tutorials
Stay on the Cutting Edge
Join the experts who read Tom’s Hardware for the inside track on enthusiast PC tech news — and have for over 25 years. We’ll send breaking news and in-depth reviews of CPUs, GPUs, AI, maker hardware and more straight to your inbox.
By submitting your information you agree to the Terms & Conditions and Privacy Policy and are aged 16 or over.
Jo Hinchliffe is a UK-based freelance writer for Tom’s Hardware US. His writing is focused on tutorials for the Linux command line.