Gnuru.org
Productive Linux


Subscribe

 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Login
Login:
Password:



Don't have an account?
Sign up to Gnuru.org
Forgot your password?

Remove All Files Except Some - Command Line
24 August 2010 @ 11:14 BST
by Paul

Here's a little command to remove all files in a directory except those which match pattern

ls * | grep -v pattern | xargs rm

This code works fine unless your file names have spaces in them, in which case this works:

ls -b * | grep -v pattern | xargs rm

The -b switch to the ls command escapes the spaces which enables xargs to handle them. There might be an edge-case problem if your pattern contains backslashes. Excepting that, this does the job for me.





Comments disabled