Creating individual bash scripts from Linux History

I want to be able to output my Linux bash history to a file. Then line by line create a new shell script with the content of the of each line. I have tried this so far from the command line..

for example

The output of the history file

73  nmap -T4 -A -v 127.0.0.1
74  nmap -T4 -A -v 192.168.0.1/24

My first problem was the line numbers ... To remove them I applied this command.

history | cut -c 8- > one.txt

Which gave me an output like this ..

nmap -T4 -A -v 127.0.0.1
nmap -T4 -A -v 192.168.0.1/24

There is a problem here as the text contains spaces

cat one.txt | tr -cd '[:alnum:]\n\r~!@#$%^&*()-_=+{}\|;:<>,./?"`' | sed '/^$/d' > bar.txt

I used the above command to removed the illegal characters that Linux doesn't like to be included in a file name.

This is where I have hit the wall...

It's clunky and messy...

There has to a simpler way that is prettier...


Do not bother trying to find out how to write your history out to a file. Your Ubuntu system already does by default to the file ~/.bash_history. So it takes a simple

cat ~/.bash_history

to retrieve the contents of the file.