Apache Ant: Learn Apache Ant from IBM Experts


Section 5. Filesystem operations

Filesystem operations introduction

We’ve learned enough about Ant now to be able to build a basic Java project, but naturally real-world projects are rarely as simple as our example. In the next few sections, we’ll look some of Ant’s numerous additional capabilities and the situations in which they can be used.

In this section, we’ll see how to perform common file operations, such as creating directories and unpacking files. One of the good features of Ant is that the tasks to perform these operations generally behave the same on all platforms.

Creating and deleting directories

One of the most basic filesystem operations is the creation of a directory or folder. The Ant task for this is called mkdir, and it is unsurprisingly very similar to the Windows and UNIX/Linux commands of the same name:


Note first the use of / as the directory separator, which is the convention for UNIX and Linux platforms. You might think that this isn’t very platform neutral, but Ant knows how to handle it, and will do the right thing for whichever platform it is running on, in the same way as we saw earlier when defining location-based properties. We could just as easily have used \ instead, regardless of platform — Ant handles either form, or even a mixture of both.

Another useful feature of the mkdir task is its ability to create parent directories if they don’t exist already. Consider the listing above, and imagine that the archive directory exists, but not the metals directory. If you were using the underlying platform’s mkdir commands, you would need to first explicitly create the metals directory, and then create the zinc directory using a second invocation of the mkdir command. But the Ant task is smarter than this, and will create both directories in one go. Similarly, if the target directory already exists, the mkdir task will not complain, but will just assume that its work is already done, and do nothing.

Deleting directories is just as easy:

This will delete the specified directory, along with any files and sub-directories it contains. Use the file attribute instead of dir to specify a single file to be deleted.

Copying and moving files and directories

Making a copy of a file is simple in Ant. For example:


You can also use move to perform a rename operation instead of copying the file:


Another common filesystem operation is to copy or move a file into another directory. The Ant syntax for this is just as straightforward:



By default, Ant only outputs a summary of the move and copy operations it performs, including such information as the number of files it has moved or copied. If you wish to see more detailed information, including the names of the files involved, you can set the verbose attribute to true.
Creating and unpacking zip and tar files

In the previous section, we saw how to create a JAR file. The process is almost exactly the same for creating other archive files. Here is an Ant task that creates a zip file instead:


The same syntax can also be used to create tar files. Files can also be compressed using the GZip and BZip2 tasks. For example:


Uncompressing and extracting files is just as straightforward:


The overwrite attribute can also be included to control the overwrite behavior. The default is to overwrite any existing files with matching entries from the archive being extracted. The related task names are untar, unjar, gunzip, and bunzip2.

Replacing tokens in files

The final filesystem operation we will look at in this section is the replace task, which performs search and replace operations within files. The token attribute specifies the string to be searched for, and the value attribute specifies the new string that all occurrences of the token string should be replaced with. For example:


The replacement occurs in place, within the file itself. To provide more detailed output, set the summary attribute to true. This causes the task to output the number of occurrences of the token string that were found and replaced.

Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x