Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm currently studying Linux reading "Linux essentials" available on LPIC website.
On page 90, a comparison of "find ... -exec rm -i ..." and "find ... | xargs rm -i" is made.
I've just tried these two commands in order to understand how they works. The first one (using "-exec") seems to perfectly work, the second one does not.
As shown below, the rm command's "-i" option ask for a confirmation but I can not give an answer. The prompt reapears instead a way to give my deletion agreement.
If you remove the "-i" from these commands they both attempt non-interactive removal of files.
The one using -exec attempts one rm command per file to remove. The one with xargs writes a list of filenames to xargs where the xargs command puts as many of these text strings as possible into the arguments of an rm command (before doing 2nd and later commands if the input is large enough).
I suggest avoiding the xargs version as the files removed by rm are the result of text processing by xargs and may not be the original filenames as seen by find. For instance if root runs this in a cron job:
1) the -i invokes the interactive removal, so you have to confirm that you want to remove the file.
Fixing this is easy - just take the -i option off.
2) the {} sequence is the internal identification used by find for the file name.
Since you are using xargs (which reads its parameter from stdin or the pipe) you don't use it on the rm command - xargs will put it there.
Sorry, English is a foreign language for me, I guess we don't understand :
- I understand how the command works. But it seems to not work as it should.
- I want to use interactive mode
- BUT (!) If the command above ask me to confirm deletion (as it should do, and does), it does not wait for my answer.
Thus, I understand how it should work but I don't understand why it does not wait for my answer.
Code:
rm: remove regular file `./myscript'?
--> I can not say "yes" or "no" : the prompt appear without waiting.
That is because rm can't access stdin - it is coming from the find. If only one file is there, the script terminates as stdin is closed. If there are two files, the second file name is used to answer the prompt... and if it isn't "yes", then again it exits. If you have many files, then you should see the prompt for every other file... and unless one of the odd numbered files is "yes", nothing will be done.
Not with a pipe. the '{}' is only understood by the find process. What you get is "... xargs -I {} rm -i {}" and the error (well usually) "file not found".
Not with a pipe. the '{}' is only understood by the find process. What you get is "... xargs -I {} rm -i {}" and the error (well usually) "file not found".
Sorry, but the xargs command does allow you to define a string that will be replaced with an argument read from stdin, and "{}" is as good a string as any other.
Not with a pipe. the '{}' is only understood by the find process. What you get is "... xargs -I {} rm -i {}" and the error (well usually) "file not found".
The "-I {}" used with xargs prepares for the {} later in the command (although it's pointless in this case where all the filenames come at the end).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.