Find the answer to your Linux question:
Results 1 to 6 of 6
Hi All, I'm hoping someone is able to give me a hand with creating a bash script that can loop over the users directories, search each sub-dir, and delete ( ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    3

    Bash Help


    Hi All,
    I'm hoping someone is able to give me a hand with creating a bash script that can loop over the users directories, search each sub-dir, and delete ( or archive? ) any '.log' files that are older than 30 days....
    This is more complicated than I can handle, and of course I don't want to screw it up and delete everything.

    Additional bonus would be if I could also have the same script purge special directories if they are found. ( eg a temp folder )

    Thanks for you help!
    Chumly

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    hi,


    cant give a complete script(am in a hurry) but i can give you guidelines. if you have trouble update us and we will try to help you.

    you can proceed as follows
    1) you read the file /etc/passwd with the awk language and get the users' home directories
    2) use the find command to get the files you looking for(find will get them exactly the way you want)
    3) delete them


    for the bonus are you talking about directories under /tmp ?
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    3
    I was thinking something like a loop over the /home sub folders. No need to check passwd.
    EG:
    $BASEDIR="/home"
    cd $BASEDIR
    for CHECKDIR in $BASEDIR; do
    for FOLDERNAME in $CHECKDIR/*
    do
    ...
    Something here to search the sub directories and remove files ending in '.log' that are older than 30 days
    ...
    Something else here that wipes any folder that matches the names that are in some other var. ( if $FOLDERNAME=$SOMEOTHERVAR )
    ...
    done
    done

    Thanks
    Chumly

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Quote Originally Posted by chumly View Post
    I was thinking something like a loop over the /home sub folders. No need to check passwd.
    if you are sure there is nothing and will never be under /home but user directories you can do that.

    EG:
    $BASEDIR="/home"
    cd $BASEDIR
    for CHECKDIR in $BASEDIR; do
    for FOLDERNAME in $CHECKDIR/*
    do
    you might consider using the find command here as traversing subdirectories with for loop will be heck of a mission(if you wanna go up to the leaves).
    ...
    Something here to search the sub directories and remove files ending in '.log' that are older than 30 days
    find does it quite easily
    Linux and me it's a love story

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    3
    Something like
    /usr/bin/find $BASEDIR -name "*.log" -mtime +30 | xargs rm
    ?

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    Indeed

    you may want to use the "-daystart" option as well.
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •