Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:41:14+00:00 2026-06-16T17:41:14+00:00

I have a php script that attempts to remove all files from a directory

  • 0

I have a php script that attempts to remove all files from a directory structure, but preserve everything in svn. I found this command online which does the job perfectly if you plug it directly in a shell

find /my/folder/path/ -path \'*/.svn\' -prune -o -type f -exec rm {} +

Unfortunately if I perform a shell_exec in php on that command like so:

$cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
shell_exec($cmd);

Then all the files in my current directory that I call the php script from are deleted as well.

Can someone explain why, and how to fix the issue so that I can fix the php script so it acts like expected, removing only those files in the specified folder

The full source code is below, just in case there is perhaps a silly mistake in there that I have missed:

<?php

# This script simply removes all files from a specified folder, that aren't directories or .svn 
# files. It will see if a folder path was given as a cli parameter, and if not, ask the user if they 
# want to remove the files in their current directory.

$execute = false;

if (isset($argv[1]))
{
    $folderPath = $argv[1];
    $execute = true;
}
else
{
    $folderPath = getcwd();
    $answer = readline("Remove all files but not folders or svn files in $folderPath (y/n)?" . PHP_EOL);

    if ($answer == 'Y' || $answer == 'y')
    {
        $execute = true;
    }
}

if ($execute)
{
    # Strip out the last / if it was given by accident as this can cause deletion of wrong files
    if (substr($folderPath, -1) != '/')
    {
        $folderPath .= "/";
    }

    print "Removing files from $folderPath" . PHP_EOL;
    $cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
    shell_exec($cmd);
}
else
{
    print "Ok not bothering." . PHP_EOL;
}

print "Done" . PHP_EOL;

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:41:15+00:00Added an answer on June 16, 2026 at 5:41 pm

    Your command looks okay. At least in shell. If you would actually troubleshoot your issue in PHP with a simple

    var_dump($cmd);
    

    You would see where your error lies:

    $cmd = 'find $folderPath -path \'*/.svn\' -prune -o -type f -exec rm {} +';
    

    Look closely. Hint: A single can’t make a double for a dollar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that inserts data from an Android app into a
I have a php script that grabs links from another website. I am storing
I have a PHP script that very much like a commenting system, but requires
I have a PHP script that attempts to read and parse JSON strings returned
I have a PHP script that moves files out of a specific folder on
I have a php script that is triggered from my app. This script uses
I have php script that creates a temporary watermark image for users that are
I have a php script that watches for the newest json file in a
I have a PHP script that opens an XML document, gets the DOM and
I have a PHP script that creates large complex tables. I am attempting to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.