0

Is there any command or way to remove all entries from history of bash shell containing a particular string? this will be useful to remove commands in history containing password. I know we can remove each history entry by its number but the issue is it deletes only one entry at a time and I need to take out number each time to remove a new entry.

eg. History command shows 5 entries containing password abcabc and I want to remove all the entries from history command containing string abcabc

975  2019-03-15 11:20:30 ll
  976  2019-03-15 11:20:33 ll cd
  977  2019-03-15 11:20:36 ll CD
  978  2019-03-15 11:20:45 chown test1:test1 CD
  979  2019-03-15 11:20:53 chown test1:test1 ./CD
  980  2019-03-15 11:20:57 chown test1:test1 .\CD
  981  2019-03-15 11:22:04 cd /tmp/logs/
  982  2019-06-07 10:36:33 su test1
  983  2019-08-22 08:35:10 su user1
  984  2019-08-22 08:35:15 /opt/abc/legacy.exe -password abcabc
  985  2019-09-24 07:20:45 cd /opt/test1/v6r2017x
  986  2019-09-24 07:20:46 ll
  987  2019-09-24 07:21:18 cd /tmp/
  988  2019-09-24 07:21:19 ll
  989  2019-09-24 07:21:24 cd linux_a64/
  990  2019-09-24 07:21:25 /opt/abc/legacy.exe -password abcabc
  991  2019-09-24 07:24:03 cd  build/
  992  2019-09-24 07:24:04 ll
  993  2019-09-24 07:24:07 cd ..
  994  2019-09-24 07:24:10 /opt/abc/legacy.exe -password abcabc
  995  2019-09-24 07:24:15 cd someapp/bin
  996  2019-09-24 07:24:21 ll
  997  2019-09-24 07:24:33 cd .
  998  2019-09-24 07:24:35 cd ..
  999  2019-09-24 07:24:36 ll

Tried following command which gave error as given below

servername:~ # sed -i 'g/abcabc/d' /home/user1/.bash_history
sed: -e expression #1, char 2: extra characters after command

Expectation : No error and all the entries containing string abcabc should be removed.

0

you could use sed to remove entries from .bash_history.
But as it's a password to avoid typing the password again in the CLI I would advise you to edit your .bashrc file and insure that the line with HISTCONTROL is set to ignoreboth or ignorespace. This would allow you to type command in bash and not put them in history if they start by a space:

promtpt:$ sed -i '/abcabc/d' /home/user/.bash_history

Notice the space between $ and the command this would make the line ignored in bash history.

-i: means edit file inline.
d in the end mean remove line containing expression between /.

This solution only works on Linux GNU sed.

| improve this answer | |
  • Isn't the history sometimes not written immediately, such that some pending writes to it might be pending, overwriting changes made to the file directly? – Royce Williams Nov 25 '19 at 22:23
  • Hi Kiwy, I tried your command but its gving me error as given below sed: -e expression #1, char 2: extra characters after command – Amol Patil Dec 6 '19 at 8:06
  • @AmolPatil could you post the full command you typed ? and add edit your question so I can help you better – Kiwy Dec 6 '19 at 15:04
  • @Kiwy Hi, I have added an example with the error in the question as requested by you. – Amol Patil Dec 13 '19 at 14:34
  • @AmolPatil I'm so sorry, delete command for sed do not need g flag. it should now work – Kiwy Dec 13 '19 at 14:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.