LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Filenames based on the date (https://www.linuxquestions.org/questions/linux-newbie-8/filenames-based-on-the-date-384362/)

yilez 11-18-2005 10:36 AM

Filenames based on the date
 
How do I create a filename based on the date?

i.e.

I'd like to run 'date' from the prompt, and create a file based upon that.

for example, create a file with the prefix 'test-' which would create a file with a name like:

test-Fri Nov 18 16:38:50 GMT 2005.txt

Thanks.

bulliver 11-18-2005 11:22 AM

Code:

$ touch "test-`date`.txt"

yilez 11-18-2005 11:25 AM

Cheers!

blindcoder 11-18-2005 02:37 PM

You might want something like this for easier sorting:
Code:

touch "test-`date +%Y%m%d`"

eddiebaby1023 11-19-2005 04:31 PM

Re: Filenames based on the date
 
Quote:

Originally posted by yilez
I'd like to run 'date' from the prompt, and create a file based upon that.

for example, create a file with the prefix 'test-' which would create a file with a name like:

test-Fri Nov 18 16:38:50 GMT 2005.txt

Thanks.

You'll live to regret creating filenames with spaces on Linux systems.

anomie 11-19-2005 10:11 PM

How about:
Code:

touch $( echo "test-$( date )" | sed 's/ //g' )
No more spaces.

Or better still:
Code:

touch $( echo "test-$( date )" | sed -e 's/ //g' -e 's/\://g' )
No more spaces, no more colons.

edit: blindcoder's is good too. Nice and short.


All times are GMT -5. The time now is 04:53 AM.