Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 23
Hello! When I'm in a terminal I can type history -c , and all the typed commands for a given user will be cleared. Is it possible to do the ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44

    [SOLVED] Clear history in bash

    Hello!

    When I'm in a terminal I can type history -c, and all the typed commands for a given user will be cleared.

    Is it possible to do the same, using a bash script?

    My code is:

    #!/bin/bash
    su -c "history -c"

    But this doesn't seem to work.
    When I execute the script, it does ask me form my root pass, but then nothing happens, since I can still access my old typed commands.

    Ideas?
    Thank you

    PS: As you can see, I'm a Linux begginer.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    If you logged in as "root" , you can omit "su -c" just try using plain
    history -c (instead of "history -c" - I don't think you need to have double quotes .)

    Little more info. history command takes input from file called ".bash_history"
    it will be available on users home directory.

    cat ~/.bash_history
    You can edit this file too like
    cat /dev/null > ~/.bash_history
    will clear the data.

    you can also set the history size in ~/.bashrc by placing the following lines
    export HISTFILESIZE=0
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44
    Hello Lakshmipathi.G

    Thanks for the answer.

    I'll try your suggestions (at this exact moment I'm on a WYNDOUZ machine).

    Thank you
    ADFC

  4. #4
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Ditto what Lak said... either of the following commands work for me as root, or user:

    Code:
    history -c
    
    >~/.bash_history
    Post back with any errors, should you get any.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44
    Hello!

    I'm using openSUSE 11.1 and Gnome (running gnome-terminal).

    In the bash script, simply putting history -c doesn't work!
    It does not return any error, but when the script finishes, I can still access my typed commands using the keyboard cursor (upper key).


    If I try to execute ~/.bash_history in the terminal it returns:
    bash: /home/user/.bash_history: Permission denied


    If I try to execute >~/.bash_history in the terminal it does not return any error, but it doesn't do anything.
    I can still access my typed commands using the keyboard cursor (upper key).


    If I try to execute cat /dev/null > ~/.bash_history in the terminal it does not return any error, but it doesn't do anything.
    I can still access my typed commands using the keyboard cursor (upper key).


    I ran all of those commands as normal user (which is what I need ) in the gnome-terminal and not in bash mode for the moment.

    Any more suggestions?
    Thank you.

  6. #6
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Try the history -c command again, then log out and right back in, and then see if your files changed.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  7. #7
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44
    Quote Originally Posted by ozar View Post
    Try the history -c command again, then log out and right back in, and then see if your files changed.
    No, it doesn't work in the bash script (after log-out and log-in).

    It only works in terminal (manually typing the command and pressing enter).

    I'm confused.

    Ideas?

    Hum... I tried in a different PC (with a Debian based distro) and I have the exact same problem!
    Have you tried making the bash shell script yourself?
    I'm probably doing something wrong, but what??

    Thank you

  8. #8
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Not sure what might be wrong, then. I always run history -c from the console and whenever I log into X the history file has been emptied and started again fresh, except for the startx command just issued to enter into graphics mode.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  9. #9
    Just Joined!
    Join Date
    Jun 2009
    Posts
    44
    Quote Originally Posted by ozar View Post
    Not sure what might be wrong, then. I always run history -c from the console and whenever I log into X the history file has been emptied and started again fresh, except for the startx command just issued to enter into graphics mode.
    Yes, but have you tried making the script with:

    #!/bin/bash
    history -c


    and then running it?

    Thank you

  10. #10
    oz
    oz is online now
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,095
    Ah, I'm with you now. No, I've not tried it in a script because I don't use the command that often and haven't needed a script. I thought you meant the command itself wasn't working. Sorry for any confusion.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...