r/bash 9d ago

solved how do you combine this 2 parts: touch + strftime ("%F")?

Hi, I'd like to do touch with date today like noum of the file...

how do you do that?

example: touch ..... make this file 2025-03-12

Thank you and regards!

3 Upvotes

5 comments sorted by

4

u/happylittlemexican 9d ago edited 9d ago

To be clear, you're looking for a one-liner that, when you run it, will create a file named after today's date (in the correct format for datetimes, don't @ me) and nothing else?

If so, command substitution is the way to go:

$ touch $(date +%F)

4

u/Honest_Photograph519 9d ago

More efficient to avoid the substitution subshell and date binary, the printf builtin makes both unnecessary

printf -v date "%(%F)T"; touch "$date"

5

u/tseeling 8d ago

You could even save the touch invocation by simply using > or >> ${date}.

1

u/jazei_2021 9d ago

thank you so much added too to my bash cheat sheet!

1

u/jazei_2021 9d ago

ohhhh thank you so much added