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)"
Есть много случаев, когда я хочу назвать файл или каталог с текущей датой. В основном это временные резервные копии, которые я делаю время от времени, на случай, если я что-то напутаю позже.
А поскольку экспортируемый файл, например база данных, всегда будет иметь одно и то же имя, поскольку я не хочу перезаписывать более старую временную резервную копию, лучшим решением будет присвоить ему дату. И я очень устал набирать текущую дату, а также подчеркивания, но я еще больше устал зависать над моими часами, чтобы проверить, какая дата.
Так что мне интересно, есть ли ярлык для вставки текущей даты, встроенной? Если нет, могу ли я его создать? Какой самый быстрый способ сделать имя / папку проще для текущей даты?
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)"
There were multiple ways to append the file :
From stackoverflow :
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.
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=$ $ EXT=$
Putting them together we get:
$ FILE=somefile.txt $ NAME=$ $ EXT=$ $ DATE=`date +%d-%m-%y` $ NEWFILE=$_$.$ $ echo $NEWFILE somefile_05-10-15.txt
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=$_`date +%d%b%y`.$ $ echo $FILE somefile_05oct15.txt
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" 20131016 $ date +"%Y-%m-%d" 2013-10-16 $ date +"%Y%m%d_%H%M%S" 20131016_193655