简体   繁体   中英

bash- Use find, grep, sed, or a mix of either of these to solve this

I am asked to use grep, sed, find, or a mix of either of these to solve the following (I can also use any other bash commands to solve this as well, but I must use at least one of the three mentioned above).

Lets say I have numerous files in my home directory and sub-directories, and sub-sub-directories, etc that have the word "draft" in its name. For example, program Draft , or draft script.c (can be lower or upper case). I have to delete all files with "draft" in its name, and which are a week or more old.

I already have a solution to this that works, but it's a little bit long :

find . -mtime +7 | egrep '.*draft.*' | xargs rm

I want a more simplified solution but I can't find one. Like, for example, I would like to this using just find, or just grep, etc.

Also, I'm curious if this can be done using find -delete.

For this question though, anything simpler and/or more concise than what I got will serve as an adequate answer.

Thanks

With GNU :

find . -type f -mtime +7 -iname '*draft*' -delete

or

find . -type f -mtime +7 -iname '*draft*' -exec rm {} +

The latter one put as many files possible as argument for each rm commands

  • -iname is like -name but is case-insensitive

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