Batch Files – Create New Folder Using Current Date and Time in Folder Name

Share on LinkedIn

This would be great for backup jobs or copying files where you want to keep track of when it was done.

In my simple batch file, I’m copying contents from a mapped network drive to my local machine.

Originally, I was using the following command:

xcopy S: C:\Users\james\Documents\Backup_WIN_Server\SysAdmin /c /v /f /i /s /e /h /y

If using this for a simple form of backup, the data will be overwritten each time I run the batch file.

In order to create a new directory each time with a time/date stamp, all I need to do is use a substring and the built-in %DATE% and %TIME% variables. In this example, I’m just adding the substring before my xcopy command.

@echo OFF
:: Use date /t and time /t from the command line to get the format of your date and
:: time; change the substring below as needed.
:: This will create a timestamp like yyyy-mm-dd-hh-mm-ss.
set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
@echo TIMESTAMP=%TIMESTAMP%

 

Finally, I just need to add that timestamp to my destination folder as such:

xcopy S: C:\Users\james\Documents\Backup_WIN_Server\"%1\%TIMESTAMP%-SysAdmin" /c /v /f /i /s /e /h /y
CentOS 7

Show Display Date And Time For Command History

Share on LinkedIn

How would you display shell command history with date and time under Linux or UNIX operating systems? For instance, CentOS?

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. Defining the environment variable as follows:

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

OR

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

To see history type

history

Sample Output:

1002 29/01/15 11:11:07 hwclock --systohc
1003 29/01/15 11:11:10 date
1004 29/01/15 11:11:22 hwclock -show
1005 29/01/15 11:11:25 hwclock -r
1006 29/01/15 11:11:32 history
1007 29/01/15 11:21:15 ip addr
1008 29/01/15 11:21:19 show ip addr
1009 29/01/15 11:21:28 history | grep ip
1010 29/01/15 11:22:34 ip addr sh
1011 29/01/15 11:22:42 hostname
1012 29/01/15 11:22:49 ifconfig
1013 29/01/15 11:24:01 history

Note: Date and Time will only appear for new commands. All previous commands will be stamped with the time and date you issued the ‘HISTTIMEFORMAT’ command.

CentOS 7

How To Change Time/Date In Linux From Command Prompt

Share on LinkedIn

These commands work with CentOS 6.5. The commands for CentOS 7 are similar and you should be able to figure them out if you start with these commands.

Only change time not date:

To only set the time use like this:

# date -s hh:mm:ss

Update Hardware clock:

To sync the hardware clock to the current system clock:

# hwclock –systohc
OR
# hwclock –systohc –utc
***** use the second option if you use UTC.

Set Hardware clock manually:

To know the current time of the hardware clock:

# hwclock –show
Tue Nov  4 22:13:40 2003  -0.684660 seconds

To set the hw clock manually:

# hwclock –set –date=”09/21/2005 14:23:23″

 

We run into power issues from time to time and our servers have been known to lose their time. The hardware time may not have been changed and the system often synchronizes off of the hardware clock, especially following a power cycle.

Below are the steps I performed to 1. change the system clock to the correct time and then 2. synchronize the hardware clock up to the system clock.

[root@ ~]# date -s 16:30:00
Mon Jan 26 16:30:00 CST 2015
[root@ ~]# date
Mon Jan 26 16:30:05 CST 2015
[root@ ~]# hwclock –show
Mon 26 Jan 2015 03:09:58 PM CST -0.188457 seconds
[root@ ~]# hwclock –systohc –utc
[root@ ~]# hwclock –show
Mon 26 Jan 2015 04:35:40 PM CST -0.616988 seconds