I have a set of files that have dates in them.
lets call them:
a20120528_120001.log
b20120528_120003.log
(name)(year)(month)(day)_(hour)(minute)(second).log
It is easy enough to move these two files simultaneously by doing:
mv *20120528_12* file/
But now I have a situation where I want to move several hours worth of files in the same day ie:
a20120528_120001.log
b20120528_120003.log
a20120528_130001.log
b20120528_130003.log
a20120528_140001.log
b20120528_140003.log
Now if i wanted to transfer all of them i could just do the day:
mv *20120528* file/
but what can I do if I only want to move hours 12 and 13, but exclude 14.
Please note this will need to be generic enough that i can input the date, because this will extend to be used across multiple days where there are 24 logs per day and several (between 3-8) will be excluded from each day.
How would I do this?
You can use ranges:
mv *20120528_1[23]* file/
For excluding everything from 3-8, go with the slightly more complicated:
mv *20120528_{0[0-29],[12]*}*
[0-29] breaks down to the range 0-2 and 9.{A,B} expands to A or B.This is a good question because in Bash, filename expansion uses only *, ?, and [. So you can deal with hours 12 and 13 only with the following:
[ab]20??????_1[23]????.log
Note that this takes you up to the year 2099 only; adjust accordingly if that bothers you.
But if you need a general range of values, you will need multiple commands. If you have hours 00 through 23 and you want to exclude 03 through 08, I think you will need the following:
[ab]20??????_0[0129]????.log
[ab]20??????_1?????.log
[ab]20??????_2[0123]????.log
Of course you can also write a script to generate the proper patterns, using something more complex than filename globbing. Not sure if you need that much complexity, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With