15

I want to make a file which name will be a current date and time. I can create a file with the touch command. Also I can get current time with the date command. So, I think, I need to somehow pipe the second command to the first one. How can I do that?

2 Answers 2

23

Use the return value from a shell expression as the argument to touch:

touch $( date '+%Y-%m-%d_%H-%M-%S' )

Result: A file named e.g. 2012-03-11_14-33-53.

This answer assumes you're using bash (it's described in the man page section Command Substitution), but other shells will work the same or only slightly different.

8
  • @MdGao And described in the same section of the man page. I prefer the answer I gave though, here are a few reasons
    – Daniel Beck
    Mar 11, 2012 at 13:41
  • I removed my comment because Markdown broken the backticks. thanks for the link @DanielBeck
    – Mengdi Gao
    Mar 11, 2012 at 13:43
  • 2
    @gasan Just surround it with " quotation marks. You can imagine the output of date replacing that part of the command line, and touch Sun Mar 11 14:48:31 CET 2012 would create files name Sun, Mar, etc. touch "Sun Mar 11 14:48:31 CET 2012" on the other hand works fine (except I'm not sure how good the colons will work).
    – Daniel Beck
    Mar 11, 2012 at 13:49
  • 2
    @gasan Single quotes won't work because they don't allow command substitution or any other interpretation. See quoting in the Bash manual.
    – slhck
    Mar 11, 2012 at 13:54
  • 2
    @gasan The difference is that content of single quotes isn't evaluated. Try it with echo "$HOME" and echo '$HOME'. That's why I suggested use of ".
    – Daniel Beck
    Mar 11, 2012 at 13:54
0

You can use more simple command

touch $(date | tr ' ' '-')
4
  • Somewhat less elegant and arguably less portable, but, yeah, that’ll work too. Nov 11, 2018 at 22:07
  • OK, since I know you’re reading this, I’ll go into a little more detail: Daniel’s answer gives a result in a known and totally controlled format.  (1) The output from date typically shows the time as hh:mm:ss.  Your answer results in filenames containing colons.  This could cause an error on Windows-based file systems. (2) The raw output from a simple invocation of date is locale-dependent; i.e., customized to the local language.  Try LANG=de_DE date and LANG=fr_FR date to see examples. … (Cont’d) Nov 13, 2018 at 22:04
  • (Cont’d) …  (2a) Therefore, in the (unlikely?) event that you ever change the language on your system, or you give your script and a collection of data files from your system to somebody whose system is configured for a different language, there will be a mismatch. (I’m not sure that this would be a real problem, but it would be an aesthetic issue.) (2b) I don’t know of any locale that does this, but there could theoretically be a locale where date writes the date as mm/dd/yy (or dd/mm/yy). … (Cont’d) Nov 13, 2018 at 22:04
  • (Cont’d) …  If you ever encountered such a system, you would have filenames containing slashes, and that will cause errors.  (3) Daniel’s answer gives a result where lexicographical (i.e., alphabetical) order, as produced by ls or *, corresponds to chronological order.  With your answer, all the Fri files will be at the beginning of any lexicographically sorted list, and Apr and Aug will appear before all the other months. Nov 13, 2018 at 22:04

You must log in to answer this question.