History linux command file

Where is bash’s history stored?

If I run history , I can see my latest executed commands. But if I do tail -f $HISTFILE or tail -f ~/.bash_history , they do not get listed. Does the file get locked, is there a temporary location or something similar?

6 Answers 6

Bash maintains the list of commands internally in memory while it’s running. They are written into .bash_history on exit:

When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to the file named by $HISTFILE

If you want to force the command history to be written out, you can use the history -a command, which will:

Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.

Write out the current history to the history file.

which may suit you more depending on exactly how you use your history.

If you want to make sure that they’re always written immediately, you can put that command into your PROMPT_COMMAND variable:

export PROMPT_COMMAND='history -a' 

Side note: if your .bash_history file accidentally becomes owned by root, things stop working. In that case, check the ownership and use sudo to fix the ownership if needed.

@young_souvlaki I expect your man history is for a library; at least, that’s what the only such man page I have available says at the top. It would be unusual for a library to document command-line options of other software, but help history (in Bash) will show applicable Bash documentation.

Thank you! You are correct! Oddly man history defaults to «history(n)» under «Tcl Built-In Commands». man 3 history gives the «Library Functions Manual». help history gives the options described.

(Not an answer but I cannot add comments)

If you are checking .bash_history because you just want delete a specific command (e.g. containing a password in clear), you can directly delete the entry in memory by history -d .

For example, supposing an output like:

$ history 926 ll 927 cd .. 928 export --password=super_secret 929 ll 

and you want purge the export line, you can simply achieve it by:

bash keeps it in working memory, bash can be configured to save it when bash closes or after each command, and to be loaded when bash starts or on request.

If you configure to save after each command, then consider the implications of having multiple bash running at same time. (command lines will be interleaved)

The start of you answer makes it sound as if the history is stored in a file called bash, or even in the bash exetable. I would write «It is stored by bash in memory, . «

While running, the history is kept only in memory (by default) if:

  • set -o history (an H in echo «$-» ) is set.
  • HISTSIZE is not 0 and
  • HISTIGNORE is not * (or some other very restrictive pattern).
Читайте также:  Связка ключей linux удалить

If any of the above fail, no history is stored in memory and consequently no history could or will be written to disk.

History in memory is written to disk if:

But only when the shell exits or if the commands history -a (append) or history -w (write) are executed.

To trigger an immediate write to disk you can use the variable:

which will append the new history lines to the history file. These are history lines entered since the beginning of the current bash session, but not already appended to the history file.

To overwrite the history in the HISTFILE with the list from memory.

So, you can remove a command from the history in memory:

 $ history 5 6359 ls 6360 cd .. 6361 comand --private-password='^%^&$@#)!@*' 6362 top 6363 set +o | less $ history -d 6361 $ history 5 6359 ls 6360 cd .. 6361 top 6362 set +o | less $ history -w 

And write it to disk with the last command:

 history -w # with `shopt -u histappend` unset 

Источник

How to Use the Linux history Command

The history command in Linux is a built-in shell tool that displays a list of commands used in the terminal session. history allows users to reuse any listed command without retyping it.

In this tutorial, we will show you how the history command works and different ways to use it.

How to use the Linux history command

How to Use Linux history Command

Using the history command without options displays the list of commands used since the start of the terminal session:

Using the history command to display the command history list

To display the command history list with a limited number of entries, append that number to the history command. For instance, to show only the latest five entries, use:

Using the history command to display a limited number of command history list entries

Once you close the terminal, the Bash shell saves new command history entries in the .bash_history file.

Use Date and Timestamps

The .bashrc file stores the Bash shell settings. Modifying this file allows you to change the output format of the history command.

Open the .bashrc file using a text editor such as Nano:

To change the output format to include date and timestamps, add the following line to the .bashrc file:

Edit the .bashrc file to include timestamps in the history command output

Note: The blank space before the closed quotation marks prevents the timestamp from connecting to the command name, making the history list easier to read.

Using different arguments after HISTTIMEFORMAT allows you to customize the level of detail in the timestamp:

Save the changes to the .bashrc file, relaunch the terminal, and run the history command to confirm the new output format:

The history command output with timestamps

View the Size of the History Buffer

The .bashrc file contains two entries that control the size of the history buffer:

  • HISTSIZE : The maximum number of entries for the history list.
  • HISTFILESIZE : The maximum number of entries in the .bash_history file.

Entries defining the history buffer size

Editing the HISTSIZE and HISTFILESIZE values changes how the Bash shell displays and saves the command history.

For instance, changing the HISTSIZE value to 10 makes the history command list show a maximum of 10 latest entries.

Читайте также:  Синхронизация времени linux ntpdate

Editing the history buffer size

Saving the changes to the .bashrc file, relaunching the terminal, and running the history command confirms the new output format:

The history command list with 10 entries

Repeat a Command

Running the history command allows you to reuse any of the commands on the list. For instance, to run the first command ( sudo apt update ) again, use:

Reusing the first command on the history list

Adding a dash () before the command number starts the count from the end of the list. For instance, to reuse the tenth last command ( history 5 ), use:

Reusing a command from the history list, counting from the end

Use double exclamation points to repeat the last command:

Reusing the last command

Search a Command by String

Adding a string after the exclamation point runs the latest command that starts with that string. For example, to reuse the latest command that begins with sudo , use:

Reusing a command starting with the

Using this method can cause problems if the shell runs an unexpected command, especially when searching for a command that starts with sudo . As a precaution, adding the :p argument displays the command without running it, allowing you to review the command and decide if you want to execute it.

Displaying the last command starting with the

To search for a command that contains a string, but may not start with it, add a question mark next to the exclamation point. For instance, to reuse the last command that contains echo :

Reusing the last command that contains the

In the example above, the shell reuses the last command that contains the echo string even though the command starts with sudo .

Check out our article on sudo command to learn how to use the sudo command with examples.

List the Matching Commands

Combining history and grep allows you to display a list of commands that contain a string. For example, to list all commands that contain ufw , use:

Searching the command history using the history and grep commands

Note: Learn more about using the grep command in Linux.

Change the Executed Command

Use the following syntax to change the last executed command:

For instance, the ufw command to enable port 20 shows that the port is already enabled:

Unsuccessfully running the ufw command

Use the syntax above to change the port number from 20 to 22:

Changing the previous command

Prevent Recording Commands in History

To prevent recording commands in the history list, temporarily disable recording by using:

To re-enable recording, use:

Delete History

Use the -d option with the history command to delete a command from the history list. For instance, delete command number 87 with:

Deleting an entry from the command history list

Use the -c option to clear the whole history list:

Update the History File

The Bash shell saves any updates to the command history list when you exit the terminal session. The history command also allows you to save changes while in the terminal session.

Using the -a option lets you append the command history entries from this session to the .bash_history file:

Another method is to use the -w option to save the entire history list to the .bash_history file:

After reading this tutorial, you should be able to use the history command in Linux to view, edit, and delete the command history list and reuse commands from it.

If you are interested in learning more about Linux commands, have a look at our Linux commands cheat sheet.

Источник

How to view the `.bash_history` file via command line?

I want to view the contents of my .bash_history file but don’t know how to get there via the command line.

Читайте также:  Install desktop oracle linux

The answer https://stackoverflow.com/questions/8561983#8562145 is a good one, I think. You can simply do cat $HISTFILE .

4 Answers 4

If you want to access the actual file itself, just use your favorite text editor (I use emacs but you can use pluma of gedit or vim or whatever):

That is the default location if your history file. If you don’t find anything there, you may have changed the history file’s name. This is stored in the $HISTFILE variable, so print it out to check its current value:

If, instead of the file, you just want to see your history, you can run history as @minerz029 suggested. The history command with no options just prints the contents of your $HISTFILE followed by the commands executed in the current shell that have not yet been written to that file, with line numbers.

If you actually need the output of the .bash_history file, replace history with
cat ~/.bash_history in all of the commands below.

If you actually want the commands without numbers in front, use this command instead of history :

otherwise, there will be no difference (except if you’re using a different shell).

Last 15 commands

to get only the last 15 lines of your history with the last executed printed last (at the bottom).

Searching for a command

history | grep "apt-get" | tail -n 15 

to get the last 15 commands which contained apt-get with the last executed printed last (at the bottom). You can replace apt-get with any command (or command argument) you want to search for (it can be a regular expression).

Scrolling through history

to scroll through all the commands executed starting with the most recent at the top. Press q to exit.

history | grep "apt-get" | tac | less 

to scroll through all the commands executed with » apt-get » in them (including arguments) starting with the most recent at the top. Press q to exit.

In addition to minerz029’s excellent answer.

To reiterate — the ‘history’ command prints the history along with a number next to it.

You can pipe the output of history into grep, less etc.

The ouput of history also shows a number on the left next to the output. e.g.

 469 free 470 ps -fA 471 ps -fA | grep xend 472 free 473 sudo vi /etc/xen/xend-config.sxp 474 cat /etc/default/grub 

With this number you can re-run the command.

e.g. to re-run 473 I would type into the terminal

followed by the enter key to repeat the command next to 473 in the history output.

You can also search interactively backwards in the command history by typing ctrl + r keys then start typing some of the contents of the command and it will search and fill it out. When you’ve found it you can type enter to repeat it, or press the [tab] key to copy it to the command line to edit the command first.

Of course, those last two options work in the bash shell. I’m not sure if these features work in other shells. But since bash is the Ubuntu default shell you should find them there.

Источник

Оцените статью
Adblock
detector