4

I want to rename my file to the current timestamp.I know how to get the current timestamp("date +%s") but how can I pipe it to 'mv' command?

  • 4
    So you can start to sort this out in your head, you should know part of the reason you didn't figure this out was you you were asking the wrong question. What you needed was, "Using the output of one command as arguments for another." Depending on the scenario this can be done with the $() and back-tick operators. This is distinctly different from "piping" data from one program to another. In the case that you had output that you wanted to convert to arguments, you could also use xargs to execute another command with arguments taken from stdin or read to just make a variable out of it. – Caleb Jun 19 '11 at 19:32
11

Use something like this (bash):

mv myfile myfile.$(date +%s)

Or alternatively:

mv myfile myfile.`date +%s`

if your shell doesn't do the $(command) thing.

  • Thanks.both work for me.I copy and pasted the second because I can't type "`".How do I type this character? – Nick.h Jun 19 '11 at 17:40
  • Depends on your keyboard... AltGr+7 on a "normal" French AZERTY keyboard. On English layouts it's often on the top-left key (just above tab, on the left of 1). Its ASCII code is 0x60 (96 dec.). – Mat Jun 19 '11 at 17:45
  • One should emphasize that these are backticks around the date command, not ordinary single quotes. – Andre Holzner Jun 19 '11 at 18:17
  • 2
    I think the importance of back-ticks over single quotes can probably be assumed on a unix site, but it should probably be noted that $() syntax is the newer and preferred format for this operation. – Caleb Jun 19 '11 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.