Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Script to save data from bash_history?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
emil_21
Posts: 98
Joined: 2017-04-14 06:13

Script to save data from bash_history?

#1 Post by emil_21 »

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?

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

#2 Post by phenest »

That's completely possible. What have you come up with so far?
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

#3 Post by phenest »

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.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

aplistir
Posts: 141
Joined: 2014-03-26 22:11

Re: Script to save data from bash_history?

#4 Post by aplistir »

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?

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Script to save data from bash_history?

#5 Post by Dai_trying »

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.

User avatar
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

#6 Post by phenest »

Only the successful installations are recorded. Aborted calls are not, for example. You might want those too.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

emil_21
Posts: 98
Joined: 2017-04-14 06:13

Re: Script to save data from bash_history?

#7 Post by emil_21 »

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.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Script to save data from bash_history?

#8 Post by debiman »

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
phenest
Posts: 1702
Joined: 2010-03-09 09:38
Location: The Matrix

Re: Script to save data from bash_history?

#9 Post by phenest »

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.
ASRock H77 Pro4-M i7 3770K - 32GB RAM - Pioneer BDR-209D

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Script to save data from bash_history?

#10 Post by Dai_trying »

One line could easily do that...

grep Commandline /var/log/apt/history.log

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: Script to save data from bash_history?

#11 Post by debiman »

^ 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

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: Script to save data from bash_history?

#12 Post by Dai_trying »

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)


Post Reply