开发者

tar only *.log files and delete orginals when finished

开发者 https://www.devze.com 2023-03-11 21:58 出处:网络
I am using the following script to create a tarball of a directory with php exec(\"tar -zcvf archive_name.tar.gz *\");

I am using the following script to create a tarball of a directory with php

exec("tar -zcvf archive_name.tar.gz *");

and this is working as expected, but I would like to extend this code so that it will only add files to the tarball that have a certain extension (*.log in this case) then delete the originals after the tarball has been created. Any advise or examples would be great.

TIA, Kevin开发者_开发百科


You can change you command to:

exec("tar -zcvf archive_name.tar.gz *.log && rm -f *.log")

Changes made:

  • Instead of passing all file (*) as argument to tar we now pass *.log

  • Added && rm -f *.log. The command rm -f *.log forcefully deletes all .log files from the present working directory. We've used an && as the glue between the two commands because we want the files to be deleted only after the tarball is created.


This isn't really relevant to php, seeing as you're just escaping out to a shell command anyway. Try

exec("tar -zcvf archive_name.tar.gz *.log && rm *.log");


This should be on serverfault. Tar is a linux program. If you want to do the same in pure PHP you can use glob and Zend_Filter tar adapter:

$files = glob("*.log");
foreach($files as $file) {
    $filter     = new Zend_Filter_Compress(array(
         'adapter' => 'tar',
         'options' => array(
            'archive' => 'filename.bz2'
         ),
    ));
    $compressed = $filter->filter($file);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消