Linux and history command

Introduction to History Command in Linux [For Beginners]

Everything you type in the terminal is stored in the shell history. Learn how to use command history in Linux in this introduction to history command.

Let’s face it, us humans are not great at remembering things. Sometimes being forgetful can be very frustrating and lead to painful consequences.

Unlike us, computers are capable of near-perfect memory. One example is the history command in Linux. This program keeps track of everything that we type into the command line.

It sounds pretty simple, and it is. You’d be surprised how often this tool can come in handy, though!

Let’s walk through some examples that will improve our command-line skills and make us more productive.

Where are the settings for history command stored in Linux?

To understand the history command better, we will look at what settings can be configured by the user.

The typical default for the number of commands logged is 1000 on Ubuntu, but it can vary from system to system. The settings for history are set by your bash profile (Normally ~/.bashrc). There are several history file settings that are editable from this file including the location of the log file.

By default, command history is usually logged to ~/.bash_history.

[email protected]:~$ cat ~/.bash_history sudo apt-get clean #sudo e4defrag / df -h sudo e4defrag / sudo fstrim / sudo -s poweroff ls

How to use the history command in Linux?

History Command

Let’s see some examples of using history command in Linux.

1) Use arrow keys to scroll through history of commands

Many of us don’t think of what the command line was like before we knew this trick. If you don’t already know, using your up/down arrow keys lets you quickly scroll through your command history.

This is great for when you make a mistake while entering some long complex command. Just hit the up arrow, and correct it. Done.

Читайте также:  Arch linux virtualbox общая папка

2) Default history settings

Let’s start with something simple. We’re just going to enter the command and see what is returned.

This will return a numbered list of your recently entered commands.

[email protected]:~$ history 1 sudo apt-get clean 2 #sudo e4defrag / 3 df -h 4 sudo e4defrag / 5 sudo fstrim / 6 sudo -s 7 poweroff 8 ls 9 history

Looks familiar, right? This is the content from our .bash_history file, plus a new entry for ‘history’.

In fact, new entries will not be reflected in the file until you close the shell.

2) Running a command from the history using index

Did you notice that the commands were numbered when you ran the history command? To re-run a specific command quickly, type ! and then the number.

[email protected]:~$ !8 ls Desktop Downloads fontconfig Pictures Templates Documents examples.desktop Music Public Videos

3) Delete a command from history

If you made a mistake, you can erase a specific item with the -d option followed by the line number. Let’s erase line number nine.

[email protected]:~$ history -d 9 [email protected]:~$ history 1 sudo apt-get clean 2 #sudo e4defrag / 3 df -h 4 sudo e4defrag / 5 sudo fstrim / 6 sudo -s 7 poweroff 8 ls

4) Delete entire command history

Maybe you want to get rid of more than one line. We can erase the entire history using the -c option.

[email protected]:~$ history -c [email protected]:~$ history 1 history

5) Searching for commands in the history

Obviously, my history file is now fairly short since I’ve just cleared it. However, with the potential to have up to 1000 commands, things can get messy quickly. Fortunately, you can use tools to search history to help find that complex command you used earlier.

Method 1. Use Grep

Here’s an updated list of our history.

[email protected]:~$ history 1 history 2 ls 3 ll 4 cd 5 cd .. 6 cd 7 ls 8 ll 9 history

Now, I am going to use the history command piped with the grep command to find entries containing the character l .

[email protected]:~$ history | grep l 2 ls 3 ll 7 ls 8 ll 10 history | grep l

You can also search your commands using the reverse-i-search tool. If you hit Ctrl+r, you will see a new prompt that you can use to search your command history.

You can start typing and it will return results as you add letters. This is one of the must-know terminal shortcuts in Linux.

Both grep and the reverse-i-search can be used to make your life easier.

This was just the very basic history command examples. There are no ends to customizing it as per your preference and requirement. Perhaps we’ll cover some advanced history commands later in the future.

I hope you find this introduction to history command useful. Please provide your feedback in the comment section.

Источник

The Power of Linux “History Command” in Bash Shell

We use history command frequently in our daily routine jobs to check history of command or to get info about command executed by user. In this post, we will see how we can use history command effectively to extract the command which was executed by users in Bash shell. This may be useful for audit purpose or to find out what command is executed at what date and time.

Читайте также:  Линукс минт форматирование флешки

By default date and timestamp won’t be seen while executing history command. However, bash shell provides CLI tools for editing user’s command history. Let’s see some handy tips and tricks and power of history command.

linux history command

1. List Last/All Executed Commands in Linux

Executing simple history command from terminal will show you a complete list of last executed commands with line numbers.

[[email protected] ~]$ history 1 PS1='\e[1;35m[\[email protected]\h \w]$ \e[m ' 2 PS1 /cdn-cgi/l/email-protection" data-cfemail="b7c2f7">[email protected]\h \W]$ \e[m " 3 PS1 /cdn-cgi/l/email-protection" data-cfemail="eb9eab">[email protected]\h:\w [\j]$ " 4 ping google.com 5 echo $PS1 6 tail -f /var/log/messages 7 tail -f /var/log/messages 8 exit 9 clear 10 history 11 clear 12 history

2. List All Commands with Date and Timestamp

How to find date and timestamp against command? With ‘export’ command with variable will display history command with corresponding timestamp when the command was executed.

[[email protected] ~]$ export HISTTIMEFORMAT='%F %T ' 1 2013-06-09 10:40:12 cat /etc/issue 2 2013-06-09 10:40:12 clear 3 2013-06-09 10:40:12 find /etc -name *.conf 4 2013-06-09 10:40:12 clear 5 2013-06-09 10:40:12 history 6 2013-06-09 10:40:12 PS1='\e[1;35m[\[email protected]\h \w]$ \e[m ' 7 2013-06-09 10:40:12 PS1 /cdn-cgi/l/email-protection" data-cfemail="522712">[email protected]\h \W]$ \e[m " 8 2013-06-09 10:40:12 PS1 /cdn-cgi/l/email-protection" data-cfemail="532613">[email protected]\h:\w [\j]$ " 9 2013-06-09 10:40:12 ping google.com 10 2013-06-09 10:40:12 echo $PS1
Meaning of HISTTIMEFORMAT variables
%F Equivalent to %Y - %m - %d %T Replaced by the time ( %H : %M : %S )

3. Filter Commands in History

As we can see same command is being repeated number of times in above output. How to filter simple or non destructive commands in history?. Use the following ‘export‘ command by specifying command in HISTIGNORE=’ls -l:pwd:date:’ will not saved by system and not be shown in history command.

[[email protected] ~]$ export HISTIGNORE='ls -l:pwd:date:'

4. Ignore Duplicate Commands in History

With the below command will help us to ignore duplicate commands entry made by user. Only single entry will be shown in history, if a user execute a same command multiple times in a Bash Prompt.

[[email protected] ~]$ export HISTCONTROL=ignoredups

5. Unset export Command

Unset export command on the fly. Execute unset export command with variable one by one whatever commands have been exported by export command.

6. Save export Command Permanently

Make an entry as follows in .bash_profile to save export command permanently.

[[email protected] ~]$ vi .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs export HISTCONTROL=ignoredups PATH=$PATH:$HOME/bin export PATH

7. List Specific User’s Executed Commands

How to see command history executed by a specific user. Bash keeps records of history in a ‘~/.bash_history’ file. We can view or open file to see the command history.

[[email protected] ~]$ vi .bash_history cd /tmp/ cd logstalgia-1.0.3/ ./configure sudo passwd root apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc ./configure make apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc++ apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc apt-get install make mysql -u root -p apt-get install grsync apt-get install unison unison

8. Disable Storing History of Commands

Some organization do not keep history of commands because of security policy of the organization. In this case, we can edit .bash_profile file (It’s hidden file) of user’s and make an entry as below.

[[email protected] ~]$ vi .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin HISTSIZE=0 export PATH .bash_profile (END)

Save file and load changes with below command.

Читайте также:  Which linux home server

Note: If you don’t want system to remember the commands that you have typed, simply execute below command which will disable or stop recording history on the fly.

Tips: Search ‘HISTSIZE‘ and edit in ‘/etc/profile’ file with superuser. The change in file will effect globally.

9. Delete or Clear History of Commands

With up and down arrow, we can see previously used command which may be helpful or may irate you. Deleting or clearing all the entries from bash history list with ‘-c‘ options.

10. Search Commands in History Using Grep Command

Search command through ‘.bash_history‘ by piping your history file into ‘grep‘ as below. For example, the below command will search and find ‘pwd‘ command from the history list.

[[email protected] ~]$ history | grep pwd 113 2013-06-09 10:40:12 pwd 141 2013-06-09 10:40:12 pwd 198 2013-06-09 15:46:23 history | grep pwd 202 2013-06-09 15:47:39 history | grep pwd

11. Search Lastly Executed Command

Search previously executed command with ‘Ctrl+r’ command. Once you’ve found the command you’re looking for, press ‘Enter‘ to execute the same else press ‘esc‘ to cancel it.

(reverse-i-search)`source ': source .bash_profile

12. Recall Last Executed Command

Recall a previously used specific command. Combination of Bang and 8 (!8) command will recall number 8 command which you have executed.

13. Recall Lastly Executed Specific Command

Recall previously used command (netstat -np | grep 22) with ‘!‘ and followed by some letters of that particular command.

[[email protected] ~]$ !net netstat -np | grep 22 (No info could be read for "-p": geteuid()=501 but you should be root.) tcp 0 68 192.168.50.2:22 192.168.50.1:1857 ESTABLISHED - tcp 0 0 192.168.50.2:22 192.168.50.1:2516 ESTABLISHED - unix 2 [ ] DGRAM 12284 - @/org/freedesktop/hal/udev_event unix 3 [ ] STREAM CONNECTED 14522 - unix 2 [ ] DGRAM 13622 - unix 3 [ ] STREAM CONNECTED 12250 - @/var/run/hald/dbus-ujAjOMNa0g unix 3 [ ] STREAM CONNECTED 12249 - unix 3 [ ] STREAM CONNECTED 12228 - /var/run/dbus/system_bus_socket unix 3 [ ] STREAM CONNECTED 12227 -

We have tried to highlight power of history command. However, this is not end of it. Please share your experience of history command with us through our comment box below.

Источник

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