Searching a pattern in file names with number

12,953

Solution 1

With zsh:

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])

Solution 2

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.

Solution 3

The grep bit is.

grep -zE "^20130[1-4](0[1-9]|1[0-5])$"
Share:
12,953

Related videos on Youtube

Aditya Cherla
Author by

Aditya Cherla

Updated on September 18, 2022

Comments

  • Aditya Cherla
    Aditya Cherla over 1 year

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

    20130101
    20130102
    20130103
    .
    .
    .
    20130131
    

    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?

  • ctrl-alt-delor
    ctrl-alt-delor over 10 years
    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.
  • Aditya Cherla
    Aditya Cherla over 10 years
    would'nt the space after question mark give an error?
  • ctrl-alt-delor
    ctrl-alt-delor over 10 years
    Beware though this should work it is fragile: e.g. if the year was 2016 it would not work.
  • ctrl-alt-delor
    ctrl-alt-delor over 10 years
    @Aditya why? it needs the space.
  • Aditya Cherla
    Aditya Cherla over 10 years
    @richard Sorry! my mistake just looked at an example of it
  • ctrl-alt-delor
    ctrl-alt-delor over 10 years
    Maybe call these first in your script shopt -s nullglob; shopt -u failglob, to stop it failing when a file is not found.
  • Zelda
    Zelda over 10 years
    @richard, You right about the shell doing the work, but not about the recursion in directories and displaying unless there are folders with that filename pattern. That is different when you use find . -name "somepattern"
  • bsd
    bsd over 10 years
    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.
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    @bdowning, no, [01..15], same as [015.] matches any of those 4 characters 0, 1, ., or 5.
  • ctrl-alt-delor
    ctrl-alt-delor over 10 years
    Yes that is what I meant only recurses directories that match the pattern, not all directories.