Quantcast
Channel: Appending a current date from a variable to a filename - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 6

Answer by Eric Leschinski for Appending a current date from a variable to a filename

$
0
0

Bash script to inject a date into a filename:

This bash code in file called a.sh

#!/bin/bash
today=`date '+%Y_%m_%d__%H_%M_%S'`;
filename="/home/el/myfile/$today.ponies"
echo $filename;

When run, prints:

eric@dev ~ $ chmod +x a.sh

eric@dev ~ $ ./a.sh
/home/el/myfile/2014_08_11__15_55_25.ponies

Explanation of the code:

Interpret the script with the /bin/bash interpreter. Make a new variable called today. Execute the date command, passing in the Y, m, d, H, M, S flags to configure the output. Place the result into the date variable.

Create a new variable called filename, surround the $today variable with the rest of the static filename text. then echo the filename to screen.

Cram it into a one-liner to increase lulz:

echo "/home/el/myfile/`date '+%Y_%m_%d__%H_%M_%S'`.ponies"

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>