Jump to content
section31

Exec With Tar And Find

Recommended Posts

i've been trying to troubleshoot this for a long time now and I can't figure it out :cry: I was hoping to see if anyone had problems like this?

 

Purpose: To make a gzipped tarball of a subdirectory. Please look at the 2nd bit of code, thats where the problem is. Whenever I change the path of the find directory, it doesn't work properly even after I checked the find's results and they are correct. The problem happens when making the tar.gz, like stated below it creates an empty archive instead.

><?php


/* This Works Fine */
/* finds any jpg or jpegs in the current working directory and makes a gzip tarball of it. */
$cmd = "tar -cvf backup.tar.gz `find . -iregex '.*\.\(jpg\|jpeg\)' -maxdepth 1`";
exec($cmd, $output, $returnCodes);

/* This Does Not Work, it puts an emtpy gzip tarball thats app. 10k in the directory */
/* The only thing thats diff here is i change the find to look in a subdirectory */
$cmd = "tar -cvf backup.tar.gz `find './foobarDir/' -iregex '.*\.\(jpg\|jpeg\)' -maxdepth 1`";
exec($cmd, $output, $returnCodes);


?>

Edited by section31

Share this post


Link to post
Share on other sites

Just a guess but try it this way.

 

>$cmd = "tar -cvf backup.tar.gz `find .\/foobarDir\/ -iregex '.*\.\(jpg\|jpeg\)' -maxdepth 1`";

Share this post


Link to post
Share on other sites

Tried that bruce, and I got the same results an empty archive.

 

I double checked what find returns and the results were correct either way, you're way or my way.

 

This is odd behavior isn't it?

Edited by section31

Share this post


Link to post
Share on other sites

Sorry I took so long for the reply.

 

Yeah, I tried using the find first and I get the identical results as the other method. It works when I use find . but doesn't work when I use find ./someDir/.

 

You mean something like this right?

>find . -iregex '.*\.\(jpg\|jpeg\)' -maxdepth 1 | xargs tar -cvf backup.tar.gz

Edited by section31

Share this post


Link to post
Share on other sites

Remove the trailing slash from the folder name and try it. Are you getting any output from the command at all? An error?

Share this post


Link to post
Share on other sites

I took off the trailing slash and no change.

 

This is whats returned, so there isn't much to work with. Is there a way I can force out more error codes or something. I'm going to start weeping if we don't figure this out. cry.gif

>exec($cmd, $output, $returnCode);

echo '<pre>' , print_r($output, true) , "\nReturn Code - " , $returnCode , '</pre>';

 

The above returns this

 

Array

(

)

 

Return Code - 2

 

NOTE: in case you need a link to exec() http://us3.php.net/function.exec

Edited by section31

Share this post


Link to post
Share on other sites

I'm running out of options.

 

>find ./foobarDir -iregex '.*\.\(jpg\|jpeg\)' -maxdepth 1 | tar -cvf backup.tar.gz -

Share this post


Link to post
Share on other sites

If you take the -maxdepth off your first one that is working wouldn't it back up recursively?

Share this post


Link to post
Share on other sites

Indeed it would do it recursively. I have it set to maxdepth 1 because I don't want it to archive recursively.

Share this post


Link to post
Share on other sites
Purpose:  To make a gzipped tarball of a subdirectory.

It looks to me like your issue could be due to the fact you're not using the '-z' option with the tar command. Without it, tar does not make a gzip file for you (you get just a plain tar file).

 

I was able to use the following command successfully in a PHP script on my account:

>$cmd = "tar -cvzf backup.tar.gz `find './images' -iregex '.*\.\(jpg\|jpeg\)$' -maxdepth 1` 2>&1";

If you want to see text output from the command to see what it's doing or any text error messages, add the following code to your script:

>echo '<pre>', implode("\n", $output), '</pre>';

The only issue I had with the above code is that it will not archive file names that have spaces in them. I imagine there is a way to deal with this - I didn't try to find out how as I don't know if it would even be an issue for you.

 

Hope this helps...

Share this post


Link to post
Share on other sites

Hey david, It appears the 2>&1 fixed the problem, but now I figured out a way without it. So, here is the final solution to my goal for the record.

 

># Works with spaces in filenames/directorys too
find './foobarDir/' -iregex '.*\.\(jpg\|jpeg\)' -maxdepth 1 -print0 | xargs -0 tar -cvzf backup.tar.gz

 

Thanks you two for being so patient with me.

Edited by section31

Share this post


Link to post
Share on other sites

I would have thought adding the '-z' to your tar command would have more effect on your problem than the '2>&1'. All the '2>&1' does is redirect any error messages (STDERR) to where your PHP script can capture them in the $output variable (STDOUT).

 

I guess you have file names with spaces in them, huh? :(

 

Glad to hear you finally got it working! :thumbup1:

Share this post


Link to post
Share on other sites
I would have thought adding the '-z' to your tar command would have more effect on your problem than the '2>&1'.  All the '2>&1' does is redirect any error messages (STDERR) to where your PHP script can capture them in the $output variable (STDOUT).

 

I guess you have file names with spaces in them, huh?  :(

 

Glad to hear you finally got it working!  :thumbup1:

 

Yeah, i don't really understand why the '2>&1' made the difference and not the z flag.

 

My files did have spaces in them, but that -print0 and xargs -0 fixed that problem.

Share this post


Link to post
Share on other sites
My files did have spaces in them, but that -print0 and xargs -0 fixed that problem.

If all of the .jpg|.jpeg file names in the target subdirectory had spaces in them, that would definitely explain the empty archive files you were getting before.

 

The '-z' option should have given you a true gzip file (a compressed tar archive) instead of just a plain tar file (an archive that is more or less one file concatenated after the other). Since you're putting .jpg files in the archive (which are already compressed), I don't know that you'd get hardly any space advantage from creating a .tar.gz file as opposed to a .tar file.

 

I'd still use the '-z' option just to be consistent though, as I've memorized the options that I use all the time with tar: '-cvzf' to make a tar, and '-xvzf' to extract one.

Share this post


Link to post
Share on other sites

Glad you got it working! :)

 

Been a long time since I messed with tar and find commands. I've been on Windows too long! :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...