A simple and powerful alternative method of appending a date and time to a file name is to assign the syntax of the date command to a variable.
Later, then we
can insert the variable name in the brackets of the $() syntax
construction,
and the shell will interpret it in the same way.
For eg:
$ day=”date +%T-%d_%m_%Y”
To check the value of the
variable,
$ echo $day
To create the “example_team_file” with the time and date
appended to the file name now
the variable can be used.
Output of the same is pasted below for better understanding:
Output:
[root ~/good/test 10:50:12] # day="date
+%T-%d_%m_%y"
[root ~/good/test 10:51:21] # echo $day
date +%T-%d_%m_%y
[root ~/good/test 10:52:35] # touch example_file$($day)
[root ~/good/test 10:52:49] # ls -l
total 4
-rw-r--r-- 1 root root 0 Aug 20 10:52
example_file10:52:49-20_08_13
[root ~/good/test 10:52:51] # touch test_file1$($day)
test_file2$($day) test_file3$($day) test_file4$($day) test_file5$($day)
[root ~/good/test 10:54:00] # ls -l
total 24
-rw-r--r-- 1 root root 0 Aug 20 10:52
example_file10:52:49-20_08_13
-rw-r--r-- 1 root root 0 Aug 20 10:54
test_file110:54:00-20_08_13
-rw-r--r-- 1 root root 0 Aug 20 10:54
test_file210:54:00-20_08_13
-rw-r--r-- 1 root root 0 Aug 20 10:54
test_file310:54:00-20_08_13
-rw-r--r-- 1 root root 0 Aug 20 10:54
test_file410:54:00-20_08_13
-rw-r--r-- 1 root root 0 Aug 20 10:54
test_file510:54:00-20_08_13
Note: Kindly note, the variable is assigned will be
temporary. Once logged
out, the variable does not exist.
No comments:
Post a Comment