Score:1

Creating individual bash scripts from Linux History

cn flag

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

Bruni avatar
cn flag
Even though @vanadium s answer does not answer the question, by using the `~/.bash_history` file and not the history command, you would circumvent all problems you described.
james  avatar
cn flag
It solves one of the issues. Granted but as you said it not the answer in which I am looking for. Thank you ..
Score:1
cn flag

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.

james  avatar
cn flag
I am aware of that and maybe I didn't explain myself well.. What I wish to do is turn the history log - line by line into individual shell scripts ... For each command in the history log..
vanadium avatar
cn flag
No indeed you didn't. There is a lot of explanation, first on just getting rid of the number. So in the end, you just want to split a text file into individual lines.
Score:0
cn flag

To split the history file into files by line

split --verbose -l1 ~/.bash_history

(do this on a copy in a separate directory)

To add the shebang:

for file in *; do 
    sed  -i '1i #!/bin/sh' $file
done
james  avatar
cn flag
That is brilliant thank you so much ..
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.