AAtsushi's Blog
Infrastructure

【Linux Tips】Output Command Results to a Datetime-Named File

→ 日本語版を読む

Outputting command results to a datetime-named file was cumbersome, so I registered an alias to streamline the process.

Setting up the alias

$ alias logd='touch log.`date +%Y%m%d%H%M%S` && ls log.`date +%Y%m%d%H%M%S`'

The alias above creates a datetime-named file with touch and returns the filename with ls. (If there's a simpler way to write this, please let me know)

The filename format is "log.20231210015944".

How to use

Use it as follows. Pipe the command output to tee to both display the result and write it to a file (log.YYYYmmddHHMMSS).

$ [command] | tee $(logd)

The Old Way

$ [command] | tee log.$(date +%Y%m%d%H%M%S)

I used to create the filename each time using the date command like this. I kept forgetting the date command format, so it was a hassle to look it up every time.

(Yes, I should probably just memorize the format...)

That's all.