There are a lot of occurrences in which I want to name a file or directory something with the current date in it. Mostly it's temporary backups that I take every once in a while, just in case I mess up something later.

And since an exported file, for example a database, will always have the same name, being that I don't want to overwrite an older temp backup, the best solution is to give it a date. And I am very tired of typing in the current date, as well as the underscores, but I am even more tired of hovering over my clock to check what the date is.

So I am wondering if there is any shortcut to paste the current date built in? If not, can I create one? What is the quickest way to make it easier to name a file/folder the current date?


In a shell, use command substitution:

touch "$(date)"

or, with format

touch "$(date +%Y-%m-%d_%H.%M.%S)"

You can keep the longer command in an alias or function

alias ts='date +%Y-%m-%d_%H.%M.%S'
touch "$(ts)"
| improve this answer | |

Following choroba's advice, you could define a custom action on Thunar's context menu

| improve this answer | |

There were multiple ways to append the file :

From stackoverflow :

  1. Get the date as a string

This is pretty easy. Just use the date command with the + option. We can use backticks to capture the value in a variable.

$ DATE=`date +%d-%m-%y`

You can change the date format by using different % options as detailed on the date man page.

  1. Split a file into name and extension.

This is a bit trickier. If we think they'll be only one . in the filename we can use cut with . as the delimiter.

$ NAME=`echo $FILE | cut -d. -f1
$ EXT=`echo $FILE | cut -d. -f2`

However, this won't work with multiple . in the file name. If we're using bash - which you probably are - we can use some bash magic that allows us to match patterns when we do variable expansion:

$ NAME=${FILE%.*}
$ EXT=${FILE#*.}

Putting them together we get:

$ FILE=somefile.txt             
$ NAME=${FILE%.*}
$ EXT=${FILE#*.} 
$ DATE=`date +%d-%m-%y`         
$ echo $NEWFILE                 

And if we're less worried about readability we do all the work on one line (with a different date format):

$ FILE=somefile.txt  
$ FILE=${FILE%.*}_`date +%d%b%y`.${FILE#*.}
$ echo $FILE                                 

Other ways would be :

$ date Wed Oct 16 19:20:51 EDT 2013

If you truly want filenames like that you'll need to wrap that string in quotes.

$ touch "foo.backup.$(date)"

$ ll foo* -rw-rw-r-- 1 saml saml 0 Oct 16 19:22 foo.backup.Wed Oct 16 19:22:29 EDT 2013

You're probably thinking of a different string to be appended would be my guess though. I usually use something like this:

$ touch "foo.backup.$(date +%F_%R)"
$ ll foo*
-rw-rw-r-- 1 saml saml 0 Oct 16 19:25 foo.backup.2013-10-16_19:25

See the man page for date for more formatting codes around the output for the date & time. Additional formats

If you want to take full control if you consult the man page you can do things like this:

$ date +"%Y%m%d"

$ date +"%Y-%m-%d"

$ date +"%Y%m%d_%H%M%S"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy