How to remove files without a certain extension?

0 min read 94 words

You have a directory with a lot of files, and you want to delete only the files that don’t match a specific extension.

This is a simple task when using the find command-line utility.

Delete a single extension

You can delete all files that do not have the following *.txt extension:

find . -type f ! -name "*.txt" -exec rm {} \;

Delete multiple extensions

You can delete all files that do not have the following *.txt or *.exe extensions.

find . -type f ! -name "*.exe" ! -name "*.txt" -exec rm {} \;
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts