jasmine214--love

只有当你的内心总是充满快乐、美好的愿望和宁静时,你才能拥有强壮的体魄和明朗、快乐或者宁静的面容。
posts - 731, comments - 60, trackbacks - 0, articles - 0

shell date 相关使用

Posted on 2013-12-27 14:13 幻海蓝梦 阅读(328) 评论(0)  编辑  收藏 所属分类: Shell
#格式化输出                                                                                
$> date +"%Y-%m-%d"                                                                        
2009-12-07                                                                                 
                                                                                           
#输出昨天日期                                                                              
$> date -d "1 day ago" +"%Y-%m-%d"                                                         
2012-11-19                                                                                 
                                                                                           
#2秒后输出                                                                                 
$> date -d "2 second" +"%Y-%m-%d %H:%M.%S"                                                 
2012-11-20 14:21.31                                                                        
                                                                                           
#传说中的 1234567890 秒                                                                    
$> date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"                            
2009-02-13 23:02:30                                                                        
                                                                                           
#普通转格式                                                                                
$> date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"                                               
2009/12/12 00:00.00                                                                        
                                                                                           
#apache格式转换                                                                            
$> date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"                                  
2009-12-05 00:00.37                                                                        
                                                                                           
#格式转换后时间游走                                                                        
$>date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"                        
2007-12-05 00:00.37                                                                        
                                                                                           
#加减操作                                                                                  
date +%Y%m%d                   //显示前天年月日                                            
date -d "+1 day" +%Y%m%d       //显示前一天的日期                                          
date -d "-1 day" +%Y%m%d       //显示后一天的日期                                          
date -d "-1 month" +%Y%m%d     //显示上一月的日期                                          
date -d "+1 month" +%Y%m%d     //显示下一月的日期                                          
date -d "-1 year" +%Y%m%d      //显示前一年的日期                                          
date -d "+1 year" +%Y%m%d      //显示下一年的日期                                          
                                                                                           
                                                                                           
#设定时间                                                                                  
date -s                        //设置当前时间,只有root权限才能设置,其他只能查看          
date -s 20120523               //设置成20120523,这样会把具体时间设置成空00:00:00          
date -s 01:01:01               //设置具体时间,不会对日期做更改                            
date -s "01:01:01 2012-05-23"  //这样可以设置全部时间                                      
date -s "01:01:01 20120523"    //这样可以设置全部时间                                      
date -s "2012-05-23 01:01:01"  //这样可以设置全部时间                                      
date -s "20120523 01:01:01"    //这样可以设置全部时间                                      
                                                                                           
有时需要检查一组命令花费的时间,举例:                                                     
                                                                                           
#!/bin/bash                                                                                
                                                                                           
start=$(date +%s)                                                                          
nmap www.1987.name &> /dev/null                                                            
                                                                                           
end=$(date +%s)                                                                            
difference=$(( end - start ))                                                              
echo $difference seconds.                                                                  


















































































































只有注册用户登录后才能发表评论。


网站导航: