Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need linux equivalent to windows "echo %date% %time% %COMPUTERNAME%"

Tags:

In Linux,

"echo %date% %time% %COMPUTERNAME%"

returns

%date% %time% %COMPUTERNAME%

not

Fri 09/24/2010 10:46:25.42 WXP2010043001

as Windows does. I need to be able to do this for the logs I'm setting up.

like image 927
Captain Claptrap Avatar asked Sep 24 '10 14:09

Captain Claptrap


3 Answers

Use the date command with a format like this:

date +"%m/%d/%Y %H:%M:%S $HOSTNAME"

To get hundredths of seconds, you may need to do some text processing like this:

DATE=date +'%m/%d/%Y %H:%M:%S.%N'
DATE=${DATE%???????}
DATE="$DATE $HOSTNAME"

This is because date offers seconds, nanoseconds, and nothing in between!

like image 54
Matt K Avatar answered Oct 24 '22 07:10

Matt K


You can do:

dt=$(date)
echo $dt $HOSTNAME
like image 35
codaddict Avatar answered Oct 24 '22 07:10

codaddict


echo $(date '+%Y %b %d %H:%M') Your output $HOSTNAME     

Outputs:

2013 Nov 01 09:11 Your output PEGASUS-SYDNEY-CL2
like image 29
Mark Casey Avatar answered Oct 24 '22 07:10

Mark Casey