Appending a current date from a variable to a filename

  • I'm trying to append the current date to the end of a file name like this:

    TheFile.log.2012-02-11
    

    Here is what I have so far:

    set today = 'date +%Y'
    mkdir -p The_Logs &
    find . -name The_Logs -atime -1 -type d -exec mv \{} "The_Logs_+$today" \; &
    

    However all I get is the name of the file, and it appends nothing. How do I append a current date to a filename?

    when you apppand date, make sure not to use `:` because then it will be read as a host in rsync and scp. http://stackoverflow.com/a/37143274/390066

  • Arcege

    Arcege Correct answer

    8 years ago

    More than likely it is your use of set. That will assign 'today', '=' and the output of the date program to positional parameters (aka command-line arguments). You want to just use C shell (which you are tagging this as "bash", so likely not), you will want to use:

    today=`date +%Y-%m-%d.%H:%M:%S` # or whatever pattern you desire
    

    Notice the lack of spaces around the equal sign.

    You also do not want to use & at the end of your statements; which causes the shell to not wait for the command to finish. Especially when one relies on the next. The find command could fail because it is started before the mkdir.

    Thanks for that mate, its working %100 fine now, Cheers for the heads-up & and mkdir

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM