Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing files interactively with find and xargs

Tags:

shell

xargs

rm

I'm trying to pipe some files from the find command to the interactive remove command, so that I can double check the files I'm removing, but I've run into some trouble.

find -name '#*#' -print0 | xargs -0 rm -i

I thought the above would work, but instead I just get a string of "rm: remove regular file ./some/path/#someFile.js#? rm: remove regular file ./another/path/#anotherFile#?..."

Can someone explain to me what's exactly is happening, and what I can do to get my desired results? Thanks.

like image 771
CopOnTheRun Avatar asked Aug 18 '13 19:08

CopOnTheRun


People also ask

How do I delete files with xargs?

If you want to use xargs command to delete these files just pipe it to xargs command with rm function as its argument. In the above case, xargs command will construct separate rm statements for each file name passed to it by the result of find command. That's it.


2 Answers

You can do this by using exec option with find. Use the command

find . -name '#*#' -exec rm -i {} \;

xargs will not work (unless you use options such as -o or -p) because it uses stdin to build commands. Since stdin is already in use, you cannot input the response for query with rm.

like image 113
unxnut Avatar answered Sep 20 '22 09:09

unxnut


you can use this simple command to solve your problem.

find . -name '#*#' -delete
like image 29
Stephen Avatar answered Sep 20 '22 09:09

Stephen