Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active December 24, 2018 10:24
Show Gist options
  • Save na0AaooQ/7b7652ba10df4affa0f5 to your computer and use it in GitHub Desktop.
Save na0AaooQ/7b7652ba10df4affa0f5 to your computer and use it in GitHub Desktop.
dateコマンドでバックアップファイル作成時にファイル名に日付_時間を付ける (LinuxとWindows用) ref: https://qiita.com/na0AaooQ/items/c26b637261d9962f981f
$ cp -p バックアップ対象ファイル名 バックアップファイル名.`date "+%Y%m%d_%H%M%S"`
D:\> copy バックアップ対象ファイル名 バックアップファイル名.%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
# date
Fri Jan 23 02:48:40 JST 2015
# date "+%Y%m%d_%H%M%S"
20150123_024843
# cp -p /etc/fstab /etc/fstab.`date "+%Y%m%d_%H%M%S"`
#
# ll /etc/fstab.20150123_024858
-rw-r--r--. 1 root root 584 Oct 3 01:59 /etc/fstab.20150123_024858
#
# ll /etc/fstab
-rw-r--r--. 1 root root 584 Oct 3 01:59 /etc/fstab
#
# diff /etc/fstab /etc/fstab.20150123_024858
#
D:\tools>echo %date%
2018/12/24
D:\tools>echo %time%
19:06:28.93
D:\tools>
D:\tools>echo %date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
20181224_190612
D:\tools>
D:\tools>copy example.txt example.txt.%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
1 個のファイルをコピーしました。
D:\tools>
D:\tools>dir
 (中略)
D:\tools のディレクトリ
2018/12/24 19:09 <DIR> .
2018/12/24 19:09 <DIR> ..
2018/12/24 19:01 4 example.txt
2018/12/24 19:01 4 example.txt.20181224_190922
 (中略)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment