I want to delete files from a specific directory recursively. So, I have used
find . -wholename "*.txt" -delete
We can also delete the files using
rm -rf *.txt
What is the difference between deletion of file using rm
and find
??
find . -name abd.txt -delete
tries to remove all files named abd.txt
that are somewhere in the directory tree of .
find . -wholename abd.txt -delete
tries to remove all files with a full pathname of abd.txt
somewhere in the directory tree of .
No such files will ever exist: when using find .
, all full pathnames of files found will start with ./
, so even a file in the current directory named abd.txt
will have path ./abd.txt
, and it will not match.
find . -wholename ./abd.txt -delete
will remove the file in the current directory named abd.txt
.
find -wholename ./abd.txt -delete
will do the same.
The removal will fail if abd.txt
is a nonempty directory.
(I just tried the above with GNU find 4.6.0; other versions may behave differently.)
rm -rf abd.txt
also tries to remove abd.txt
in the current directory, and if it is a nonempty directory, it will remove it, and everything in it.
To do this with find
, you might use
find . -depth \( -wholename ./abd.txt -o -wholename ./abd.txt/\* \) -delete