Name your files with dates etc.

Name your files with dates etc.

The command to append a date to a file name is the ‘date’ command.

●How to use the date command

$ date
2020 Septembe 23 Wednesday 00:11:21 JST
———————————————————-
The date is now displayed. Now run it as follows
———————————————————-
$ date +%Y%m%d
20200923
———————————————————-

●Save the file with the date in the name

[root@Lion ~]# touch test.txt ←Create a “test.txt” file with the touch command
[root@Lion ~]# ls -l
-rw-rw-r– 1 koro koro0 09 23 09:16 test.txt
[root@Lion ~]# mv test.txt test.txt_`date +%Y%m%d` ←Run the date command
[root@Lion ~]# ls -l test.txt*
-rw-rw-r– 1 koro koro0 09 23 09:16 test.txt_20200923

If you execute mv or cp command with `date +%Y%m%d` after or before the file name, you can save the file with the date.
Variables enclosed in `date +%Y%m%d` with “`” will be executed as a command. The same is true if the variable is enclosed in $() instead of “`”.

[root@Lion ~]# touch test.txt_$(date +%Y%m%d)
[root@Lion ~]# ls -l test.txt*
-rw-rw-r– 1 koro koro0 09 23 09:16 test.txt_20200923

“%Y%m%d” will name the file with the date, but you can also name it with minutes and seconds

%YYear(4 digit notation)
%yYear(2 digit notation)
%m Month(01~12)
%dDay(01~31)
%HHours(00~23)
%IMinutes(00~12)
%MMinutes(00~59)
%SSeconds(00~59)
%a Day of the week(Sun~Sat)
%bMonth name(Jan~Dec)
タイトルとURLをコピーしました