I know we have covered finding files, but it occurred to me that sometimes I need to find files that don't match a pattern. For example I have drives full of zipped files that all end in '.gz'. These files are placed there via a process that runs on a schedule. Sometimes the files are not zipped for whatever reason. So I need to find the files that are not zipped. That is all the files that don't have the extension '.gz'. When I find them I want to zip them. Here's how we can use the find command to accomplish this task:
First lets see which ones are not zipped:
find . ! -name '*.gz' -print (this will print out all the files so I can take a look at them)
Now lets find them and zip them
find . ! -name '*.gz' -exec gzip {} \;
The '!' tells find to locate files that DO NOT match the pattern *.gz. Then the exec part calls gzip to zip up the files.
Commands used in this post: find, gzip
No comments:
Post a Comment