Delete all files in directory (but not directory) - one liner solution

Delete all files in directory (but not directory) - one liner solution

java delete all files with extension in directory
java delete directory
how to delete multiple files in java
fileutils
java delete all files in directory except one
java 8 delete directory
delete all files in a directory
delete all files in folder android

I want to delete all files inside ABC directory.

When I tried with FileUtils.deleteDirectory(new File("C:/test/ABC/")); it also deletes folder ABC.

Is there a one liner solution where I can delete files inside directory but not directory?


import org.apache.commons.io.FileUtils;

FileUtils.cleanDirectory(directory); 

There is this method available in the same file. This will also recursively deletes all sub-folders and files under them.

Docs: org.apache.commons.io.FileUtils.cleanDirectory

Remove all files/directories except for one file, Thanks, and yes I was looking for a one-line solution. It's too time will remove all regular files (recursively, including hidden ones) except file.txt . To remove directories, change -type f to -type d and add -r option to rm . In bash Another take in a different direction (iff there are no spaces in file names) ls | grep In one line: Let us look at one additional example, the command below will wipe out all files excluding .gz, .odt, and .jpg files in the current directory: $ find . -type f -not \ (-name '*gz' -or -name '*odt' -or -name '*.jpg' \) -delete. Remove All Files Except File Extensions.


Do you mean like?

for(File file: dir.listFiles()) 
    if (!file.isDirectory()) 
        file.delete();

This will only delete files, not directories.

3 Ways to Delete All Files in a Directory Except One or Few Files , Thats all! If you have any other command line techniques in mind for the same purpose, do not forget to share with us via our feedback section  Introduction: You can remove all files in a directory using unlink command. Another option is to use the rm command to delete all files in a directory. This page explains how to delete all files in a directory using the command line options.


Peter Lawrey's answer is great because it is simple and not depending on anything special, and it's the way you should do it. If you need something that removes subdirectories and their contents as well, use recursion:

void purgeDirectory(File dir) {
    for (File file: dir.listFiles()) {
        if (file.isDirectory())
            purgeDirectory(file);
        file.delete();
    }
}

To spare subdirectories and their contents (part of your question), modify as follows:

void purgeDirectoryButKeepSubDirectories(File dir) {
    for (File file: dir.listFiles()) {
        if (!file.isDirectory())
            file.delete();
    }
}

Or, since you wanted a one-line solution:

for (File file: dir.listFiles())
    if (!file.isDirectory())
        file.delete();

Using an external library for such a trivial task is not a good idea unless you need this library for something else anyway, in which case it is preferrable to use existing code. You appear to be using the Apache library anyway so use its FileUtils.cleanDirectory() method.

Unix - delete files and folders excluding PATTERN, So I cooked up a single-line unix command to do the work. Here's another find solution. I also added -type f so that it would not attempt to delete directories. I gave one of my test files 0 permissions and the command above deleted it  - Create a bat file to loop through all the files name in the txt file and move(or copy) them to a new folder. - Check all the files in the new folder that are copied after the script finished. - Go back to your old folder and delete all. - Copy back all the files you need to be remained.


Java 8 Stream

This deletes only files from ABC (sub-directories are untouched):

Arrays.stream(new File("C:/test/ABC/").listFiles()).forEach(File::delete);

This deletes only files from ABC (and sub-directories):

Files.walk(Paths.get("C:/test/ABC/"))
                .filter(Files::isRegularFile)
                .map(Path::toFile)
                .forEach(File::delete);

^ This version requires handling the IOException

Delete all files from a folder and its sub folders, If it does not work, check if you have an up-to-date version of Powershell Reading between the lines on your original question I can offer an alternative BATCH code line you can use. Use PowerShell to Delete a Single File or Folder. Delete all files from current directory and sub-directories but leaving the folders  "When you call Directory.Delete and a file is open in such way, Directory.Delete succeeds in deleting all files but when Directory.Delete calls RemoveDirectory a "directory is not empty" exception is thrown because there is a file marked for deletion but not actually deleted." – Kiquenet Jul 19 '13 at 7:54 71


Or to use this in Java 8:

try {
  Files.newDirectoryStream( directory ).forEach( file -> {
    try { Files.delete( file ); }
    catch ( IOException e ) { throw new UncheckedIOException(e); }
  } );
}
catch ( IOException e ) {
  e.printStackTrace();
}

It's a pity the exception handling is so bulky, otherwise it would be a one-liner ...

Working with Files and Directories – Introduction to the Command , How can I create, copy and delete files and directories? How can What would the output look like if the wildcard could not be matched? You can reuse one of these commands directly by referring to the number of that command. Find the line number in your history for the command that listed all the .sh files in /usr/bin . Removing empty directory in Java is as simple as calling File.delete() (standard IO) or Files.delete() (NIO) method. However, if the folder is not empty (for example contains one or more files or subdirectories), these methods will refuse to remove it. In this post I want to present few ways to recursively remove the directory together with its


Remove or Delete a Directory in Linux, For those who are using Gnome's File can delete a directory using the graphical The command-line option is even more effective when a coder has to remove multiple Therefore, it does not delete it permanently, and one can recover it. The best solution for the error is to remove all files in the current directory with the​  This recursively lists all files and folders inside the root folder (regardless of attributes), but there is no command I know of that deletes both single files and directories. Besides, one would have to start at the deepest level with folder deletion, but this command begins with the highest level. So it looks like it is necessary to delete files and folders separately. Let us start with files: del /a /f /q "d:\temp\Root Folder\*" This deals with all files (regardless of attributes) at the


Find and Remove Files With One Linux Command On Fly, I am new to Unix and Linux command line. How do I find in a single go. However, the rm command does not support search criteria. Find all core files in the / (root) directory and remove them (be careful with this command): # find / -​name What could be the possible reason and the solution. Thanks The command DEL is used to delete all files in the specified directory. The option /A is necessary to process really all files including files with the hidden attribute which DEL would ignore without using option /A. The option /F is necessary to force deletion of files with the read-only attribute set.


How to Remove (Delete) Directory in Linux, rm is a command-line utility for deleting files and directories. To delete an empty directory, use the -d ( --dir ) option and to delete a non-empty directory, and all of and manually or using a loop to remove sub-directories one by one. The easiest solution is first to delete all files within the directory with the  then do dir to see what files are in there, then u can delete them, I think delete *.* should work to remove all files. all those are Linux based commands cd - moves to folder pwd - show u current location /directory. Regards, ML **Please Rate All Helpful Responses **