You are not logged in.

#1 2018-03-05 08:43

bin
Member
From: U.K.
Registered: 2016-01-28
Posts: 560

Accessing previous command line entries

Thought I'd just throw this in as a reminder....

Stuff you enter on the command line is all held in your .bash_history file.

You can access all those old commands by either loading that file in a text editor such as write - or you can use the 'history' command.

Just run history and then you can re-run a command just using copy and paste.

However, used with caution but even cooler, just type ! followed by the command number in the list.

I find this really handy as I merge all my old .bash_history files from wherever, clean out the junk and then make it my current file. That way I have easy access to all sorts of obscure stuff that may still be valid.

Offline

#2 2018-03-05 09:29

Dai_trying
Member
From: UK
Registered: 2015-12-14
Posts: 2,212
Website

Re: Accessing previous command line entries

I have a handy tiny little script I use to remove duplicates from my bash_history (I use the terminal a lot) and this simply removes all duplicates and sorts the history file in alphabetical order, it usually only takes a fraction of a second to complete.

#!/bin/bash

cat ~/.bash_history | sort -u >~/new_bash_history
mv ~/new_bash_history ~/.bash_history

As you can see it puts the file through sort with the unique option and then overwrites the old history file with the new one. smile

Last edited by Dai_trying (2018-03-05 09:30)

Offline

#3 2018-03-05 10:04

bin
Member
From: U.K.
Registered: 2016-01-28
Posts: 560

Re: Accessing previous command line entries

That's neat!

Sorting to alphabetical order confuses me as I tend to look for commands grouped around a specific task. For example I'll do a number of tests on backports + no-recommends commands for a package to keep cruft to a minimum - or realise that actually a 'suggested' might have been a good idea and so add it to the command. So, I use

awk '!a[$0]++' .bash_copy > .new_bash_history

Which just cleans out duplicates and leaves the file in original order.

Offline

#4 2018-03-05 10:21

Dai_trying
Member
From: UK
Registered: 2015-12-14
Posts: 2,212
Website

Re: Accessing previous command line entries

I've got used to having mine in alphabetical order now as I've been using this script for years but I might try your method and see how I get on, I can see it being useful to keep them in chronological order, thanks for sharing smile

Offline

#5 2018-03-05 12:32

chris_elvidge
Member
Registered: 2016-05-15
Posts: 13

Re: Accessing previous command line entries

If you want to keep duplicates out of your history, see man bash / HISTCONTROL

Offline

#6 2018-03-05 15:21

Dai_trying
Member
From: UK
Registered: 2015-12-14
Posts: 2,212
Website

Re: Accessing previous command line entries

Thanks for that Chris, I did have ignoreboth in my bashrc but I think the better option (for me) is erasedups which I have now added.

Offline

Board footer

Powered by FluxBB