Search code examples
linuxfilezip

Trying to zip and automatically delete files with "find" and "zip -m" on Linux


there are over 300000 files in the /test folder with a long name (over 30 character )and same header like this "TEST_*" i want to zip all the files into a .zip package and remove them from the /test file. so i used the command show as below:

find ./test -name "TEST_\*" -mtime +1 | zip -m /home/TESTbac.zip -@;

but the files in the /test folder still exist after i run the shell script

what i want to ask is why the files still exist after running the script ? and how can i fix this problem ?


Solution

  • You don't need to pipe the results to the zip command. find has a -exec parameter that will execute the given command for each matching path. I suspect something in the piping process is causing the -m to not work as expected.

    Instead try this:

    find ./test -name "TEST_*" -exec zip -m /home/TESTbac.zip '{}' ';'
    

    Note: The quoted semicolon denotes the end of the -exec command. It's quoted so the command line can differentiate between the ending of the -exec command vs the ending of the entire command itself. Meanwhile the {} are automatic replaced by find with the matching path results.