Linux History - Present Progressive

30.10.2022 - Sunday - 15:49

Linux History

This is going to be a continues post - whenver I'll find something new I will add it.


Linux history is such a greate utility of linux.
If you don't know why I am saying that, this post is for you

Lets start with a story
Lets say you executed a very long command few minutes ago.
Now you have to do it again :(
You probably don't have the power to write it all again. You would lik a tool for that.
Did you ever struggled with the up arrow searching for a command you did?

So How Do You Solve That?

You can use all the utilities that are suggested with the histroy utlities.




Execute command by its index

First of all you can use the basic command with grep <part_of_command> and get the command you want.
For example:
>> history | grep "list.xml"
6 cat list.xml
7 vim list.xml
...
900 cat list.xml | grep name
With this output you can do multiple things -
You can execute a command by its index (the number in the left side):
!7
Or
!900
Or any other number that is in the index number



Execute last command

Another basic use is to exeute the last command you did.
Instead if writing the whole command again just do:
!!



Execute command by counting backwards

If you know that the last command was 3 command backward, or any number you have, lets say it n.
You can use the history exclamation mark with minus after it, and n like this:
>> !-<n> For example, you know you want to execute 10 command back:
>> !-10



Execute command that starts with string you know

If you know the starting string of the command you want to do, and its the last time you did a command that start with that string,
then you can use it with exclamatiom mark.
For example: If the command you want to do is cat /etc/passwd and that's the last time you used cat at the begining of a command
Then you can do:
!cat
This will execute the last command you did that starts with cat which is the command you want



Use command argument in next commands

Lets say you used a command that have a very long name in the second argument, for example:
>> ls firstdir second_dir_that_have_a_very_long_name_that_never_ends_and_more
Now, lets say you want to use this name and not have to retype it, or autocomplete it, which both can take minutes......
So you can do:
echo first_thing_to-echo !!:2
If you want to use the 7th argument of 3rd command backwards
echo !-3:7

You can use all the rules of history here and add :<num> in the end, for using nth argument of the command in history.



Print command

In case you only want to print the comnmand in the history then you can do:
For example you want to print the 20th command backwards:
>> !-20:p
Or if you want to print the 15th command backwards 3rd argument:
>> !-15:p
Or if you want to print the 50th command 2rd argument:
>> !50:p



Interactive search of command backwards

First of all do <C-r>
You will get this in the command line:
(reverse-i-search)`':
Start typing and you will get the last command that contains the string you are typing.



Use arguments from the current command line in the command you are in

Lets say you want to print the same argument twice in an echo command.
Lets say again that this argument is very long and you don't want to print it manually twice.
For example:
>> echo this_is_a_long_parameter_that_i_dont_want_to_print_manually_twice short_one third

What you can do is:
>> echo this_is_a_long_parameter_that_i_dont_want_to_print_manually_twice short_one third !#:1
and the output will be:
>> echo this_is_a_long_parameter_that_i_dont_want_to_print_manually_twice short_one third this_is_a_long_parameter_that_i_dont_want_to_print_manually_twice



Run a command without using history - multiple ways

The first usage is just not put the command in history:
>> <space>echo "some text"
Just start the command with space and it won't be recognized as a command by history.

To disable history temporarily:
>> <space>set +o history
or
>> <space>shopt -uo history

To enable it back:
>> <space>set -o history
or
>> <space>shopt -so history

To disable hisotry for the current session
>> unset HISTFILE
This file is where the history is saved. You can see first where it is with echo $HISTFILE or see its content with cat $HISTFILE
After you unset it, no history will be saved. To enable history back, just set it to whatever file you want.

To remove a command from history, first find its number:
>>history | grep <part_of_command>
output:
... 101 <command1>
102 <command_to_remove>
103 <command2>
Assume you want to the command_to_remove which is number 102
So do the command:
>> history -d 102