只删除 linux NOT 目录中的文件

可以运行哪些 delete 命令仅删除给定目录中的文件

  • 不是目录
  • 不是子目录
  • 这些子目录中没有文件。

有些文件没有扩展名,所以 rm *.*不能工作..。

此文件夹中有数千个文件。

有什么建议吗?

118895 次浏览

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.

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

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.

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.

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.

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.

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

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 *

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.

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