OS X terminal command to create a file with the current date name

I created a cron task and I want to save the output to a file. The file must have a name based on cron runtime (for example: 20110317-113051.txt).

My actual cron command is as follows:

  lynx -dump http: //somesite/script.php > /Volumes/dev0/textfile.txtpreviously>

I want the textfile to textfile replaced with some unique timestamp.

I tried

  lynx -dump http: //somesite/script.php > $ (date) .txt 
but I get the error that the command is ambiguous.

Thank you for your help!

Sorin

+4
source share
2 answers

The date command can be formatted to determine exactly in what form it generates dates. It looks like you want $(date +%Y%m%d-%H%M%S).txt . In this format, the date output should be free of spaces, parentheses, etc., which otherwise could confuse the shell or the lynx command.

See http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/date.1.html for documentation on the date command and http://developer.apple.com/library/mac/ # documentation / Darwin / Reference / ManPages / man3 / strftime.3.html for documenting a format string that is similar to the strftime function in the standard library.

+8
source

You need to specify the file name, since the default date will contain special characters:

 lynx -dump http://somesite/script.php > "$(date).txt" 

As @Gareth says, you should specify a format string for the date so that you get a more readable / manageable file name, e.g.

 lynx -dump http://somesite/script.php > "$(date +%Y%m%d-%H%M%S).txt" 
+6
source

Source: https://habr.com/ru/post/1344070/


All Articles