Remove only files in directory on linux NOT directories
LinuxRmLinux Problem Overview
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?
Linux Solutions
Solution 1 - Linux
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.
Solution 2 - Linux
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.
Solution 3 - Linux
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.
Solution 4 - Linux
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 *
Solution 5 - Linux
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.
Solution 6 - Linux
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.
Solution 7 - Linux
rm dirname/*
? Without -f
it won't force-delete, without -r
it won't recurse and delete directories as well as files.
Solution 8 - Linux
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
Solution 9 - Linux
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.
Solution 10 - Linux
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