-4

I have a situation where I have to clean up files that are older than 7 days, but from multiple locations like below.

/home/user1/Out/vendor
/home/user2/Out/vendor
/home/user3/Out/vendor
/home/user4/Out/vendor
        ︙
/home/usern/Out/vendor

Can someone tell me how to achieve this without deleting folders? I am thinking to call there users from a text file.

1 Answers1

2

The technique described in Delete files older than X days + will work for you:

find /path/to/directory/ -mtime +7 -delete

but it leaves out a couple of pieces of your question.

  1. ... from multiple locations ...

    You can specify multiple starting points for a single find command.  Do

    find /home/user1/Out/vendor /home/user2/Out/vendor /home/user3/Out/vendor /home/user4/Out/vendor … /home/usern/Out/vendor -mtime +7 -delete
    or
    find /home/user1/Out/vendor /home/user2/Out/vendor /home/user3/Out/vendor \
         /home/user4/Out/vendor … /home/usern/Out/vendor -mtime +7 -delete
    for readability.

    But you don't have to explicitly list all the directories like that.  If you're literally dealing with user1, user2, ..., usern, (where n is a number), you can do

    find /home/user{1,2,3,4,…,n}/Out/vendor -mtime +7 -delete
    or even
    find /home/user{1..n}/Out/vendor -mtime +7 -delete
    (using literally two dots (.), a.k.a. period or full stop) in bash.

    But I guess you probably aren't literally dealing with user1, user2, ..., usern; your users probably have names.  But the same principle applies; you can do

    find /home/{user1,user2,user3,user4,…,usern}/Out/vendor -mtime +7 -delete
    For example,
    find /home/{alice,bob,cathy,david,…,nathan}/Out/vendor -mtime +7 -delete
    or even
    find ~{alice,bob,cathy,david,…,nathan}/Out/vendor -mtime +7 -delete
  2. … how to achieve this without deleting folders?

    Just add -type f to the command:

    find /home/{user1,user2,user3,user4,…,usern}/Out/vendor -type f -mtime +7 -delete