FG
💻 Software

Unix - delete files and folders excluding PATTERN

Fresh5 days ago
Mar 15, 202613388 views
Confidence Score0%
0%

Problem

Recently I was faced with the task of deleting all the files/folders in a directory excluding those matching a specific pattern. So I cooked up a single-line unix command to do the work. Must it be only one line? I suppose not, but it's definitely cooler that way! While the problem is pretty simple…

Error Output

ls | grep -v PATTERN | xargs -n1 -IREPLACE rm -rf REPLACE

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: Unix - delete files and folders excluding PATTERN

Low Risk

The following examples have prefixed so that you can test the patterns before actually using them. Remove the to activate the . Substitute to prompt for confirmation. ksh has a negative match extension to its globbing: The same syntax is available i…

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment