Linux – Zip up a folder, but only include folders with numerical names

command-line-interfacecompressionlinux

I have a very messy folder which contains thousands of files and directories. I would like to create a Zip file which only contains some of the folders (and files within those folders) – specifically those folders which have number-only names (104, 2342, etc…).

I can't figure out how to do this though – any suggestions?

I'm on Linux – CentOS to be exact 🙂

Best Answer

Does this work for you:

 zip -ry archive.zip [0-9]*

Run on a directory containing this:

001  002  100  200  aaa  bbb

it creates a zip archive of only the numbered directories:

$ unzip -l archive.zip 
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2010-10-19 22:27   001/
        0  2010-10-19 22:27   002/
        0  2010-10-19 22:41   100/
        0  2010-10-19 22:41   100/10000/
        0  2010-10-19 22:41   100/10000/a.txt
        0  2010-10-19 22:41   100/10000/10000.txt
        0  2010-10-19 22:27   200/
---------                     -------
        0                     7 files

That is only numbered files/dirs at the top level get put in the zip archive.

UPDATE To exclude files from the top level:

find . -maxdepth 1 -type f > excluded.files

review 'excluded.files' carefully. Then:

zip -ry test.zip [0-9]* -x@excluded.files