Saturday, May 18, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 473  / 1 Year ago, wed, march 29, 2023, 12:43:19

Terminal outputI am using Lubuntu 20.04 LTS. I would like to add a timestamp to some wav files so they can be uploaded to a web site. I would like the first file to be Garden2021-05-02_04-10-00.wav and the following files to increase by 30 seconds for each file.
How can I best do this please?


More From » timestamp

 Answers
1

Although it's generally used with substitutions of the form s/pattern/replacement/, the perl-based rename command can actually accept any valid perl expressions. So for example (borrowing the initialization from pLumo's answer) you could do:


rename -n -- '
BEGIN{use Time::Piece; our $ts = Time::Piece->new(shift)}
$_ = our $ts->strftime("Garden%F_%H-%M-%S.wav");
$ts += 30
' "$(date -d 2021-05-02T04:10:00 +%s)" *.wav

Remove the -n once you are happy with the suggested renamings.


Note that the files will be stamped in the order that * expands in your locale. If you want another order, then IMHO the simplest way is to switch to the Z-shell (zsh) where you can use glob qualifiers to specify the sort order. For example *.wav(n) or *.wav(.non) to sort plain files numerically by name increasing:


% ls *.wav
1.wav 10.wav 11.wav 12.wav 2.wav 3.wav 4.wav 5.wav 6.wav 7.wav 8.wav 9.wav

% rename -n -- '
BEGIN{use Time::Piece; our $ts = Time::Piece->new(shift)}
$_ = our $ts->strftime("Garden%F_%H-%M-%S.wav");
$ts += 30
' "$(date -d 2021-05-02T04:10:00 +%s)" *.wav(.non)
rename(1.wav, Garden2021-05-02_04-10-00.wav)
rename(2.wav, Garden2021-05-02_04-10-30.wav)
rename(3.wav, Garden2021-05-02_04-11-00.wav)
rename(4.wav, Garden2021-05-02_04-11-30.wav)
rename(5.wav, Garden2021-05-02_04-12-00.wav)
rename(6.wav, Garden2021-05-02_04-12-30.wav)
rename(7.wav, Garden2021-05-02_04-13-00.wav)
rename(8.wav, Garden2021-05-02_04-13-30.wav)
rename(9.wav, Garden2021-05-02_04-14-00.wav)
rename(10.wav, Garden2021-05-02_04-14-30.wav)
rename(11.wav, Garden2021-05-02_04-15-00.wav)
rename(12.wav, Garden2021-05-02_04-15-30.wav)

If you can't use zsh, then you can do the same in other shells using an external sort together with xargs:


printf '%s0' *.wav | sort -zn | xargs -0 rename -n -- '
BEGIN{use Time::Piece; our $ts = Time::Piece->new(shift)}
$_ = our $ts->strftime("Garden%F_%H-%M-%S.wav");
$ts += 30
' "$(date -d 2021-05-02T04:10:00 +%s)"

[#1551] Thursday, March 30, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antebrow

Total Points: 291
Total Questions: 135
Total Answers: 117

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;