2

I have a Java program that runs on a Linux machine and it needs to delete million of files in a directory on Windows machine. Not all the files needs to be deleted--only the files that have "xxx" in name need to be deleted.

I used Samba API to connect to Windows from UNIX and fired delete command. It ran fine but took 12 hours to delete millions of files.

Can I delete the files faster? Any UNIX script from Java?

mins
  • 6,478
  • 12
  • 56
  • 75
Wafa Saba
  • 101
  • 1
  • 9
  • 1
    adding some code will be really helpful!!!! – Dev Apr 21 '15 at 18:09
  • 3
    Why not just create a batch script and run that ? – Jonas Czech Apr 21 '15 at 18:10
  • If you have a program on a Linux machine which needs to delete files over a Samba-connection, chances are that the bottleneck is your network connection. Doing it in a unix script file is not likely to improve things. – JesperE Apr 21 '15 at 18:12
  • 2
    Can you explain the circumstances a little more? Why aren't you deleting the files directly from the Windows machine? Why are you using Java rather than simple windows BAT files or Linux scripts? If it's just removing files with xxx, why not use a filename pattern? In short, why did you reject the simple solutions? – RealSkeptic Apr 21 '15 at 18:13
  • 1
    Did you fire millions of delete commands or one command for deleting `*xxx*.*` ? Can you delete files on Windows from the Linux commandline? Are the combination Powershell/taskmanager or Java on Windows an option? – Walter A Apr 21 '15 at 18:13
  • capitalization; clarify text and title – Alois Mahdal Apr 21 '15 at 18:22
  • Below is my code: SmbFile smbFile = null; for (FileVO fileVo : fileList) { StringBuffer fileName = getFileName(fileVo); NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", username, password); String path = directoryName.append(fileName).toString(); smbFile = new SmbFile(path, auth); if (smbFile.isFile()) { smbFile.delete(); LOGGER.info("File Deleted :" + path); } } – Wafa Saba Apr 22 '15 at 13:47

2 Answers2

2

You don't need Java program to delete files in directory. I would just run below command in Windows folder (or batch to upload)

del /q /s *xxx*

/q quite mode
/s include subdirectory
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • Java program is a spring batch program called from ESP on schedule. Before deleting files, there are number of rules to identify fileName having "xxx". I can try creating .Bat file having above commands, move to windows directory using samba and then execute, Please suggest. – Wafa Saba Apr 22 '15 at 13:48
  • To start the bacth file from Java visit this [SO article](http://stackoverflow.com/a/616014/4454454) – MaxZoom Apr 22 '15 at 14:10
  • Suggested solution: 1. Create a batch file on Linux that contains a list of file names to delete and a command to remove them. 2. Upload this file to Windows directory using SAMBA 3. Execute the batch file (and log what was deleted to log file) 4. Remove the batch file using SAMBA for cleanup. – MaxZoom Apr 22 '15 at 15:14
2

You can use a shell command similar to the following to do this:

find /path/to/windows/dir -type f -name '*xxx*' -print0 | xargs -0 rm -f

If you need to do this through Java then you could invoke the above command using Java's Runtime class:

Runtime.getRuntime().exec("find /path/to/windows/dir -type f -name '*xxx*' -print0 | xargs -0 rm -f");
Erik Gillespie
  • 3,929
  • 2
  • 31
  • 48