83

What delete command can be run to remove only files in given directory

  • NOT directories
  • NOT sub-directories
  • NOT files in these sub-directories.

Some files don't have extensions so rm *.* wont work...

There are thousands of files in this folder.

Any advice?

Share a link to this question
CC BY-SA 4.0
2

10 Answers 10

74
find PATH -maxdepth 1 -type f -delete

BUT this won't prompt you for confirmation or output what it just deleted. Therefore best to run it without the -delete action first and check that they're the correct files.

Share a link to this answer
CC BY-SA 4.0
3
  • 4
    This is my favorite solution, since it is one command; the fact that it doesn't invoke -exec is also a plus for me, since that requires knowledge of two distinct commands unnecessarily. IMO this should be the accepted answer. Jan 2 '15 at 16:22
  • 6
    on some versions this throws a warning, better turn the arguments around like: find /path/to/file -maxdepth 1 -type f
    – Max
    Oct 23 '17 at 13:36
  • In 2020, I findally learned there is a -delete parameter -- much simpler than -1 and a following xargs. Works on [this version of] OS X as well, as find is annoyingly different between some *IX distributions. Jun 15 '20 at 23:03
69

You can use find with -type f for files only and -maxdepth 1 so find won't search for files in sub-directories of /path/to/directory. rm -i will prompt you on each delete so you can confirm or deny the delete. If you dont care about being asked for confirmation of each delete, change it to rm -fv (-f for force the delete). The -v flag makes it so that with each delete, a message is printed saying what file was just deleted.

find /path/to/directory -maxdepth 1 -type f -exec rm -iv {} \;

This should meet the criteria:

NOT directories
NOT subdirectories
NOT files in these subdirectories.

Share a link to this answer
CC BY-SA 3.0
4
  • 2
    It's worth mentioning that this will also remove files from all sub-directories, not only the current directory.
    – Staven
    Oct 10 '11 at 15:24
  • 1
    @staven This won't work then, want only the files directly under the specified directory to be deleted..
    – AndrewC
    Oct 10 '11 at 15:31
  • 1
    I can never make exec do what I want, so I make the loop explicit: for r in $(find /path/to/directory -type f -maxdepth 1);do rm -v $r;done Mar 17 '13 at 1:59
  • 2
    @Staven No it won't remove files from subdirectories because of the -maxdepth 1.
    – kovacsbv
    May 19 '20 at 15:44
24

Since this is high on google search, the simplest answer is:

rm $directoryPath/*

where $directoryPath is the directory you want to empty. Credits should go to cbm3384 (that for some reason has gotten negative votes for this answer, why?)

If you do not want to confirm:

rm -f $directoryPath/*

If you don't believe try man rm or

mkdir -p 1/2/3; echo 'hello1' > 1/hello1.txt; echo 'hello2' > 1/2/hello2.txt;echo 'hello3' > 1/2/3/hello3.txt
rm 1/2/*

The above creates a directory structure, that has 'helloX.txt' in each folder (X is the directory level). rm 1/2/* deletes hello2.txt and leaves the other structure intact.

Also rm */*/* deletes only hello2.txt. It is the only that matches the pattern.

Just an example of a Makefile that cleans cakephp tmp-directory and leaves the directory structure intact:

clean:
    -rm -f tmp/*
    -rm -f tmp/*/*
    -rm -f tmp/*/*/*
    -rm -f tmp/*/*/*/*

Minus in front of the rm means "do not halt on errors" (unremoved directory returns an error). If you want some level to be saved, just remove that line, e.g. second rm line removes logs.

Let me know if you have a system that does something else (BSD?).

EDIT: I tested this on ubuntu 12.04, osx lion and sourceforge.net shell. All behave like the explanation above.

Share a link to this answer
CC BY-SA 3.0
3
  • 2
    The problem is that this creates standard error, which we don't want in shell scripts.
    – zachaysan
    Aug 31 '15 at 22:18
  • @zachaysan True, if you want silent, redirect stderr to somewhere else: rm -r $path/* 2> /dev/null In anycase you are right, this raises errors and suppressing all errors is generally bad programming. Same applies if you modify the exit status to 0 (success).
    – Juha
    Sep 11 '15 at 23:17
  • How to delete the hidden files as well? For example, a file called .htaccess is not removed. Ahh, this answer has helped me.
    – Pathros
    Apr 20 '20 at 18:47
9

rm won't delete directories by default. So in your example, assuming you're in the parent directory and those are all the files, all you need is:

rm *
Share a link to this answer
CC BY-SA 3.0
1
  • 2
    If you don't want to see the error stating that it can't remove the directory, execute rm * 2> /dev/null
    – SALEH
    Aug 7 '19 at 21:52
7

TL;DR:

find . -maxdepth 1 -type f -delete

Etc:

Not a big deal but the suggestions above didn't work for me because...

find . -type f -maxdepth 1 -delete

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

Share a link to this answer
CC BY-SA 3.0
5

rm dirname/*? Without -f it won't force-delete, without -r it won't recurse and delete directories as well as files.

Share a link to this answer
CC BY-SA 3.0
8
  • rm -f * doesn't delete directories without -r. It just ignores empty directories and doesn't ask for confirmation. rm -rf * deletes also directories.
    – Juha
    Mar 23 '13 at 11:58
  • I ran rm -r dirname/ and it did delete both the top-level directory and all subdirectories and files without any prompting. I tried rm -r dirname/* (with the asterisk) and that deleted all subdirectories and files but left the top-level directory. Conclusion: rm without -f will delete directories if you specify -r.
    – Mark Berry
    Feb 25 '14 at 16:33
  • @MarkBerry May be shell and/or OS dependent, it didn't for me a couple of years ago, and I didn't have any default RM flags set. Feb 25 '14 at 16:39
  • @DaveNewton, if you're able, it would be interesting to know if you still get the same results. At the least, people need to know it may not be universal and they should test first.
    – Mark Berry
    Feb 26 '14 at 3:23
  • 2
    @Juha Which is probably why the default in some systems is to add the -i option. I works be unhappy if I was forbidden to use -r, I use it quite frequently, without mishap. I'm on the command line most of the time and short of a command that moved files instead of deleting them would be the only acceptable solution. Even that would need to delete them "eventually". Jan 15 '15 at 16:45
5

rm -f dirname/* will remove only files without prompting for each file. It will also display "Cannnot remove 'subdirname': Is a directory" for each sub directory.

Share a link to this answer
CC BY-SA 4.0
2
1

For this, I would use find with a max depth of 1 and then exec rm with the file list.

find ./dir -maxdepth 1 -type f -exec rm -rf '{}' \;

Edit: this is essentially the same as what James posted but I didn't see his post until after

Share a link to this answer
CC BY-SA 3.0
0

The following two commands will recursively delete all files and symbolic links in the current directory:

find . -type f -delete
find . -type l -delete

As one command, the following works: find . -type f -delete&&find . -type l -delete

Share a link to this answer
CC BY-SA 4.0
-2

What worked for me is a PERL script:

perl -e 'chdir "subdirectory_name" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'

Run this one level up from the directory you wish to clean: replace "subdirectory_name" with the directories name.

Worked on millions of files without killing the CPU.

Share a link to this answer
CC BY-SA 3.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.