Grouping files into tars

Hi all,

I have a problem where i have several files in a directory which I SCP from a server to my local machine and i would like to periodically tar/gzip them based on their naming convention.

Here is the scenario:

I SCP files (which all end with the same ending) periodically across to a '/stored' location:

stat_t_123
stat_p_123
stat_c_123

stat_t_999
stat_p_999
stat_c_999

As you can see they all end in '123' or '999' (as an example).

So what I'm looking for is a way to query the '/stored' directory and look for all files ending with '123' or '999' and then grouping them together into a single tar/gzip'd file.

the tarred files will then remain in the '/stored' location.
So you would end up with:

/stored/files123.tar.gz
/stored/files999.tar.gz

I know i can get all files which are not tars or gzips like this:

`ls /stored | grep -v ".tar" | grep -v ".tar.gz"`

but how would i achieve the grouping? :confused::frowning:

any helps guys?

Hi,
one approach could be

tar czvf files123.tar.gz *123

And about Your listing command, the first -v .tar will also exclude tar.gz so You don't need that -v tar.gz

Best regards,
Lakris

for i in `ls -1 stat_?_???|cut -d"_" -f3|sort -u` ; do
     tar czvf /stored/files${i}.tar.gz stat_?_$i
done

Thanks guys, i will give both a go.

Once again many thanks! :slight_smile: