Announcement

Collapse
No announcement yet.

Use current date/time in filename in an easier way?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] Use current date/time in filename in an easier way?

    TL/DR; I use the current time/date stamp in a file name from the command line. This requires typing out "$(date +"%y%m%d_%H%M%S")" as part of the filename. I would like to shorten this by using a variable or alias or something that requires less typing and memory (mine, not the PC's).

    Longer explanation of my current usage:
    I have a headless Ubuntu 20.04 server that I access via ssh. The server uses only the BTRFS file system. I am in the habit of taking a manual snapshot before updating. BTRFS snapshots keep the time stamp of the source subvolume, which is useful because it definitively shows which subvolume was the source, however knowing when the snapshot was taken is also important. The obvious solution is to use a date/time in the filename thus recording the time of the snapshot while still retaining the source subvolume date. I know I can script the entire command, which I may do anyway, but knowing a good shortcut to doing this would be useful in many ways for me.


    Here's the snapshot command I use:

    Code:
    sudo btrfs su sn /mnt/install/@Ubuntu_Server_2004/ /mnt/install/@Ubuntu_Server_2004_"$(date +"%y%m%d_%H%M%S")"
    I would like to replace "$(date +"%y%m%d_%H%M%S")" with something like "$now"

    Please Read Me

    #2
    Your date construct can be shortened to: "$(date +"%F_%T")"
    Using Kubuntu Linux since March 23, 2007
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      One option (not the only one) would be to use a shell function, in ~/.bashrc, for example (assuming you use bash):
      Code:
      dnow () {
          date +"%y%m%d_%H%M%S"
      }
      After starting a new shell (or sourcing .bashrc), you could use "$(dnow)" in place of "$(date +"%y%m%d_%H%M%S")"

      An alias should also work, in a similar way (and could be preferable for such a simple command):
      Code:
      alias dnow='date +"%y%m%d_%H%M%S"'
      If you run the commands regularly enough for the typing to be a burden, it could be beneficial to script the whole thing.
      Last edited by kubicle; Apr 21, 2022, 11:08 AM.

      Comment


        #4
        Originally posted by kubicle View Post
        One option (not the only one) would be to use a shell function, in ~/.bashrc, for example (assuming you use bash):
        Code:
        dnow () {
        date +"%y%m%d_%H%M%S"
        }
        After starting a new shell (or sourcing .bashrc), you could use "$(dnow)" in place of "$(date +"%y%m%d_%H%M%S")"

        An alias should also work, in a similar way (and could be preferable for such a simple command):
        Code:
        alias dnow='date +"%y%m%d_%H%M%S"'
        If you run the commands regularly enough for the typing to be a burden, it could be beneficial to script the whole thing.
        That's an awesome tip. I've never used a shell variable that way. I inserted that into ~/.profile and it seems to work perfectly.

        I considered the script but I use this in several places for small similar tasks. Having it "live" in the bash shell is really the best option for me. Thanks!

        Please Read Me

        Comment


          #5
          Originally posted by oshunluvr View Post
          I would like to shorten this by using a variable or alias or something that requires less typing and memory (mine, not the PC's).
          I use alias dt='date +"%F-%T"'

          Note that for some purposes, using the bash "builtin" printf "%(%F_%T)T" $EPOCHSECONDS would be quicker. (But zsh doesn't do this...)

          ...knowing when the snapshot was taken is also important...
          The date is in .snapshots/<number>/info.xml. Annoyingly, info.xml gets rw------- permissions, so it's hard to get at.
          Regards, John Little

          Comment


            #6
            I bindkonsole -e bash -c "printf '%(%y%m%d_%H%M%S)T' | xsel -i -b && exit" to Meta+Alt+T. Then to paste the timestamp somewhere (terminal, GUI text editor, etc), I press Meta+Alt+T followed by Ctrl+V assuming of course that the text cursor is where it should be and that the relevant window is in focus.
            Kubuntu 20.04

            Comment


              #7
              Thanks to everyone for the suggestions. I count at least four answers to my question so far and they're all greatly appreciated.

              For the record, the shorter alternate suggestions for "date" don't equal my desired output of 220422_072727. Nit picky I know, but I don't like dashes or especially semicolons in file names. I sort of adopted the underscore as my default and I really don't need the extra separators as my chosen format seems readable enough for my purposes. The subvolume names are long enough!

              Please Read Me

              Comment

              Working...
              X