5

I want to have a complete and sorted list of my bash history. But there are some problems:

  1. By doing history the list never shows the complete list and only 1000 commands are shown.
  2. In my $HOME directory there is a file named .bash_history that shows 2000 results.
  3. When I want to make a sorted .txt file out of my history I do:

    history | sort > History.txt
    

    But because of numbers the commands are not sorted alphabetically and are sorted by number.

Is there any way to do what I want?

1
7

How about:

cat ~/.bash_history | sort > Sorted_history.txt

to have an unlimited bash history, have a look at this QA:

https://stackoverflow.com/questions/9457233/unlimited-bash-history

3
  • 3
    and if you don't want exact repeats, you could "cat ~/.bash_history | sort | uniq > Sorted_history.txt"
    – jpezz
    Jun 3 '18 at 19:46
  • 4
    @jpezz: or more simply < ~/.bash_history sort -u > Sorted_history.txt. And you can use history -a first to "append history lines from this session to the history file". Jun 4 '18 at 5:05
  • history | cut -c 30- | sort -u -i | ack "ssh" will (for me) cut off the variable parts (time codes and indexing) and then sort. I do get some duplicates, I think because of different whitespace. Pipe it through "ack" as shown to get simple searching.
    – pbhj
    Jun 5 '18 at 15:07
7

sort can sort input by the field specified by the user to -k, so strictly speaking something like this would be what you are looking for.

history | sort -k2 > History.txt  # or -K3 if you have $HISTTIMEFORMAT, etc set

Additionally, there's often a difference between what's available in the output of history command vs what's recorded in ~/.bash_history in that the latter is not updated after every command (is usually updated only on logout), so using history | sort .. is better at giving you a current view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy