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.
As the man page for xargs says (under the
-aoption): "If you use this option, stdin remains unchanged when commands are run. Otherwise, stdin is redirected from /dev/null."Since you're not using the
-aoption, eachrm -icommand thatxargsis running gets its stdin from /dev/null (i.e. no input is available). Whenrmasks whether to remove a particular file, the answer is effectively "no" because /dev/null gives no reply.rmreceives an EOF on its input, so it does not remove that file, and goes on to the next file.Besides using
find -execas unxnut explained, another way to do it is to use the-o(or--open-tty) option withxargs:That's probably the ideal way, because it allows
rm -ito handle interactive confirmation itself, as designed.Another way is to use the
-p(or--interactive) option withxargs:With this approach,
xargshandles the interactive confirmation instead of havingrmdo it. You may also want to use-n 1, so that each prompt only asks about one file:The advantage of using
xargsoverfind -execis that you can use it with any command that generates the file path arguments, not just withfind.