Riju Mahna
Riju Mahna

Reputation: 6926

How to delete directory with symlinks, without deleting their targets?

I am running an Ant command like:

<delete dir="/some/folder/path/"/>

The folder /path/ contains some files, folders and some symlinks. The files and folders are deleted but the command tries to delete the target directories of the symlinks which gives an error.

I don't want to delete the target directories but only the symlinks when I run the <delete> command on the parent folder (/path/).

Upvotes: 3

Views: 2551

Answers (2)

Ilya Solozobov
Ilya Solozobov

Reputation: 1

use "rm -rf" from bash. I can't find a better solution

<macrodef name="bash_rm">
    <attribute name="dir" />
    <sequential>
        <echo message="Remove directory @{dir}"/>
        <exec executable="rm" >
            <arg value="-rf"/>
            <arg value="@{dir}" />
        </exec>
    </sequential>
</macrodef>

Upvotes: 0

ReyCharles
ReyCharles

Reputation: 1792

I found this bugreport.

A solution is to use fileset.

<delete>
  <fileset dir="/some/folder/path/" followsymlinks="false"/>
</delete>

Edit: the above does not delete symlinks, though. The following deletes all symlinks in a folder. I found it here.

<exec output="/path/to/symlink/list" executable="/usr/bin/find">
    <arg value="/some/folder/path/"/>
    <arg value="-type"/>
    <arg value="l"/>
</exec>

<fileset id="victims" dir="/some/folder/path/">
    <includesfile name="/path/to/symlink/list>
</fileset> 

After this it should be safe to use delete dir.

Upvotes: 4

Related Questions