Find the answer to your Linux question:
Results 1 to 5 of 5
Hi All: I would like to write a small shell script to be added to cronjobs, the main purpose is for the shell script is to delete the outdated files ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    1

    question about find command

    Hi All:
    I would like to write a small shell script to be added to cronjobs, the main purpose is for the shell script is to delete the outdated files and outdated sub-directories under a certain folder, however I do not want to delete the top folder.
    For example: the top folder is /proj/tmp
    the command I wrote was:
    “ find /proj/tmp/ -mtime +10 | xargs rm -rf ”

    The problem is: once there are some files inside satisfying the find condition, "/proj/tmp/" will be returned as well.
    I can't add -type f, because I would like to remove those subfolders as well.

    if I revise the command as the following:
    “ find /proj/tmp/* -mtime +10 | xargs rm -rf ”
    it works as I expected. However, as the top folder is empty, it always report some errors like: "find /proj/tmp/*: no such file or directory". If it's added to cronjob, which will be emailed to root again and again.

    Any suggestions to this problem? Thanks a lot!!!

    btw: I tried with >/dev/null, which didn't help with the error message. Someone suggested tmpwatch, the problem is that I don't know which files or directories returned by tmpwatch, so that I can't test with tmpwatch.

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Error messages go to stderr not stdout. So if you want to ignore error messages the redirection would be 2>/dev/null

  3. #3
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    find /proj/tmp/* -mtime +10 | xargs rm -rf
    Code:
    find /proj/tmp ! -regex ^/proj/tmp$ -mtime +10 -print0 | xargs -0 rm -rf
    -regex is being used here to exclude the root directory itself. -print0 to print the find result in one line and pass it as the parameter of the rm command. good luck.

  4. #4
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97
    use the mindepth option, and eliminate the search in the parent directory....

    hope this helps ?

  5. #5
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    The mindepth approach is definitely more elegant than any other methods mentioned in this thread so far. Thanks for sharing.

Posting Permissions

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