Appending the file modified date to the file name

Ian Malone ibmalone at gmail.com
Tue Dec 1 13:14:33 UTC 2015


On 1 December 2015 at 02:30, Gordon Messmer <gordon.messmer at gmail.com> wrote:
> On 11/30/2015 04:49 PM, Robert Moskowitz wrote:
>>>
>>> You could:
>>>  mv "$file" "$file-$(stat -c %y "$file")"
>>
>>
>> Not quite as the script:
>>
>> #!/bin/bash
>> for i in `seq 20 42`;
>>      do
>>          file="Voice "0${i}.m4a
>>          mv $file "$file-$(stat -c %y "$file")"
>>      done
>>
>> is producing:
>>
>> mv: target ‘Voice 036.m4a-2015-05-07 06:51:59.000000000 -0400’ is not a
>> directory
>
>
> That's why I quoted "$file" in both instances.
>
> Your file name has a space in it, so the command you ran is:
>
> "mv" "Voice" "036.m4a" "Voice 036.m4a-2015-05-07 06:51:59.000000000 -0400"
>
> And since "mv" got 3 arguments, it requires that the last one is a
> directory.
>
> If you had quoted "$file" after mv, it should have worked properly.
>

Though in general try to avoid adding whitespace when automatically
naming files, it's just a pain. (This doesn't mean you don't need to
be careful about your source files having whitespace, but no need to
inflict more on yourself.)

E.g. ISO time without white space:
date -d "$(stat -c %y "$file")" +"%Y-%m-%dT%H:%M:%S%z"

Substituting spaces in the target name:
echo "$outfile" | tr " " "-"

-- 
imalone
http://ibmalone.blogspot.co.uk


More information about the users mailing list