Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANT How to delete ONLY empty directories recursively

Does anyone know how to recursively delete "empty" directories with ANT (empty includes directories that only contain ".svn" etc).

I know ant allows you to "includeEmptyDirs=true" but I want it to ONLY delete a directory if it is empty (and actually I'd probably need to walk up the recursive chain and delete the directory it was contained in if it is now empty).

Basically as part of our build process we copy over a set of directories that contain a bunch of other nested directories containing various XML and data, and when we move the location for that data our "copy" and checkin build process doesn't really work, and because we are checking into another source control (SVN), wiping out the directories and copying over isn't really an option either (we'd be blowing away the ".svn" folders).

Before we copy over the new builds I can "clear" out the directories by doing the following:

<delete>
  <fileset dir="${webplatformBin}" includes="**/*"/>
</delete>

This leaves every directory (with the ".svn") as an empty directory and then I copy over the new files. After they're copied I'm not sure how I can clear out the empty directories that are left (if we've completely moved where the top-level data directory is etc.).

For example if I had a /projectA/data/localization/text.xml file and I moved it to /projectB/data/localization/text.xml, I would end up with an empty folder /projectA/data/localization/ (that would only contain a .svn folder).

like image 902
Dougnukem Avatar asked Sep 24 '09 16:09

Dougnukem


People also ask

How do you delete an empty directory in recursively?

First, search all the empty files in the given directory and then, delete all those files. This particular part of the command, find . -type f -empty -print, will find all the empty files in the given directory recursively. Then, we add the -delete option to delete all those files.

Can rm remove empty directories?

Removing Directories with rm rm is a command-line utility for deleting files and directories. Unlike rmdir the rm command can delete both empty and non-empty directories. By default, when used without any option rm does not remove directories.

What command deletes empty directories?

Many times empty directories get cluttered in the Linux file system, and it becomes a difficult task to manually search for and delete each of them. The command rmdir (remove directory) is used in Linux to delete empty folders.


3 Answers

Here's the best answer I've been able to come up with:

<delete includeemptydirs="true">
  <fileset dir="${dirToStartFrom}"  >
    <and>
      <size value="0"/>
      <type type="dir"/>
     </and>
  </fileset>
</delete>

I then wrapped it in a macro so I can pass the dir name in from any target:

<!-- Find and delete empty folders under dir -->
<macrodef name="deleteEmptyFolders">
    <attribute name="dir"/>
    <sequential>
        <delete includeemptydirs="true">
            <fileset dir="@{dir}"  >
                <and>
                    <size value="0"/>
                    <type type="dir"/>
                </and>
            </fileset>
        </delete>
    </sequential>
</macrodef>

Like so:

<target name="clean">
  <deleteEmptyFolders dir="build"/>
  <deleteEmptyFolders dir="common"/>
  <deleteEmptyFolders dir="lib"/>
</target>
like image 120
Steve Jackson Avatar answered Oct 05 '22 00:10

Steve Jackson


Here's what I cooked up:

<!-- next three targets are connected
     To remove empty folders from XXX folder.  Process is recursed 3 times.  This 
     is necessary because parent directories are not removed until all their children
     are (if they are empty), and parents are processed before children 
      My example will process structures 3 deep, if you need to go deeper
      then add members to the list  like list="1,2,3,x,x,x,x,x,x"  -->

<target name="rmmtdirs">
        <foreach list="1,2,3" target="rmmtdirs_recurse" param="num"/>
</target>

<target name="rmmtdirs_recurse">
        <foreach target="rmmtdir" param="rmdir">
            <path>
                <dirset dir="${XXX}"/>
            </path>
        </foreach>
</target>

<target name="rmmtdir">
        <echo message=" Removing: ${rmdir} "/>
        <delete includeemptydirs="true">
                <fileset dir="${rmdir}" excludes="**/*"/>
        </delete>
</target>
like image 25
Keith Avatar answered Oct 05 '22 02:10

Keith


I was able to delete all the empty directories starting with the current working directory with the following:

<delete includeemptydirs="true">
    <fileset dir="." includes="**" excludes="**/*.*" />
</delete>
like image 32
kmtarrant13 Avatar answered Oct 05 '22 00:10

kmtarrant13