133

How can I remove all .swp files in all of my subdirectories under Linux?

1
  • 3
    Don't forget to look at programs like trash-cli so you can undelete, instead of using rm. code.google.com/p/trash-cli (You'd just replace 'rm' with 'trash' in all of the below answers.)
    – Roger Pate
    Feb 22, 2010 at 15:10

5 Answers 5

220

Remove all *.swp files underneath the current directory, use the find command in one of the following forms:

  • find . -name \*.swp -type f -delete

    The -delete option means find will directly delete the matching files. This is the best match to OP's actual question.

    Using -type f means find will only process files.

  • find . -name \*.swp -type f -exec rm -f {} \;
    find . -name \*.swp -type f -exec rm -f {} +

    Option -exec allows find to execute an arbitrary command per file. The first variant will run the command once per file, and the second will run as few commands as possible by replacing {} with as many parameters as possible.

  • find . -name \*.swp -type f -print0 | xargs -0 rm -f

    Piping the output to xargs is used form more complex per-file commands than is possible with -exec. The option -print0 tells find to separate matches with ASCII NULL instead of a newline, and -0 tells xargs to expect NULL-separated input. This makes the pipe construct safe for filenames containing whitespace.

See man find for more details and examples.

2
  • I'd wrap the name in quotes rather than escape special characters; it feels safer. '*.swp'.
    – Cai
    May 15, 2023 at 11:18
  • I used this script anyhow, but my use case is somewhat different. I want to delete all the files from sub-directories, whereas, the file must be in the "migrations" directory.
    – Khaksar
    Oct 27, 2023 at 7:45
26

find . -name '*.swp' -delete

Having find do the delete itself remove any risk for space embedded in filename, ... For extra security also consider adding -type f for files only.

8
find /path -type f -name "*.swp" -delete
find /path -type f -name "*.swp" -exec rm -f "{}" +;

bash 4.0

shopt -s globstar
rm -f /path/**/*.swp
2

For searching under my home directory (and using the GNU 'find' and 'xargs'), I'd use:

find $HOME -name '*.swp' -print0 | xargs -0 rm -f

The use of '-print0' and '-0' means that the names will be delimited by ASCII NUL '\0' characters, and this will handle file paths with blanks etc in the names. If you think you might have directories (or device files, or FIFOs, or other non-files) under your directory ending with '.swp', you could add the '-type f' option to 'find'. If you only have directories, the command above will fail to remove them noisily.

0
-2

If you would like to delete all the files from all the subfolders, you can use the command provided,

$ find . -name \* -type f -delete

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .