Cómo eliminar un solo comando del historial en la shell bash de GNU Linux

En algunas ocasiones queremos eliminar un solo comando del historial y la forma mas habitual de borrar el historial borra absolutamente todo. Por eso hoy vamos ver cómo eliminar un solo comando del historial en la shell bash de Linux de una forma fácil.

Encontrar el numero del historial

Para poder borrar la entrada con numero 3 del historial, lo primero es identificarlo.
Tomaremos como ejemplo el siguiente historial:

root@delhistory:~# history
    1  echo test
    2  echo 1
    3  top
    4  history
root@delhistory:~# 

Por defecto esto se guarda en el fichero ~/.bash_history. Se puede cambiar el nombre del fichero modificando la variable $HISTFILE que es la encargada de definirlo.

Borrar todo el historial

Como hemos comentado antes, lo más común es eliminar el historial, para hacer el vaciado de este historial se puede con:

history -c

Para borrar una sola linea

Como hemos comentado para borrar una linea del historial, debemos conocer su número. Sabiendo este numero ejecutaremos history -d NUMERO_DE_LINEA.

En nuestro caso queremos eliminar la linea:

3  top

Por lo que ejecutaremos:

history -d 3

Y el historial se quedará así:

root@delhistory:~# history
    1  echo test
    2  echo 1
    3  top
    4  history
root@delhistory:~# history -d 3
root@delhistory:~# history
    1  echo test
    2  echo 1
    3  history
    4  history -d 3
    5  history
root@delhistory:~# 

Como hacer que no se guarden los comandos en el historial

Se puede deshabilitar el historial para la sesión actual ejecutando:

unset HISTFILE
#COMANDOS A EJECUTAR

Otra opción es enlazar el fichero de historial a /dev/null.

Bonus sobre historial de bash

Establecer el numero máximo de lineas del fichero de historial

La variable HISTFILESIZE establece el número máximo de lineas de historial.
Se puede establecer así (habrá que hacerlo en cada sesión o meterlo en el .bashrc del usuario).

HISTFILESIZE=5000000

Más cosas que puedes hacer en con el historial

Para ver que mas cosas se pueden hacer con el historial puedes consultar la opción “–help

root@delhistory:~# history --help
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
    
    Display the history list with line numbers, prefixing each modified
    entry with a `*'.  An argument of N lists only the last N entries.
    
    Options:
      -c	clear the history list by deleting all of the entries
      -d offset	delete the history entry at position OFFSET.
    
      -a	append history lines from this session to the history file
      -n	read all history lines not already read from the history file
    		and append them to the history list
      -r	read the history file and append the contents to the history
    		list
      -w	write the current history to the history file
    
      -p	perform history expansion on each ARG and display the result
    		without storing it in the history list
      -s	append the ARGs to the history list as a single entry
    
    If FILENAME is given, it is used as the history file.  Otherwise,
    if HISTFILE has a value, that is used, else ~/.bash_history.
    
    If the HISTTIMEFORMAT variable is set and not null, its value is used
    as a format string for strftime(3) to print the time stamp associated
    with each displayed history entry.  No time stamps are printed otherwise.
    
    Exit Status:
    Returns success unless an invalid option is given or an error occurs.
root@delhistory:~# 

Deja una respuesta