How to delete certain files using find and piping them to rm in linux

If you need to delete many files with same characteristics like the same name or the same extension then you can use the find command and then rm to remove them.

So here is what I did:

find /home/ -name "*xml.gz" -print0 | xargs -0 rm

In simple language this is the following:

find: Find
/home/: all files inside the directory /home
-name "*xml.gz": which their names include the expression "*xml.gz". * is any character
-print0: and print no newline characters (\n)
|: pipe them to
xargs -0: split the output of find command and call rm command for each file
rm: remove the file told by xargs

If you need to delete many files with same characteristics like the same name or the same extension then you can use the find command and then rm to remove them.

Leave a Reply