Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

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.

share|improve this answer
    
@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 '12 at 13:41
    
I removed my comment because Markdown broken the backticks. thanks for the link @DanielBeck –  Mengdi Gao Mar 11 '12 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 '12 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 '12 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 '12 at 13:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.