Using Unix Date for Bash Filename Timestamp

I’ve started using a neat Bash trick for inserting time stamps into file names — insert a timestamp using command substitution (posix specification):

wp db export$(date +%Y-%m-%d--%H-%M-%S).sql

# gives something like

Yes, this is a pain to type out — the first time.

But going to the effort of writing this out allows us to quickly recall a command which gives us a filename with a “now” time stamp.

I didn’t learn about using ctrl+r in bash until fairly recently. If you don’t know about it, you can do a reverse search through your bash history. Using the above command substitution technique, I can recall a previous dump with a ‘now’ timestamp with just a few keystrokes:

<ctrl+r> wp db ex<enter>

See the below screencast for an example.

I find this particularly helpful with database dumps and tar creation commands!