code-test

2014年4月6日 星期日

將Terminal 的session儲存log

Save all of your Terminal activity to a file UNIX
I use Terminal everyday, and I find it a good idea to log everything I you. It makes it much easier to undo your mistakes when you know what those mistakes were. Here's how I do this: 

Open Terminal's preferences. 

Go to Settings, then Shell. You can choose here to run a command at startup. You could create a simple log of your session using the following:
/usr/bin/script ~/Desktop/Terminal.log
This will log everything you do and append it to the log file. 

I like to keep my history so instead I wrote this small script that archives previous sessions by renaming the file with a date/time string. I then set Terminal.app to run at startup the following command:
~/Desktop/logger.sh
Here's the script:
#!/bin/bash 
# logger.sh 
# Log everything you do in Terminal. 
 
#* Formatted date & time string. 
FORMATTED_DATE=`/bin/date "+%Y-%m-%d%H%M%S"` 
 
#* Archive the previous file 
/bin/cp -f ~/Desktop/Terminal.log{,.$FORMATTED_DATE.txt} 
 
#* Begin a new one 
/usr/bin/script ~/Desktop/Terminal.log
 
[kirkmc adds: This is a good idea. I don't use Terminal a lot, but I find that I sometimes need to remember a command I ran in the past which is no longer in my history. 

Also, this is similar to something I do with texts I write. I do most of my writing in BBEdit, and I have a number of "scratch" files which I change every year. For example, I have one file for most of what I write, another just for Mac OS X Hints, and another for a specific client I write for. I archive these files at the end of the year, and create new ones. If I ever need to go back to these files to look for something I can do so. I don't bother to enter dates - which I could do easily enough with a TypeIt4Me shortcut - but if I'm looking for something I'll generally know what text to search for.]

沒有留言:

張貼留言