On 2013-04-11, Thomas 'PointedEars' Lahn wrote:
> mc wrote:
>
>> I have several files in a folder. Each filename contains a date
>> occurrence in the format yyyyMMdd (eg. 20130401)
>>
>> Using bash, how can I rename the files removing everything but the date?
>>
>> For example:
>> "02file_20130401foo.txt" ---> "20130401.txt"
>>
>> I don't need to verify the date, I know it is always valid and there is
>> just one occurrence of the pattern yyyyMMdd.
>
> As you can see, this is non-trivial in a sh or ksh-compatible shell.
> Why not use the right tool for the right job?
>
> find . -name . -or -prune rename 's/^.*(\d{8}).*(\..+)?$/\1\2/' {} +
>
> If you are sure about the date, you could specify further, to avoid false
> positives:
>
> find . -name . -or -prune rename 's/^.*(20\d{6}).*(\..+)?$/\1\2/' {} +
rename is not an operator in either POSIX or GNU find (unless it's a
very recent addition), nor is it a standard command (and there are
at least two versions in the wild with differing syntax).
--
Chris F.A. Johnson, author <
http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)