delete all .swp file using rm command












0















I am trying to find and delete all .swp files in my current directory. I was using below command:



rm -i `find . | grep .swp$`


But everytime it is giving me this error:



rm: missing operand
Try `rm --help' for more information.


Anything wrong I am doing?










share|improve this question


















  • 1





    see also How can I delete all files with a particular extension in a particular folder?

    – don_crissti
    Apr 27 '15 at 20:08








  • 1





    Please clarify your question. Do you want to delete files in your current directory, or in your current directory and all its subdirs?

    – dr01
    Apr 28 '15 at 9:14
















0















I am trying to find and delete all .swp files in my current directory. I was using below command:



rm -i `find . | grep .swp$`


But everytime it is giving me this error:



rm: missing operand
Try `rm --help' for more information.


Anything wrong I am doing?










share|improve this question


















  • 1





    see also How can I delete all files with a particular extension in a particular folder?

    – don_crissti
    Apr 27 '15 at 20:08








  • 1





    Please clarify your question. Do you want to delete files in your current directory, or in your current directory and all its subdirs?

    – dr01
    Apr 28 '15 at 9:14














0












0








0








I am trying to find and delete all .swp files in my current directory. I was using below command:



rm -i `find . | grep .swp$`


But everytime it is giving me this error:



rm: missing operand
Try `rm --help' for more information.


Anything wrong I am doing?










share|improve this question














I am trying to find and delete all .swp files in my current directory. I was using below command:



rm -i `find . | grep .swp$`


But everytime it is giving me this error:



rm: missing operand
Try `rm --help' for more information.


Anything wrong I am doing?







linux grep rm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 27 '15 at 20:01









arsenalarsenal

1,098142747




1,098142747








  • 1





    see also How can I delete all files with a particular extension in a particular folder?

    – don_crissti
    Apr 27 '15 at 20:08








  • 1





    Please clarify your question. Do you want to delete files in your current directory, or in your current directory and all its subdirs?

    – dr01
    Apr 28 '15 at 9:14














  • 1





    see also How can I delete all files with a particular extension in a particular folder?

    – don_crissti
    Apr 27 '15 at 20:08








  • 1





    Please clarify your question. Do you want to delete files in your current directory, or in your current directory and all its subdirs?

    – dr01
    Apr 28 '15 at 9:14








1




1





see also How can I delete all files with a particular extension in a particular folder?

– don_crissti
Apr 27 '15 at 20:08







see also How can I delete all files with a particular extension in a particular folder?

– don_crissti
Apr 27 '15 at 20:08






1




1





Please clarify your question. Do you want to delete files in your current directory, or in your current directory and all its subdirs?

– dr01
Apr 28 '15 at 9:14





Please clarify your question. Do you want to delete files in your current directory, or in your current directory and all its subdirs?

– dr01
Apr 28 '15 at 9:14










4 Answers
4






active

oldest

votes


















7














The error message you received probably indicates that no file matched the name pattern .swp$. A generally safer way to do what you wrote (because it will handle any file name):



find . -name '*.swp' -print0 | xargs -0 rm -i --


In the snippet above, I used -print0 so that find separates
records with null characters; the default is newlines, which
unfortunately is valid within a filename. With -print0, the command
works works with any filenames, including any that embed
newlines. Likewise, xargs -0 processes its input as null-separated
records.






share|improve this answer


























  • What is the -print0 flag?

    – T.Woody
    Jan 14 at 17:57











  • Thanks! I will be removing my comments then ;)

    – T.Woody
    Jan 14 at 22:39



















5














Or a variation with find alone e.g.:



find . -name "*.swp" -ok rm {} +


or just without confirmation (WARNING!):



find . -name "*.swp" -delete





share|improve this answer



















  • 1





    Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

    – dhag
    Apr 27 '15 at 20:25











  • Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

    – FloHimself
    Apr 27 '15 at 20:28



















0














If you want to be more restricted, you can use this:



find . -type f -name "*.swp" -exec rm -f {} ;





share|improve this answer
























  • -0 for using exec

    – Michael Durrant
    Apr 27 '15 at 20:37











  • @MichaelDurrant What's wrong with using exec?

    – Erathiel
    Apr 28 '15 at 8:36





















0














Always use the simplest tool for the job. As you want to operate on the current directory only, this command will work fine:



rm -i *.swp






share|improve this answer



















  • 1





    This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

    – a CVn
    Apr 28 '15 at 9:06











  • The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

    – dr01
    Apr 28 '15 at 9:11











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%2f198971%2fdelete-all-swp-file-using-rm-command%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














The error message you received probably indicates that no file matched the name pattern .swp$. A generally safer way to do what you wrote (because it will handle any file name):



find . -name '*.swp' -print0 | xargs -0 rm -i --


In the snippet above, I used -print0 so that find separates
records with null characters; the default is newlines, which
unfortunately is valid within a filename. With -print0, the command
works works with any filenames, including any that embed
newlines. Likewise, xargs -0 processes its input as null-separated
records.






share|improve this answer


























  • What is the -print0 flag?

    – T.Woody
    Jan 14 at 17:57











  • Thanks! I will be removing my comments then ;)

    – T.Woody
    Jan 14 at 22:39
















7














The error message you received probably indicates that no file matched the name pattern .swp$. A generally safer way to do what you wrote (because it will handle any file name):



find . -name '*.swp' -print0 | xargs -0 rm -i --


In the snippet above, I used -print0 so that find separates
records with null characters; the default is newlines, which
unfortunately is valid within a filename. With -print0, the command
works works with any filenames, including any that embed
newlines. Likewise, xargs -0 processes its input as null-separated
records.






share|improve this answer


























  • What is the -print0 flag?

    – T.Woody
    Jan 14 at 17:57











  • Thanks! I will be removing my comments then ;)

    – T.Woody
    Jan 14 at 22:39














7












7








7







The error message you received probably indicates that no file matched the name pattern .swp$. A generally safer way to do what you wrote (because it will handle any file name):



find . -name '*.swp' -print0 | xargs -0 rm -i --


In the snippet above, I used -print0 so that find separates
records with null characters; the default is newlines, which
unfortunately is valid within a filename. With -print0, the command
works works with any filenames, including any that embed
newlines. Likewise, xargs -0 processes its input as null-separated
records.






share|improve this answer















The error message you received probably indicates that no file matched the name pattern .swp$. A generally safer way to do what you wrote (because it will handle any file name):



find . -name '*.swp' -print0 | xargs -0 rm -i --


In the snippet above, I used -print0 so that find separates
records with null characters; the default is newlines, which
unfortunately is valid within a filename. With -print0, the command
works works with any filenames, including any that embed
newlines. Likewise, xargs -0 processes its input as null-separated
records.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 14 at 20:09

























answered Apr 27 '15 at 20:07









dhagdhag

11.3k33045




11.3k33045













  • What is the -print0 flag?

    – T.Woody
    Jan 14 at 17:57











  • Thanks! I will be removing my comments then ;)

    – T.Woody
    Jan 14 at 22:39



















  • What is the -print0 flag?

    – T.Woody
    Jan 14 at 17:57











  • Thanks! I will be removing my comments then ;)

    – T.Woody
    Jan 14 at 22:39

















What is the -print0 flag?

– T.Woody
Jan 14 at 17:57





What is the -print0 flag?

– T.Woody
Jan 14 at 17:57













Thanks! I will be removing my comments then ;)

– T.Woody
Jan 14 at 22:39





Thanks! I will be removing my comments then ;)

– T.Woody
Jan 14 at 22:39













5














Or a variation with find alone e.g.:



find . -name "*.swp" -ok rm {} +


or just without confirmation (WARNING!):



find . -name "*.swp" -delete





share|improve this answer



















  • 1





    Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

    – dhag
    Apr 27 '15 at 20:25











  • Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

    – FloHimself
    Apr 27 '15 at 20:28
















5














Or a variation with find alone e.g.:



find . -name "*.swp" -ok rm {} +


or just without confirmation (WARNING!):



find . -name "*.swp" -delete





share|improve this answer



















  • 1





    Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

    – dhag
    Apr 27 '15 at 20:25











  • Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

    – FloHimself
    Apr 27 '15 at 20:28














5












5








5







Or a variation with find alone e.g.:



find . -name "*.swp" -ok rm {} +


or just without confirmation (WARNING!):



find . -name "*.swp" -delete





share|improve this answer













Or a variation with find alone e.g.:



find . -name "*.swp" -ok rm {} +


or just without confirmation (WARNING!):



find . -name "*.swp" -delete






share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 27 '15 at 20:18









FloHimselfFloHimself

6,36421318




6,36421318








  • 1





    Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

    – dhag
    Apr 27 '15 at 20:25











  • Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

    – FloHimself
    Apr 27 '15 at 20:28














  • 1





    Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

    – dhag
    Apr 27 '15 at 20:25











  • Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

    – FloHimself
    Apr 27 '15 at 20:28








1




1





Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

– dhag
Apr 27 '15 at 20:25





Hah, I was thinking of posting the same, but then figured I would wait for comments, if any :). Note that POSIX find doesn't support closing a -ok command with +; I would write -ok rm -- {} ; instead.

– dhag
Apr 27 '15 at 20:25













Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

– FloHimself
Apr 27 '15 at 20:28





Thanks for the POSIX hint! I'm on OSX atm so I expected other limitations anyway. :)

– FloHimself
Apr 27 '15 at 20:28











0














If you want to be more restricted, you can use this:



find . -type f -name "*.swp" -exec rm -f {} ;





share|improve this answer
























  • -0 for using exec

    – Michael Durrant
    Apr 27 '15 at 20:37











  • @MichaelDurrant What's wrong with using exec?

    – Erathiel
    Apr 28 '15 at 8:36


















0














If you want to be more restricted, you can use this:



find . -type f -name "*.swp" -exec rm -f {} ;





share|improve this answer
























  • -0 for using exec

    – Michael Durrant
    Apr 27 '15 at 20:37











  • @MichaelDurrant What's wrong with using exec?

    – Erathiel
    Apr 28 '15 at 8:36
















0












0








0







If you want to be more restricted, you can use this:



find . -type f -name "*.swp" -exec rm -f {} ;





share|improve this answer













If you want to be more restricted, you can use this:



find . -type f -name "*.swp" -exec rm -f {} ;






share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 27 '15 at 20:27









canonalecanonale

1




1













  • -0 for using exec

    – Michael Durrant
    Apr 27 '15 at 20:37











  • @MichaelDurrant What's wrong with using exec?

    – Erathiel
    Apr 28 '15 at 8:36





















  • -0 for using exec

    – Michael Durrant
    Apr 27 '15 at 20:37











  • @MichaelDurrant What's wrong with using exec?

    – Erathiel
    Apr 28 '15 at 8:36



















-0 for using exec

– Michael Durrant
Apr 27 '15 at 20:37





-0 for using exec

– Michael Durrant
Apr 27 '15 at 20:37













@MichaelDurrant What's wrong with using exec?

– Erathiel
Apr 28 '15 at 8:36







@MichaelDurrant What's wrong with using exec?

– Erathiel
Apr 28 '15 at 8:36













0














Always use the simplest tool for the job. As you want to operate on the current directory only, this command will work fine:



rm -i *.swp






share|improve this answer



















  • 1





    This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

    – a CVn
    Apr 28 '15 at 9:06











  • The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

    – dr01
    Apr 28 '15 at 9:11
















0














Always use the simplest tool for the job. As you want to operate on the current directory only, this command will work fine:



rm -i *.swp






share|improve this answer



















  • 1





    This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

    – a CVn
    Apr 28 '15 at 9:06











  • The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

    – dr01
    Apr 28 '15 at 9:11














0












0








0







Always use the simplest tool for the job. As you want to operate on the current directory only, this command will work fine:



rm -i *.swp






share|improve this answer













Always use the simplest tool for the job. As you want to operate on the current directory only, this command will work fine:



rm -i *.swp







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 28 '15 at 8:32









dr01dr01

16k114971




16k114971








  • 1





    This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

    – a CVn
    Apr 28 '15 at 9:06











  • The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

    – dr01
    Apr 28 '15 at 9:11














  • 1





    This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

    – a CVn
    Apr 28 '15 at 9:06











  • The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

    – dr01
    Apr 28 '15 at 9:11








1




1





This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

– a CVn
Apr 28 '15 at 9:06





This command is different from what the OP is doing, since find by default will recurse into subdirectories to find matching files.

– a CVn
Apr 28 '15 at 9:06













The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

– dr01
Apr 28 '15 at 9:11





The OP wrote, verbatim: "I am trying to find and delete all .swp files in my current directory." He didn't specify that he wants to delete files in subdirectories also. As per OP's specifications, the rm command I posted is the best choice.

– dr01
Apr 28 '15 at 9:11


















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%2f198971%2fdelete-all-swp-file-using-rm-command%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?

第一次世界大戦

Touch on Surface Book