Display .bash_history with timestamp using script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display .bash_history with timestamp using script
# 1  
Old 09-23-2013
Display .bash_history with timestamp using script

Hi would like to ask if there is anyway to display .bash_history with timestamp using shell script?

i know that you should use history command with
Code:
HISTTIMEFORMAT="%d/%m/%y %T "

to display it in terminal but it does not work when i use it on shell script. It seem that you can't run history command on script. i can use cat ~/.bash_history on script but it does not contain the timestamp.

Last edited by vbe; 09-23-2013 at 05:40 AM..
# 2  
Old 09-23-2013
Hi.

An example:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate bash history in script.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

set -o history
pl " Results from history in script, first call."
echo " Hello, world from bash."

HISTTIMEFORMAT="%d/%m/%y %T "
history

pl " Results from history in script, second call."
echo " another Hello, world from bash."
history

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39

-----
 Results from history in script, first call.
 Hello, world from bash.
    1  23/09/13 06:09:08 pl " Results from history in script, first call."
    2  23/09/13 06:09:08 echo " Hello, world from bash."
    3  23/09/13 06:09:08 HISTTIMEFORMAT="%d/%m/%y %T "
    4  23/09/13 06:09:08 history

-----
 Results from history in script, second call.
 another Hello, world from bash.
    1  23/09/13 06:09:08 pl " Results from history in script, first call."
    2  23/09/13 06:09:08 echo " Hello, world from bash."
    3  23/09/13 06:09:08 HISTTIMEFORMAT="%d/%m/%y %T "
    4  23/09/13 06:09:08 history
    5  23/09/13 06:09:08 pl " Results from history in script, second call."
    6  23/09/13 06:09:08 echo " another Hello, world from bash."
    7  23/09/13 06:09:08 history

See man bash and Google for details.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display lines between timestamp

Hi Gurus, I have a software which logs event in the log file and it has become to big to search into it. I want to display all the lines from the log files between <Jul 21, 2016 3:30:37 PM BST> to <Jul 21, 2016 3:45:37 PM BST> that is 15 min data . Please help Use code tags, thanks. (10 Replies)
Discussion started by: guddu_12
10 Replies

2. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

3. UNIX for Dummies Questions & Answers

Display files based on particular file timestamp

Hi, I have requirement to list out files that are created after particular file. ex. I have below files in my directory. I want to display files created after /dirdat/CG1/cg004440 file. ./dirdat/CG1/cg004438 09/07/14 0:44:05 ./dirdat/CG1/cg004439 09/07/14 6:01:48 ... (3 Replies)
Discussion started by: tmalik79
3 Replies

4. UNIX for Advanced & Expert Users

How to recover .bash_history?

rm -rf .bash_history some one ran rm -rf .bash_history on my Linux server(SUSE),I can see this command being run in current history, but I want the OLD history as well,can I recover the old history back. (9 Replies)
Discussion started by: Ankit Bansal
9 Replies

5. Shell Programming and Scripting

.bash_history

Dear All, I am creating users on our servers. the .bash_history supposed to store all the commands entered by the user. My question is, how can I prevent the user himself from editing or viewing this file? I have tried chaning the owner of the .bash_history to be the root user but... (5 Replies)
Discussion started by: charbel.n.s
5 Replies

6. UNIX for Advanced & Expert Users

ls -l timestamp display format

The time stamp format using "ls -l" is either mmm dd hh:mm or mmm dd yyyy. For later case, how can I know the hh:mm as well. Thanks. (3 Replies)
Discussion started by: pok.fung
3 Replies

7. UNIX for Advanced & Expert Users

.bash_history

During the course of the session before I logout I see some of the commands listed from my previous session but not from my current session and after I logout and log back in I see the commands which I ran before logging out. Does the .bash_history stay in the buffer or someplace else then? ... (2 Replies)
Discussion started by: jacki
2 Replies

8. Shell Programming and Scripting

Find files older then today & display with timestamp info

Small query- I want to do some operation on all the files older then today. Before I do that operation, i want to verify if the command works properly or not. Surprisingly, the command below returns me file, which are created today - find /mrk_archive/PG/ftp/incomming/gbs/2008 -type f... (2 Replies)
Discussion started by: kedar.mehta
2 Replies

9. UNIX for Advanced & Expert Users

.bash_history

Question for all sysadmins. How do you keep track of what commands each user uses on his account. I thought an easy way is to monitor .bash_history, however those files can be "edited" by the user. Is there a permission combination that will allow the shell to record to it but yet they can't edit... (12 Replies)
Discussion started by: jason6792
12 Replies

10. UNIX for Dummies Questions & Answers

display the timestamp of a server UNIX

I can't find the command shell UNIX to display the timestamp of the current date. It's not an option of the commande date. (3 Replies)
Discussion started by: nicolas
3 Replies
Login or Register to Ask a Question