How to Delete matching files in all subdirectories

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.

  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

How To: Back Up MySQL Databases From The Command Line

While automated backups are important, sometimes you just want to take a quick and dirty snapshot...

How To Install MariaDB on CentOS 6

MariaDB is a drop-in replacement for MySQL. It is easy to install, offers many speed and...

How to Display (List) All Jobs in Cron / Crontab

View Root’s Cron Jobs crontab -l  View a User’s Cron Jobs crontab -u username -l Example with...

How To: Automate Server Scripts With Cron

Servers can automatically perform tasks that you would otherwise have to perform yourself, such...

How to find sending spam emails and enable mail header in php.ini

- In Linux servers if there are a lot of emails in the queue "over 100" emails. you can check if...