public static int createDeepHardlink ( final File sourceDir , final File destDir ) throws IOException { if ( ! sourceDir . exists ( ) ) { throw new IOException ( "Source directory " + sourceDir . getPath ( ) + " doesn't exist" ) ; } else if ( ! destDir . exists ( ) ) { throw new IOException ( "Destination directory " + destDir . getPath ( ) + " doesn't exist" ) ; } else if ( sourceDir . isFile ( ) && destDir . isFile ( ) ) { throw new IOException ( "Source or Destination is not a directory." ) ; } final Set < String > paths = new HashSet <> ( ) ; createDirsFindFiles ( sourceDir , sourceDir , destDir , paths ) ; int linkCount = 0 ; for ( String path : paths ) { final File sourceLink = new File ( sourceDir , path ) ; path = destDir + path ; final File [ ] targetFiles = sourceLink . listFiles ( ) ; for ( final File targetFile : targetFiles ) { if ( targetFile . isFile ( ) ) { final File linkFile = new File ( path , targetFile . getName ( ) ) ; // NOTE!! If modifying this, you must run this ignored test manually to validate: // FileIOUtilsTest#testHardlinkCopyOfBigDir Files . createLink ( linkFile . toPath ( ) , Paths . get ( targetFile . getAbsolutePath ( ) ) ) ; linkCount ++ ; } } } return linkCount ; }
Hard link files and recurse into directories .