LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Use date with touch to create new file with date based name (https://www.linuxquestions.org/questions/linux-newbie-8/use-date-with-touch-to-create-new-file-with-date-based-name-794960/)

craigjl 03-12-2010 09:11 AM

Use date with touch to create new file with date based name
 
I am using CRON to create a new, blank file, every minute, in a specific location on my web server. After web searching, and reading man pages, I get the impression that the following command is supposed to work:

touch /home/mydomain/var/folder/attachments/`date +%H%M`.txt

This should give me a new file with a file name that is the current hour and minute.

However, when executed, the CRON mailer reports:

touch /home/mydomain/var/folder/attachments/`date +

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file

So, it looks like shell is seeing the plus (+) sign as an EOF

Obviously, nothing get created.

What would be the easiest, single line command to create an empty file, at a given location, with a time based file name?

Jim Craig

arashi256 03-12-2010 09:15 AM

That command works fine for me. Have you tried simply running that command from the commandline? If so, and it works, what does your crontab entry look like?

druuna 03-12-2010 09:19 AM

Hi,

Cron sees the % as being special, you need to escape it (\%).

Hope this helps.

jwl17330536 03-12-2010 09:29 AM

Or simply put the command in a script and execute the script every minute.

craigjl 03-12-2010 09:34 AM

Excellant! Problem Solved
 
Quote:

Originally Posted by druuna (Post 3895829)
Hi,

Cron sees the % as being special, you need to escape it (\%).

Hope this helps.

Changed my original line:
touch /home/mydomain/var/folder/attachments/`date +%H%M`.txt
to:
touch /home/mydomain/var/folder/attachments/`date +\%H\%M`.txt

And everything just works now!

I figured something had to be escaped, but wasn't sure. Will have to read up on CRON a little more.

Thanks for the quick solution.

Jim Craig

druuna 03-12-2010 09:46 AM

You're welcome ;)


All times are GMT -5. The time now is 08:21 AM.