6

I have a single file which I want to rename mv to year_month_day_h:m:s - whats the best way to do that?

I've tried the following but it doesn't dynamically add the correct stamp (original file actually has a backslash in the name):

mv getnw/myfilename.txt "%Y%m%d%H%M%S".txt mv getnw/myfilename.txt "%Y-%m%d%H%M%S".txt mv getnw/myfilename.txt %Y-%m%d%H%M%S.txt mv getnw/myfilename.txt "'date +%Y%m%d%H%M%S'.txt"

Tony
  • 8,681
  • 7
  • 36
  • 55
  • 1
    Use `mv`. What have you tried? – Mat May 17 '12 at 15:53
  • Please edit that into your question, and please also add a bit more details about exactly what the source file name looks like (i.e. a real example filename) and what you're trying to automate. – Mat May 17 '12 at 16:02
  • 2
    Note that while most filesystems don't have a problem with `:` in file names, there will be a problem getting this file to a Windows-compatible filesystem, as those treat `:` as a special character. – Piskvor left the building May 17 '12 at 16:13
  • 1
    Piskvor: that's correct. it works on linux/mac but it's generally bad idea to use special chars in file names. – dsomnus May 17 '12 at 16:14
  • @dsomnus: There's nothing "special" about punctuation. I for one like the fact that I can use a complete sentence for a file name, without having to replace all punctuation with `_`. Oh, wait, `.` is allowed in NTFS filenames. But is `,`? Maybe. And `!` is okay, but `?` means trouble. Or was it the other way around? Fun fun fun with arbitrary rules from the 1980s :-( – Piskvor left the building May 18 '12 at 06:55

2 Answers2

16
mv myfile.txt `date +%Y_%m_%d_%H:%M:%S`.txt
dsomnus
  • 1,391
  • 14
  • 21
2
mv myfile.txt myfile`date -Is`.txt

is a shorter version

but : won't work with some of the unix commands like rsync or scp because it parses part of the name as a host address. use tr to change :

mv myfile.txt myfile`date -Is|tr : -`.txt

or

mv myfile.txt myfile$(date -Is|tr : -).txt
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
  • 1
    Right, the colon is also not usable in all filesystems or computer systems. See [Allowed characters in filename](https://stackoverflow.com/questions/4814040/) and [What characters should be restricted from a Unix file name?](https://stackoverflow.com/questions/457994/). – U880D May 01 '20 at 04:07