Linux terminal history файл

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.

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.

Читайте также:  Включить права администратора linux

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.

Источник

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.
Читайте также:  Linux get device name

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.

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:

Читайте также:  Получить переменные среды linux

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.

Источник

View history of commands run in terminal

To clear the history, delete the file and clear the temp history:

rm ~/.bash_history && history -c 

The history size defaults to 500 commands. You can, however, increase this by adding a line to your ~/.bashrc file to set the HISTSIZE variable:

This will not take effect immediately, but only to newly started sessions. To apply this, re-source the .bashrc file:

or run HISTSIZE=. in your current session.

By default it only keeps the latest 500 commands though. You can change the number to be kept, but you only have to accidentally start it with the default settings once, and all your older history will be gone.

You can type history on a terminal to view all the previous executed commands.

You can truncate the output to some lines (where 5 is the number of lines):

If do you want to view only commands containing a string (i.e. mv ), you can do this:

You can recall a command by typing ! followed by the entry number.

Let’s say that I have a history like this:

1 ls -la 2 mkdir foo 3 mv bar.txt foo 
  • To run mkdir foo , you can type !2 .
  • To run the last command, you can use !-1 or !!
  • To run the penultimate, you can use !-2

If you run a command that fails because it needs root privileges (i.e. touch /etc/foo ), you can use sudo !! to run the last command as root.

  • If you type !man you will execute the last command that begins with man
  • If do you type !?man? it will execute the last command that contains man (not neccessarily at the line begin)

If do you have a typo in a command, you can fix it this way. Let’s say that I type cat .bash_hi , to replace .bash_hi by .bash_history I only need to type ^hi^history^ .

Источник

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