What is an equivalent of rm `find lib/ -name *.swp` without find?












2















I would like to remove all files in the lib directory with .swp in the end.



How can I do this without find in:



rm `find lib/ -name *.swp`









share|improve this question

























  • Why do you wish to remove 'find', academic reason or something more precise?

    – bbaja42
    Jan 21 '13 at 22:08











  • I'm just curious how this can be done in other way.

    – Patryk
    Jan 21 '13 at 22:12











  • If you have Perl installed you can use find2perl to generate an equivalent script, which you can customise as needed.

    – pgs
    Jan 22 '13 at 0:55
















2















I would like to remove all files in the lib directory with .swp in the end.



How can I do this without find in:



rm `find lib/ -name *.swp`









share|improve this question

























  • Why do you wish to remove 'find', academic reason or something more precise?

    – bbaja42
    Jan 21 '13 at 22:08











  • I'm just curious how this can be done in other way.

    – Patryk
    Jan 21 '13 at 22:12











  • If you have Perl installed you can use find2perl to generate an equivalent script, which you can customise as needed.

    – pgs
    Jan 22 '13 at 0:55














2












2








2








I would like to remove all files in the lib directory with .swp in the end.



How can I do this without find in:



rm `find lib/ -name *.swp`









share|improve this question
















I would like to remove all files in the lib directory with .swp in the end.



How can I do this without find in:



rm `find lib/ -name *.swp`






find wildcards rm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 10 at 19:04









Rui F Ribeiro

40.5k1479137




40.5k1479137










asked Jan 21 '13 at 22:04









PatrykPatryk

3,735134253




3,735134253













  • Why do you wish to remove 'find', academic reason or something more precise?

    – bbaja42
    Jan 21 '13 at 22:08











  • I'm just curious how this can be done in other way.

    – Patryk
    Jan 21 '13 at 22:12











  • If you have Perl installed you can use find2perl to generate an equivalent script, which you can customise as needed.

    – pgs
    Jan 22 '13 at 0:55



















  • Why do you wish to remove 'find', academic reason or something more precise?

    – bbaja42
    Jan 21 '13 at 22:08











  • I'm just curious how this can be done in other way.

    – Patryk
    Jan 21 '13 at 22:12











  • If you have Perl installed you can use find2perl to generate an equivalent script, which you can customise as needed.

    – pgs
    Jan 22 '13 at 0:55

















Why do you wish to remove 'find', academic reason or something more precise?

– bbaja42
Jan 21 '13 at 22:08





Why do you wish to remove 'find', academic reason or something more precise?

– bbaja42
Jan 21 '13 at 22:08













I'm just curious how this can be done in other way.

– Patryk
Jan 21 '13 at 22:12





I'm just curious how this can be done in other way.

– Patryk
Jan 21 '13 at 22:12













If you have Perl installed you can use find2perl to generate an equivalent script, which you can customise as needed.

– pgs
Jan 22 '13 at 0:55





If you have Perl installed you can use find2perl to generate an equivalent script, which you can customise as needed.

– pgs
Jan 22 '13 at 0:55










2 Answers
2






active

oldest

votes


















4














The benefit to find is that it is recursive. Some modern shells offer search recursion, but it is not in the POSIX standard, so you can not rely on them to work everywhere. Here is an example that works in bash 4.0 and higher.



shopt -s globstar
for f in **/*.swp; do
rm -- "$f"
done


As far as the find command that you already have, it will break on any files that contain whitespace such as spaces. Here is a fixed example:



find lib/ -name '*.swp' -exec rm {} +


With some versions of find, you can use -delete:



find lib/ -name '*.swp' -delete





share|improve this answer


























  • Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

    – Stéphane Chazelas
    Jan 28 '13 at 23:44



















1














cd lib && ls -R  | grep '.swp$' | xargs -d 'n' rm


As for your original, I'd have done it:



find lib -name '*.swp' | xargs -d 'n' rm


because if find returns no results you won't get an error.



You might also want to use ! -type d so find doesn't return any directories which happen to be called *.swp, because rm would fail to remove them. You could do something similar with my alternative using ls -RF because directories would get a trailing / so not match the regex.






share|improve this answer


























  • Both examples break on files with spaces.

    – jordanm
    Jan 21 '13 at 22:34











  • Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

    – user17591
    Jan 21 '13 at 22:41











  • fixed by setting newline as the delimiter

    – Jonathan Wakely
    Jan 21 '13 at 22:41








  • 1





    @user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

    – jordanm
    Jan 21 '13 at 22:42








  • 1





    So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

    – jordanm
    Jan 21 '13 at 22:43













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%2f62075%2fwhat-is-an-equivalent-of-rm-find-lib-name-swp-without-find%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









4














The benefit to find is that it is recursive. Some modern shells offer search recursion, but it is not in the POSIX standard, so you can not rely on them to work everywhere. Here is an example that works in bash 4.0 and higher.



shopt -s globstar
for f in **/*.swp; do
rm -- "$f"
done


As far as the find command that you already have, it will break on any files that contain whitespace such as spaces. Here is a fixed example:



find lib/ -name '*.swp' -exec rm {} +


With some versions of find, you can use -delete:



find lib/ -name '*.swp' -delete





share|improve this answer


























  • Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

    – Stéphane Chazelas
    Jan 28 '13 at 23:44
















4














The benefit to find is that it is recursive. Some modern shells offer search recursion, but it is not in the POSIX standard, so you can not rely on them to work everywhere. Here is an example that works in bash 4.0 and higher.



shopt -s globstar
for f in **/*.swp; do
rm -- "$f"
done


As far as the find command that you already have, it will break on any files that contain whitespace such as spaces. Here is a fixed example:



find lib/ -name '*.swp' -exec rm {} +


With some versions of find, you can use -delete:



find lib/ -name '*.swp' -delete





share|improve this answer


























  • Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

    – Stéphane Chazelas
    Jan 28 '13 at 23:44














4












4








4







The benefit to find is that it is recursive. Some modern shells offer search recursion, but it is not in the POSIX standard, so you can not rely on them to work everywhere. Here is an example that works in bash 4.0 and higher.



shopt -s globstar
for f in **/*.swp; do
rm -- "$f"
done


As far as the find command that you already have, it will break on any files that contain whitespace such as spaces. Here is a fixed example:



find lib/ -name '*.swp' -exec rm {} +


With some versions of find, you can use -delete:



find lib/ -name '*.swp' -delete





share|improve this answer















The benefit to find is that it is recursive. Some modern shells offer search recursion, but it is not in the POSIX standard, so you can not rely on them to work everywhere. Here is an example that works in bash 4.0 and higher.



shopt -s globstar
for f in **/*.swp; do
rm -- "$f"
done


As far as the find command that you already have, it will break on any files that contain whitespace such as spaces. Here is a fixed example:



find lib/ -name '*.swp' -exec rm {} +


With some versions of find, you can use -delete:



find lib/ -name '*.swp' -delete






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 22 '13 at 7:18









Stéphane Chazelas

307k57581936




307k57581936










answered Jan 21 '13 at 22:39









jordanmjordanm

30.8k38695




30.8k38695













  • Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

    – Stéphane Chazelas
    Jan 28 '13 at 23:44



















  • Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

    – Stéphane Chazelas
    Jan 28 '13 at 23:44

















Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

– Stéphane Chazelas
Jan 28 '13 at 23:44





Note that that for loop above would rather be equivalent to find -L . -name '.*' ! -name . -prune -o -name '*.swp' -exec rm {} +

– Stéphane Chazelas
Jan 28 '13 at 23:44













1














cd lib && ls -R  | grep '.swp$' | xargs -d 'n' rm


As for your original, I'd have done it:



find lib -name '*.swp' | xargs -d 'n' rm


because if find returns no results you won't get an error.



You might also want to use ! -type d so find doesn't return any directories which happen to be called *.swp, because rm would fail to remove them. You could do something similar with my alternative using ls -RF because directories would get a trailing / so not match the regex.






share|improve this answer


























  • Both examples break on files with spaces.

    – jordanm
    Jan 21 '13 at 22:34











  • Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

    – user17591
    Jan 21 '13 at 22:41











  • fixed by setting newline as the delimiter

    – Jonathan Wakely
    Jan 21 '13 at 22:41








  • 1





    @user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

    – jordanm
    Jan 21 '13 at 22:42








  • 1





    So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

    – jordanm
    Jan 21 '13 at 22:43


















1














cd lib && ls -R  | grep '.swp$' | xargs -d 'n' rm


As for your original, I'd have done it:



find lib -name '*.swp' | xargs -d 'n' rm


because if find returns no results you won't get an error.



You might also want to use ! -type d so find doesn't return any directories which happen to be called *.swp, because rm would fail to remove them. You could do something similar with my alternative using ls -RF because directories would get a trailing / so not match the regex.






share|improve this answer


























  • Both examples break on files with spaces.

    – jordanm
    Jan 21 '13 at 22:34











  • Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

    – user17591
    Jan 21 '13 at 22:41











  • fixed by setting newline as the delimiter

    – Jonathan Wakely
    Jan 21 '13 at 22:41








  • 1





    @user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

    – jordanm
    Jan 21 '13 at 22:42








  • 1





    So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

    – jordanm
    Jan 21 '13 at 22:43
















1












1








1







cd lib && ls -R  | grep '.swp$' | xargs -d 'n' rm


As for your original, I'd have done it:



find lib -name '*.swp' | xargs -d 'n' rm


because if find returns no results you won't get an error.



You might also want to use ! -type d so find doesn't return any directories which happen to be called *.swp, because rm would fail to remove them. You could do something similar with my alternative using ls -RF because directories would get a trailing / so not match the regex.






share|improve this answer















cd lib && ls -R  | grep '.swp$' | xargs -d 'n' rm


As for your original, I'd have done it:



find lib -name '*.swp' | xargs -d 'n' rm


because if find returns no results you won't get an error.



You might also want to use ! -type d so find doesn't return any directories which happen to be called *.swp, because rm would fail to remove them. You could do something similar with my alternative using ls -RF because directories would get a trailing / so not match the regex.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 21 '13 at 22:41

























answered Jan 21 '13 at 22:26









Jonathan WakelyJonathan Wakely

1114




1114













  • Both examples break on files with spaces.

    – jordanm
    Jan 21 '13 at 22:34











  • Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

    – user17591
    Jan 21 '13 at 22:41











  • fixed by setting newline as the delimiter

    – Jonathan Wakely
    Jan 21 '13 at 22:41








  • 1





    @user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

    – jordanm
    Jan 21 '13 at 22:42








  • 1





    So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

    – jordanm
    Jan 21 '13 at 22:43





















  • Both examples break on files with spaces.

    – jordanm
    Jan 21 '13 at 22:34











  • Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

    – user17591
    Jan 21 '13 at 22:41











  • fixed by setting newline as the delimiter

    – Jonathan Wakely
    Jan 21 '13 at 22:41








  • 1





    @user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

    – jordanm
    Jan 21 '13 at 22:42








  • 1





    So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

    – jordanm
    Jan 21 '13 at 22:43



















Both examples break on files with spaces.

– jordanm
Jan 21 '13 at 22:34





Both examples break on files with spaces.

– jordanm
Jan 21 '13 at 22:34













Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

– user17591
Jan 21 '13 at 22:41





Does piping to xargs provide a benefit over -delete or even -exec rm {} ?

– user17591
Jan 21 '13 at 22:41













fixed by setting newline as the delimiter

– Jonathan Wakely
Jan 21 '13 at 22:41







fixed by setting newline as the delimiter

– Jonathan Wakely
Jan 21 '13 at 22:41






1




1





@user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

– jordanm
Jan 21 '13 at 22:42







@user17591 - it has a benefits over -exec rm {} ; because it will execute rm one time, rather than once for each file. This benefit does not carry over to -exec rm {} +. The latter is almost always what you want.

– jordanm
Jan 21 '13 at 22:42






1




1





So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

– jordanm
Jan 21 '13 at 22:43







So will using + as a terminator instead of ; to -exec (POSIX, should work everywhere).

– jordanm
Jan 21 '13 at 22:43




















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%2f62075%2fwhat-is-an-equivalent-of-rm-find-lib-name-swp-without-find%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 make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀