Script to save data from bash_history?

New to Debian (Or Linux in general)? Ask your questions here!

Script to save data from bash_history?

Postby emil_21 » 2017-04-24 17:46

I want to keep in a file history of all 'apt-get install' commands i am running. Is it possible with a bash script, that will be set to run every 24 hours, to check .bash_history file for new lines that contain 'apt-get install' and copy the lines in a text file?
emil_21
 
Posts: 22
Joined: 2017-04-14 06:13

Re: Script to save data from bash_history?

Postby phenest » 2017-04-24 18:16

That's completely possible. What have you come up with so far?
NEC Spirit 550 P4 3.8GHz HT - 2GB RAM - nVidia 7600GT - Pioneer BDR-209DBK
ASUS Sabertooth P67 i7 3770K - 32GB RAM - 2x nVidia 660GTX SLI'd
User avatar
phenest
 
Posts: 1571
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

Postby phenest » 2017-04-24 18:40

You're over-thinking the problem. It would be better to add the "apt-get" command to a file each time it's invoked. That would save doing a search and running any cron jobs.

I have something that will do what you want.
NEC Spirit 550 P4 3.8GHz HT - 2GB RAM - nVidia 7600GT - Pioneer BDR-209DBK
ASUS Sabertooth P67 i7 3770K - 32GB RAM - 2x nVidia 660GTX SLI'd
User avatar
phenest
 
Posts: 1571
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

Postby aplistir » 2017-04-24 19:05

What if you or the user accidentally installs with
Code: Select all
apt install

or
Code: Select all
aptitude install


Do you want to save those too?
aplistir
 
Posts: 130
Joined: 2014-03-26 22:11

Re: Script to save data from bash_history?

Postby Dai_trying » 2017-04-24 19:06

These command are already logged in /var/log/apt/history.log, it give you user name, date/time started and stopped and all packages (dependencies) installed.
Dai_trying
 
Posts: 313
Joined: 2016-01-07 12:25

Re: Script to save data from bash_history?

Postby phenest » 2017-04-24 19:14

Only the successful installations are recorded. Aborted calls are not, for example. You might want those too.
NEC Spirit 550 P4 3.8GHz HT - 2GB RAM - nVidia 7600GT - Pioneer BDR-209DBK
ASUS Sabertooth P67 i7 3770K - 32GB RAM - 2x nVidia 660GTX SLI'd
User avatar
phenest
 
Posts: 1571
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

Postby emil_21 » 2017-04-24 21:36

I didn't know about history.log. Will be useful.

I was thinking about script like this:
Code: Select all
grep -a 'apt-get install' .bash_history >> history.txt


But it will generate duplicate lines which i have to clean after that which is not a big problem if there is no another way. Also i don't know how to setup the script to run every 24 hours.
emil_21
 
Posts: 22
Joined: 2017-04-14 06:13

Re: Script to save data from bash_history?

Postby debiman » 2017-04-25 06:10

Dai_trying wrote:These command are already logged in /var/log/apt/history.log, it give you user name, date/time started and stopped and all packages (dependencies) installed.

this.

i wish there was a +1 button here somewhere.
User avatar
debiman
 
Posts: 1164
Joined: 2013-03-12 07:18

Re: Script to save data from bash_history?

Postby phenest » 2017-04-25 07:45

Does the OP only want the "apt-get install" commands? The contents of /var/log/apt/history.log shows more than that. So it would still mean a script to parse that.
NEC Spirit 550 P4 3.8GHz HT - 2GB RAM - nVidia 7600GT - Pioneer BDR-209DBK
ASUS Sabertooth P67 i7 3770K - 32GB RAM - 2x nVidia 660GTX SLI'd
User avatar
phenest
 
Posts: 1571
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

Postby Dai_trying » 2017-04-25 08:02

One line could easily do that...

grep Commandline /var/log/apt/history.log
Dai_trying
 
Posts: 313
Joined: 2016-01-07 12:25

Re: Script to save data from bash_history?

Postby debiman » 2017-04-25 15:31

^ fishing for compliments now? ;)

but yes, it's a no-brainer.
except you need a second oneliner:
Code: Select all
zcat history*gz|grep Commandline
User avatar
debiman
 
Posts: 1164
Joined: 2013-03-12 07:18

Re: Script to save data from bash_history?

Postby Dai_trying » 2017-04-25 15:42

debiman wrote:^ fishing for compliments now? ;)

Noooo, but I do accept them when given :D

debiman wrote:but yes, it's a no-brainer.
except you need a second oneliner:
Code: Select all
zcat history*gz|grep Commandline

Useful if your logs are rolled (mine aren't by default)
Dai_trying
 
Posts: 313
Joined: 2016-01-07 12:25

Re: Script to save data from bash_history?

Postby dermitdemdino » 2017-04-25 16:23

User avatar
dermitdemdino
 
Posts: 3
Joined: 2017-04-17 06:33
Location: Hückeswagen


Return to Beginners Questions

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable