How to delete a file after using find and grep to search for file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















I'm using this command to search for a specific file:



find . -type f -name Client.php | xargs grep WARNING


I can't figure out how to add the delete command so that way the user is prompted before the delete command happens.










share|improve this question






















  • Which find versson do you use, GNU one? Try find . -type f -name Client.php -exec grep -q WARNING ; -delete (warning: deletes without asking).

    – jimmij
    Feb 4 at 20:56











  • @jimmij version 4.4.2

    – music-code
    Feb 4 at 21:02















0















I'm using this command to search for a specific file:



find . -type f -name Client.php | xargs grep WARNING


I can't figure out how to add the delete command so that way the user is prompted before the delete command happens.










share|improve this question






















  • Which find versson do you use, GNU one? Try find . -type f -name Client.php -exec grep -q WARNING ; -delete (warning: deletes without asking).

    – jimmij
    Feb 4 at 20:56











  • @jimmij version 4.4.2

    – music-code
    Feb 4 at 21:02













0












0








0








I'm using this command to search for a specific file:



find . -type f -name Client.php | xargs grep WARNING


I can't figure out how to add the delete command so that way the user is prompted before the delete command happens.










share|improve this question














I'm using this command to search for a specific file:



find . -type f -name Client.php | xargs grep WARNING


I can't figure out how to add the delete command so that way the user is prompted before the delete command happens.







shell-script command-line






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 4 at 20:39









music-codemusic-code

1




1












  • Which find versson do you use, GNU one? Try find . -type f -name Client.php -exec grep -q WARNING ; -delete (warning: deletes without asking).

    – jimmij
    Feb 4 at 20:56











  • @jimmij version 4.4.2

    – music-code
    Feb 4 at 21:02

















  • Which find versson do you use, GNU one? Try find . -type f -name Client.php -exec grep -q WARNING ; -delete (warning: deletes without asking).

    – jimmij
    Feb 4 at 20:56











  • @jimmij version 4.4.2

    – music-code
    Feb 4 at 21:02
















Which find versson do you use, GNU one? Try find . -type f -name Client.php -exec grep -q WARNING ; -delete (warning: deletes without asking).

– jimmij
Feb 4 at 20:56





Which find versson do you use, GNU one? Try find . -type f -name Client.php -exec grep -q WARNING ; -delete (warning: deletes without asking).

– jimmij
Feb 4 at 20:56













@jimmij version 4.4.2

– music-code
Feb 4 at 21:02





@jimmij version 4.4.2

– music-code
Feb 4 at 21:02










2 Answers
2






active

oldest

votes


















3














You can't pipe to xargs here and rejoin the find, find can run commands like grep on its own.



The -exec primary runs a command for a file or files. It succeeds and continues processing the operands to the right only when the command itself exits with a successful result, so



find . -type f -name Client.php -exec grep -q WARNING ; -exec echo ;


will print out the path of every file named "Client.php" that contains "WARNING" inside it. You can change that second -exec ... ; to -delete to have it delete, or change the command to use rm or mv to deal with them.




The semicolons make sure only one file is checked at a time, and that we get this true-on-success behaviour:




If the primary expression is punctuated by a <semicolon>, the utility utility_name shall be invoked once for each pathname and the primary shall evaluate as true if the utility returns a zero value as exit status.




The whole -exec ... ; is one chunk, so if you're using -delete you want to remove the semicolon along with -exec and the command.




I suggest running the command above first and making sure that it really does only include only the files you want before you actually delete them. However, a full delete-without-asking command would be



find . -type f -name Client.php -exec grep -q WARNING ; -delete


Proceed with caution!






share|improve this answer























  • thanks for that clarification

    – music-code
    Feb 4 at 21:06


















0














You could write a script:



#!/bin/bash

FINDFILE=Client.php
if [[ $(find . -type f -name $FINDFILE | xargs grep "WARNING") ]] ; then
rm -i $FINDFILE
else
echo "File with warning not found."
fi


Place the script in your /usr/local/bin, chmod +x /usr/local/bin/scriptname.

Now you can execute it from anywhere at any time. (It's also best to leave off the .sh extension)






share|improve this answer


















  • 1





    If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

    – Kusalananda
    Feb 4 at 23:06











  • @Kusalananda That looks like what the OP is looking for.

    – Michael Prokopec
    Feb 4 at 23:07











  • @MichaelProkopec very nice solution! Thanks!

    – music-code
    Feb 6 at 14:17











  • @music-code is it the answer, does something need clarification?

    – Michael Prokopec
    Feb 6 at 18:32











  • @MichaelProkopec No, I get what you did now

    – music-code
    Feb 7 at 21:20










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498666%2fhow-to-delete-a-file-after-using-find-and-grep-to-search-for-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can't pipe to xargs here and rejoin the find, find can run commands like grep on its own.



The -exec primary runs a command for a file or files. It succeeds and continues processing the operands to the right only when the command itself exits with a successful result, so



find . -type f -name Client.php -exec grep -q WARNING ; -exec echo ;


will print out the path of every file named "Client.php" that contains "WARNING" inside it. You can change that second -exec ... ; to -delete to have it delete, or change the command to use rm or mv to deal with them.




The semicolons make sure only one file is checked at a time, and that we get this true-on-success behaviour:




If the primary expression is punctuated by a <semicolon>, the utility utility_name shall be invoked once for each pathname and the primary shall evaluate as true if the utility returns a zero value as exit status.




The whole -exec ... ; is one chunk, so if you're using -delete you want to remove the semicolon along with -exec and the command.




I suggest running the command above first and making sure that it really does only include only the files you want before you actually delete them. However, a full delete-without-asking command would be



find . -type f -name Client.php -exec grep -q WARNING ; -delete


Proceed with caution!






share|improve this answer























  • thanks for that clarification

    – music-code
    Feb 4 at 21:06















3














You can't pipe to xargs here and rejoin the find, find can run commands like grep on its own.



The -exec primary runs a command for a file or files. It succeeds and continues processing the operands to the right only when the command itself exits with a successful result, so



find . -type f -name Client.php -exec grep -q WARNING ; -exec echo ;


will print out the path of every file named "Client.php" that contains "WARNING" inside it. You can change that second -exec ... ; to -delete to have it delete, or change the command to use rm or mv to deal with them.




The semicolons make sure only one file is checked at a time, and that we get this true-on-success behaviour:




If the primary expression is punctuated by a <semicolon>, the utility utility_name shall be invoked once for each pathname and the primary shall evaluate as true if the utility returns a zero value as exit status.




The whole -exec ... ; is one chunk, so if you're using -delete you want to remove the semicolon along with -exec and the command.




I suggest running the command above first and making sure that it really does only include only the files you want before you actually delete them. However, a full delete-without-asking command would be



find . -type f -name Client.php -exec grep -q WARNING ; -delete


Proceed with caution!






share|improve this answer























  • thanks for that clarification

    – music-code
    Feb 4 at 21:06













3












3








3







You can't pipe to xargs here and rejoin the find, find can run commands like grep on its own.



The -exec primary runs a command for a file or files. It succeeds and continues processing the operands to the right only when the command itself exits with a successful result, so



find . -type f -name Client.php -exec grep -q WARNING ; -exec echo ;


will print out the path of every file named "Client.php" that contains "WARNING" inside it. You can change that second -exec ... ; to -delete to have it delete, or change the command to use rm or mv to deal with them.




The semicolons make sure only one file is checked at a time, and that we get this true-on-success behaviour:




If the primary expression is punctuated by a <semicolon>, the utility utility_name shall be invoked once for each pathname and the primary shall evaluate as true if the utility returns a zero value as exit status.




The whole -exec ... ; is one chunk, so if you're using -delete you want to remove the semicolon along with -exec and the command.




I suggest running the command above first and making sure that it really does only include only the files you want before you actually delete them. However, a full delete-without-asking command would be



find . -type f -name Client.php -exec grep -q WARNING ; -delete


Proceed with caution!






share|improve this answer













You can't pipe to xargs here and rejoin the find, find can run commands like grep on its own.



The -exec primary runs a command for a file or files. It succeeds and continues processing the operands to the right only when the command itself exits with a successful result, so



find . -type f -name Client.php -exec grep -q WARNING ; -exec echo ;


will print out the path of every file named "Client.php" that contains "WARNING" inside it. You can change that second -exec ... ; to -delete to have it delete, or change the command to use rm or mv to deal with them.




The semicolons make sure only one file is checked at a time, and that we get this true-on-success behaviour:




If the primary expression is punctuated by a <semicolon>, the utility utility_name shall be invoked once for each pathname and the primary shall evaluate as true if the utility returns a zero value as exit status.




The whole -exec ... ; is one chunk, so if you're using -delete you want to remove the semicolon along with -exec and the command.




I suggest running the command above first and making sure that it really does only include only the files you want before you actually delete them. However, a full delete-without-asking command would be



find . -type f -name Client.php -exec grep -q WARNING ; -delete


Proceed with caution!







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 4 at 20:59









Michael HomerMichael Homer

49.1k8133172




49.1k8133172












  • thanks for that clarification

    – music-code
    Feb 4 at 21:06

















  • thanks for that clarification

    – music-code
    Feb 4 at 21:06
















thanks for that clarification

– music-code
Feb 4 at 21:06





thanks for that clarification

– music-code
Feb 4 at 21:06













0














You could write a script:



#!/bin/bash

FINDFILE=Client.php
if [[ $(find . -type f -name $FINDFILE | xargs grep "WARNING") ]] ; then
rm -i $FINDFILE
else
echo "File with warning not found."
fi


Place the script in your /usr/local/bin, chmod +x /usr/local/bin/scriptname.

Now you can execute it from anywhere at any time. (It's also best to leave off the .sh extension)






share|improve this answer


















  • 1





    If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

    – Kusalananda
    Feb 4 at 23:06











  • @Kusalananda That looks like what the OP is looking for.

    – Michael Prokopec
    Feb 4 at 23:07











  • @MichaelProkopec very nice solution! Thanks!

    – music-code
    Feb 6 at 14:17











  • @music-code is it the answer, does something need clarification?

    – Michael Prokopec
    Feb 6 at 18:32











  • @MichaelProkopec No, I get what you did now

    – music-code
    Feb 7 at 21:20















0














You could write a script:



#!/bin/bash

FINDFILE=Client.php
if [[ $(find . -type f -name $FINDFILE | xargs grep "WARNING") ]] ; then
rm -i $FINDFILE
else
echo "File with warning not found."
fi


Place the script in your /usr/local/bin, chmod +x /usr/local/bin/scriptname.

Now you can execute it from anywhere at any time. (It's also best to leave off the .sh extension)






share|improve this answer


















  • 1





    If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

    – Kusalananda
    Feb 4 at 23:06











  • @Kusalananda That looks like what the OP is looking for.

    – Michael Prokopec
    Feb 4 at 23:07











  • @MichaelProkopec very nice solution! Thanks!

    – music-code
    Feb 6 at 14:17











  • @music-code is it the answer, does something need clarification?

    – Michael Prokopec
    Feb 6 at 18:32











  • @MichaelProkopec No, I get what you did now

    – music-code
    Feb 7 at 21:20













0












0








0







You could write a script:



#!/bin/bash

FINDFILE=Client.php
if [[ $(find . -type f -name $FINDFILE | xargs grep "WARNING") ]] ; then
rm -i $FINDFILE
else
echo "File with warning not found."
fi


Place the script in your /usr/local/bin, chmod +x /usr/local/bin/scriptname.

Now you can execute it from anywhere at any time. (It's also best to leave off the .sh extension)






share|improve this answer













You could write a script:



#!/bin/bash

FINDFILE=Client.php
if [[ $(find . -type f -name $FINDFILE | xargs grep "WARNING") ]] ; then
rm -i $FINDFILE
else
echo "File with warning not found."
fi


Place the script in your /usr/local/bin, chmod +x /usr/local/bin/scriptname.

Now you can execute it from anywhere at any time. (It's also best to leave off the .sh extension)







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 4 at 23:02









Michael ProkopecMichael Prokopec

1,532218




1,532218







  • 1





    If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

    – Kusalananda
    Feb 4 at 23:06











  • @Kusalananda That looks like what the OP is looking for.

    – Michael Prokopec
    Feb 4 at 23:07











  • @MichaelProkopec very nice solution! Thanks!

    – music-code
    Feb 6 at 14:17











  • @music-code is it the answer, does something need clarification?

    – Michael Prokopec
    Feb 6 at 18:32











  • @MichaelProkopec No, I get what you did now

    – music-code
    Feb 7 at 21:20












  • 1





    If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

    – Kusalananda
    Feb 4 at 23:06











  • @Kusalananda That looks like what the OP is looking for.

    – Michael Prokopec
    Feb 4 at 23:07











  • @MichaelProkopec very nice solution! Thanks!

    – music-code
    Feb 6 at 14:17











  • @music-code is it the answer, does something need clarification?

    – Michael Prokopec
    Feb 6 at 18:32











  • @MichaelProkopec No, I get what you did now

    – music-code
    Feb 7 at 21:20







1




1





If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

– Kusalananda
Feb 4 at 23:06





If a file with the name Client.php anywhere under the current directory is found that contains the string WARNING, the Client.php in the current directory is removed?

– Kusalananda
Feb 4 at 23:06













@Kusalananda That looks like what the OP is looking for.

– Michael Prokopec
Feb 4 at 23:07





@Kusalananda That looks like what the OP is looking for.

– Michael Prokopec
Feb 4 at 23:07













@MichaelProkopec very nice solution! Thanks!

– music-code
Feb 6 at 14:17





@MichaelProkopec very nice solution! Thanks!

– music-code
Feb 6 at 14:17













@music-code is it the answer, does something need clarification?

– Michael Prokopec
Feb 6 at 18:32





@music-code is it the answer, does something need clarification?

– Michael Prokopec
Feb 6 at 18:32













@MichaelProkopec No, I get what you did now

– music-code
Feb 7 at 21:20





@MichaelProkopec No, I get what you did now

– music-code
Feb 7 at 21:20

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498666%2fhow-to-delete-a-file-after-using-find-and-grep-to-search-for-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay