简体   繁体   中英

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 ?

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.

zip -rm files.zip *.xml

这完美地工作

It remain because nothing told it to be deleted.

sudo rmdir /test <- your directory location.

if it gives you guff that there are still files inside it and you still want it gone add a -rf flag to it. RECLUSIVE, meaning all in the tree below it and FORCE -- just do what I told you to.

sudo rmdir /test -rf

Just double check everything is exactly what you want before you do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM