Results 1 to 4 of 4

Thread: create text file with the date

  1. #1
    Join Date
    Nov 2008
    Beans
    2

    create text file with the date

    Huge beginner here. I'd like to create a text file containing a formatted date using bash, and can't quite seem to get it.

    let's say I'm trying to create a file containing the single line:

    mm/dd/yyyy-mm/dd/yyyy

    I can get the date from bash like this:
    Code:
    `date +%m/%d/%Y`
    and I think I'm supposed to be able to get tomorrow's date using the following code, but can't quite seem to get it:
    Code:
    `date +%m/%d/%Y` --date="-1 days ago"
    I'm also not quite sure how to create / write over a one-line text file. I can create the file using cat (but I have no preference over cat / sed / vi, etc.):
    Code:
    cat > dates
    but no text shows up in my file at all, and I can't seem to be able to close the file. I guess Crtl+Shft+D is supposed to work.

    right now, if my logic worked (and it doesn't), my code would look like this:

    Code:
    $now=$(`date +%m/%d/%Y`)
    $later=$(`date +%m/%d/%Y` --date="-1 days ago")
    
    cat > dates
    $now-$later
    ^D
    I'm sure this is a joke for most of you guys. Any help would be greatly appreciated. Thanks!

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: create text file with the date

    try

    Code:
    echo $(date '+%m/%d/%Y')-$(date '+%m/%d/%Y' --date="tomorrow") > dates

  3. #3
    Join Date
    Nov 2008
    Beans
    2

    Re: create text file with the date

    Thanks! Worked perfectly! Well...this thread's over

  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: create text file with the date

    Just a suggestion, use %F if you can for non-display output. Big-endian dates are free of ambiguity. Otherwise, if this output is meant for human consumption, use +%x instead for a locale-appropriate date format.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •