Linux command line command history

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^ .

Источник

How to Check Linux Commands History by Dates

The history command in Linux is used to view previously executed commands from the terminal. It will show a list of commands, with an ‘id’ next to each command.

Читайте также:  Установка qt creator linux manjaro

View History Last Executed Linux Commands

The historically run commands are stored in a dedicated file by the Linux shell.

View History of Commands Run in Terminal

Set History Date and Time for Each Command

The history command also stores the date and time of executing the command, however, does not, by default, show the time of execution of past commands.

To make history show the date as well, we need to set the global variable HISTTIMEFORMAT in the shell to the appropriate format, which can be done using the export command as follows:

$ export HISTTIMEFORMAT='%F %T' $ history

Set History Timestamps for Each Linux Command

View Linux Command History by Date

Now, you can pipe the above history command output to ‘grep‘ to get a history of commands executed on a particular date.

View History Commands by Date

Similarly, you can also filter the output for multiple key dates.

$ history | grep -E '2021-01-25|2021-01-23'

View History Commands by Date Range

Using this way, you can specify the dates as patterns for the last ‘N’ days, or some other combination of dates, and thus get command history for all those dates.

For example, to get command history from the last third to last the fifth date, you can use:

$ history | grep -E '2021-01-23|2021-01-22|2021-01-21'

Now, when we export the variable ‘HISTTIMEFORMAT‘ to the shell profile from the command line, it is exported just for the session, i.e., till the time the terminal is running, after which the variable is removed from the shell profile.

Permanently Set History Date and Time for Each Command

To permanently add the variable to the shell profile, add the export statement to your shell profile file (every command from which runs at the start of a shell).

For example, in the case of ‘Bash‘, run the following to export the variable permanently.

$ export HISTTIMEFORMAT='%F %t' >> ~/.bash_profile

If the file .bash_profile exists, the operator ‘>> ‘ will append the export statement to the file, otherwise, it creates the file and writes the statement.

Conclusion

In this article, we have learned how to check the command history of the last few days in Linux. For more information on the history command, make sure you check out its command line manual page by running:

Thanks for reading and let us know your questions or thoughts in the comments below!

Источник

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.

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:

Читайте также:  Arch linux oracle java

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.

Источник

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