Reputation: 18109
When I do
rm file.txt
or
rm *.txt
I'm prompted for each file, since I didn't specify the -f
option to rm
.
But when I do this:
find . -type f -name '*.txt' | xargs rm
the files are removed without the confirmation.
What is the logics behind this? Is it possible to find the reason in some documentation? I cannot explain why this would be the case.
Upvotes: 33
Views: 87437
Reputation: 1581
ls "fnames with wild chars" | xargs -I{} rm -v {}
Here -I{}
is used to replace all inputs be substituted in place of {}
.
NOTE: It can be catastrophic to use names with improper wildcards as a file that is removed cannot be retrieved. Use it with caution and try to refrain from using recursive(-r) flag.
Upvotes: 0
Reputation: 641
You want to remove into PATH_DIR1 existing files in PATH_DIR2 :
find PATH_DIR1 -type f -exec basename '{}' ';' | xargs printf -- 'PATH_DIR2/%s\n' | xargs rm
Explanations :
find PATH_DIR1 -type f -exec basename '{}' ';'
xargs printf -- 'PATH_DIR2/%s\n'
xargs rm
Upvotes: -1
Reputation: 783
you can use this simple command to solve your problem
find . -type f -name '*.txt' -delete
Upvotes: 27
Reputation: 25
Depending on your version of xargs you may have the --no-run-if-empty GNU extension option available to you:
find . -type f -name '*.txt' | xargs --no-run-if-empty rm -rf
Upvotes: 2
Reputation: 7544
You have an alias set for the rm command to 'rm -i'. Therefore if you invoke the command directly as in
rm file.txt
or
rm *.txt
the alias will be expanded. If you will call it with xargs as in
find . -type f -name '*.txt' | xargs rm
The rm is passed as a simple string argument to xargs and is later invoked by xargs without alias substitution of the shell. You alias is probably defined in ~/.bashrc, in case you want to remove it.
Upvotes: 36