Find the answer to your Linux question:
Results 1 to 5 of 5
Hello. I need to write a script to delete files older than 30 days in the tmp directory of server. would the following work ? /tmp -mtime +30 -type f ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4

    Script to delete files


    Hello.

    I need to write a script to delete files older than 30 days in the tmp directory of server.

    would the following work ?

    /tmp -mtime +30 -type f -exec rm {} \;

    thanks in advance
    Pete

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Quote Originally Posted by peterepka View Post
    Hello.

    I need to write a script to delete files older than 30 days in the tmp directory of server.

    would the following work ?

    /tmp -mtime +30 -type f -exec rm {} \;

    thanks in advance
    Pete
    Yes, pretty much. Some suggestions:

    1. I think you intended to have find in front of your command
    2. If the command is run by an unprivileged user (not root), it will generate errors for any files the user does not own and they won't be removed
    3. You might want to use the -f option of rm to override read-only files
    4. If you anticipate the command removing a lot of files at any one time, you might consider making use of combining find and xargs to spawn off fewer processes

  3. #3
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4
    Hi

    thanks for the reply.
    1. does the find need to be in front for it to work ? i need to delete everything older than 30 days in the tmp folder.
    2. the command will be scripted so run as root.
    3. thanks for the suggestion of -f after rm.

    so
    /tmp -mtime +30 -type f -exec rm -f {} \;

    ?

  4. #4
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Quote Originally Posted by peterepka View Post
    Hi

    thanks for the reply.
    1. does the find need to be in front for it to work ? i need to delete everything older than 30 days in the tmp folder.
    2. the command will be scripted so run as root.
    3. thanks for the suggestion of -f after rm.

    so
    /tmp -mtime +30 -type f -exec rm -f {} \;

    ?
    find is the command that's driving the removes. They way you've written it, it's going to try to execute /tmp and you'll probably get an error saying you're trying to execute a directory. So it should be:

    Code:
    find /tmp -mtime +30 -type f -exec rm -f {} \;

  5. #5
    Just Joined!
    Join Date
    Jun 2014
    Posts
    4
    Hi - thanks for the clarification.

Posting Permissions

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