Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant -- copying files and subdirectories from only one subdirectory on a tree

Tags:

ant

I'd like to copy files and subdirectories using Ant from a single subdirectory without copying the rest of the directory structure and contents. For example, I'd like to copy dir_3, its files (file_1 to file_n) and subdirectories (dir_4 and dir_5), but not dir_1 nor dir_2. Is there a pattern that I can use to do this?

temp
   \--dir_1
   \--dir_2
       |
       \--dir_3
           |
           \--dir_4
           \--dir_5
           \-- file_1
           |
           \--file_n

Thanks.

like image 648
Drew Avatar asked Jun 21 '11 18:06

Drew


People also ask

What is the difference between a directory and a subdirectory?

Files are organized by storing related files in the same directory. In a hierarchical file system (that is, one in which files and directories are organized in a manner that resembles a tree), a directory contained inside another directory is called a subdirectory.

How will you copy a directory structure dir1 to dir2 with all the subdirectories?

By using cp -r /dir1/* /dir2 , it copies all files and its structure to /dir2 . By using cp -r /dir1/*.

How do I copy a file in subdirectories?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option.


2 Answers

<copy todir="${copy.dir}">
     <fileset dir="temp">
         <include name="**/dir3/**"/>
     </fileset>
</copy>

When you use the include directive, it will only include the files that match the pattern you give it. In this case, I'm copying only those files that have /dir3/ somewhere in their full path name. This includes sub-directories under dir3 and all files under dir3.

You can use the exclude directive to override the include directives:

<copy todir="${copy.dir}">
     <fileset dir="temp">
         <include name="**/dir3/**"/>
         <exclude name="**/dir3/*"/>
     </fileset>
</copy>

This will copy all sub-directories and files in those sub directories, but not the files under dir3 itself. The * matches all files in the directory while ** matches the all the files in the entire directory tree.

Notice this will create a directory temp/dir2/dir3. If I want temp/dir3, I have to set my fileset to the parent directory of dir3:

<copy todir="${copy.dir}">
     <fileset dir="temp/dir2">
         <include name="dir3/**"/>
     </fileset>
</copy>

Doing this:

<copy todir="${copy.dir}">
     <fileset dir="temp/dir2/dir3"/>
</copy>

Will create a directory temp with all the files directly under dir3 directly under temp. There will also be a temp/dir4 and temp/dir5 directory containing all the files (and directory trees) under those directories.

like image 105
David W. Avatar answered Nov 15 '22 13:11

David W.


<copy todir="/some/path/foobar" verbose="true">
  <fileset dir="/some/path/temp/dir2" includes="**"/>
</copy>

just use a fileset starting from dir2 including all dirs and files below..
verbose = true to echo all the files copied
May be you need to use overwrite = true also if the dir that is specified by todir
attribute already exists, otherwise existing files won't be overwritten by copy task

like image 44
Rebse Avatar answered Nov 15 '22 13:11

Rebse