How to delete a file after using find and grep to search for file?
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
Whichfind
versson do you use, GNU one? Tryfind . -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
add a comment |
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
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
shell-script command-line
asked Feb 4 at 20:39
music-codemusic-code
1
1
Whichfind
versson do you use, GNU one? Tryfind . -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
add a comment |
Whichfind
versson do you use, GNU one? Tryfind . -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
add a comment |
2 Answers
2
active
oldest
votes
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!
thanks for that clarification
– music-code
Feb 4 at 21:06
add a comment |
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)
1
If a file with the nameClient.php
anywhere under the current directory is found that contains the stringWARNING
, theClient.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
|
show 1 more comment
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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!
thanks for that clarification
– music-code
Feb 4 at 21:06
add a comment |
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!
thanks for that clarification
– music-code
Feb 4 at 21:06
add a comment |
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!
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!
answered Feb 4 at 20:59
Michael HomerMichael Homer
49.1k8133172
49.1k8133172
thanks for that clarification
– music-code
Feb 4 at 21:06
add a comment |
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
add a comment |
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)
1
If a file with the nameClient.php
anywhere under the current directory is found that contains the stringWARNING
, theClient.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
|
show 1 more comment
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)
1
If a file with the nameClient.php
anywhere under the current directory is found that contains the stringWARNING
, theClient.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
|
show 1 more comment
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)
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)
answered Feb 4 at 23:02
Michael ProkopecMichael Prokopec
1,532218
1,532218
1
If a file with the nameClient.php
anywhere under the current directory is found that contains the stringWARNING
, theClient.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
|
show 1 more comment
1
If a file with the nameClient.php
anywhere under the current directory is found that contains the stringWARNING
, theClient.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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Which
find
versson do you use, GNU one? Tryfind . -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