Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large number of files in a folder each with the date as the filename.
For example


similarly for other months 20130201 to 20130230, the pattern is [Year][Month][Day]. I need to make a tar file of the first fifteen files from JAN to APRIL (i.e 2013[01-04][01-15]), since double digit number range is not allowed in grep. How should I search this pattern?

share|improve this question
up vote 5 down vote accepted

With zsh:

setopt extendedglob
tar zcf ~/file.tar.gz 20130<1-4><1-15>

If you have to use bash:

shopt -s extglob
tar zcf ~/file.tar.gz 20130[1-4]@(0?|1[0-5])
share|improve this answer

The grep bit is.

grep -zE "^20130[1-4](0[1-9]|1[0-5])$"
share|improve this answer
std grep will work as well. Since this particular pattern happens to want months 01-04, the first 0 can be included with the year. So, grep '20130[1-4][01..15]' will match the specified range. – bsd Dec 13 '13 at 11:44
@bdowning, no, [01..15], same as [015.] matches any of those 4 characters 0, 1, ., or 5. – Stéphane Chazelas Dec 13 '13 at 11:47
Thanks, my mistake. – bsd Dec 13 '13 at 11:52

Use find to find the filenames:

find . -maxdepth 1 -name "20130[1-4]0?" -o -name "20130[1-4]1[0-5]" 

check if that is the correct set, and use the output as input for cpio:

find . -maxdepth 1 -name "20130[1-4]0?" -o -name "20130[1-4]1[0-5]" | cpio --create --format=ustar -O file.tar

@richard pointed out this could traverse in sub-directories (if a sub-directory matches the pattern) (and that shell expanded the pattern, corrected).

Expanding on the commandline runs the risk of the commandline becoming too long, which might happen if you had a file every few seconds for every day (20130101-00005, 20130101-00007. Piping the filename into cpio does not have problem.

share|improve this answer
Note it is not find that is doing the work here, but the shell. Warning will recurse directories, use echo instead of find to not do this. – richard Dec 13 '13 at 11:15
would'nt the space after question mark give an error? – Aditya Cherla Dec 13 '13 at 11:16
@Aditya why? it needs the space. – richard Dec 13 '13 at 11:19
@richard Sorry! my mistake just looked at an example of it – Aditya Cherla Dec 13 '13 at 11:22
Maybe call these first in your script shopt -s nullglob; shopt -u failglob, to stop it failing when a file is not found. – richard Dec 13 '13 at 11:28

You can tar czvf file.tar.gz $(ls | grep "201301\|201302\|201303\|201304" | grep -v "16\|17\|18\|19\|20\|21\|22\|23\|24\|25\|26\|27\|28\|29\|30\|31") from the base directory.

share|improve this answer
Beware though this should work it is fragile: e.g. if the year was 2016 it would not work. – richard Dec 13 '13 at 11:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.