Vinod R M Vinod R M - 9 months ago 33
Linux Question

Remove all but few specific pattern of files in Linux

I have the following requirement.


=> Shell snippet

% ls

c.log.1 c.log.11 c.log.13 c.log.15 c.log.17 c.log.19 c.log.20 c.log.22 c.log.24 c.log.26 c.log.28 c.log.3 c.log.31 c.log.4 c.log.6 c.log.8 c.log.10 c.log.12 c.log.14 c.log.16 c.log.18 c.log.2 c.log.21 c.log.23 c.log.25 c.log.27 c.log.29 c.log.30 c.log.32 c.log.5 c.log.7 c.log.9

I need to delete all files except say, For ex: c.log.7 or any such specific file.

Kindly provide the solution.


ls -1 |grep -w -v 'c.log.7'|xargs rm -rf