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 ??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
find . -name abd.txt -deletetries to remove all files namedabd.txtthat are somewhere in the directory tree of.find . -wholename abd.txt -deletetries to remove all files with a full pathname ofabd.txtsomewhere 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 namedabd.txtwill have path./abd.txt, and it will not match.find . -wholename ./abd.txt -deletewill remove the file in the current directory namedabd.txt.find -wholename ./abd.txt -deletewill do the same.The removal will fail if
abd.txtis a nonempty directory.(I just tried the above with GNU find 4.6.0; other versions may behave differently.)
rm -rf abd.txtalso tries to removeabd.txtin 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 usefind . -depth \( -wholename ./abd.txt -o -wholename ./abd.txt/\* \) -delete