-1

Can you please check this command? It's deleting the files and the current directory itself. Script is run via cron.

Directories:

/log/dir/
/log/dir/tmp/

Commands in script:

find /log/dir/ -mtime +7 -name "*" -exec rm -rf {} \;
find /log/dir/tmp -mtime +7 -name "*" -exec rm -rf {} \;

Testing: Run the find /log/dir/ -mtime +7 -name "*" to check the files and I got result. After running the script, the file was deleted. After some days, the folders got deleted as well (the dir in /log/dir/ and the tmp in /log/dir/tmp).

Expectation should be only the files/directories INSIDE the path provided older than 7 days will be deleted.

Linux redhat6.5

  • add '-mindepth 1' and remove -name "*" – MetNP Feb 27 '18 at 3:26
  • find /log/dir/ -mtime +7 -mindepth 1 -exec rm -rf {} \; --- like this? – chelseagirl01 Feb 27 '18 at 6:55
  • yes... '-mindepth 1' ensures not-deleting main directory, not mentioned bellow, but '-type f' will also disallow it well. – MetNP Mar 1 '18 at 21:23
2

If you want to use find to delete only regular files and no directories, then make absolutely sure that you use -type f. This will prevent pathnames that refer to directories (or anything that is not a regular file) to be processed. The directories got deleted since their last modified timestamp fulfilled the criteria for deletion.

Also, if you are deleting files, don't use rm -r since recursing into a regular file does not make sense. With most find implementations you can also choose to use -delete rather than -exec rm.

The -name "*" is a no-op and could be removed since every name matches *.

If you run find over /log/dir, then you don't need to run it on /log/dir/tmp as the latter will be processed as part of the former. If you intend to only look in these two directories, but not in subdirectories, add -maxdepth 1 to find (if it supports it), and in that case, you do need to process them separately (but this may be done in one find invocation as the utility takes any number of top-directories).

Suggestion, depending on what it is you want to achieve:

find /log/dir /log/dir/tmp -maxdepth 1 -type f -mtime +7 -delete

The mtime of a directory is updated when a file is added or deleted from the directory. It is therefore unclear whether your attempts to delete directories based on the last modified timestamp is a good idea to start with.


For rotation of log files, you may also look into using logrotate or some similar utility, from a cron job.

  • Yes, it turned out that it deleted the directory itself because it fits the criteria of being older than 7days. Let's say the two directories are different: /log/dir and /otherdir/tmp Can I still include it in one line such as find /log/dir /otherdir/tmp -maxdepth 1 -type f -mtime +7 -delete Will that be okay? – chelseagirl01 Feb 28 '18 at 1:20
  • @chelseagirl01 That command will look for regular files that have a modification timestamp of more than a week ago and delete them. It will look inside the two directories /log/dir and /otherdir/tmp, but not in their subdirectories. If that's what you want, then it will be okay. – Kusalananda Feb 28 '18 at 6:34
0

You need to define what you mean by 'older than 7 days'. Could be accessed more than 7 days ago, changed more than 7 days ago or created more than 7 days ago. All different situations. Also are you sure you want to consider files and directories?

If you are looking to delete files and directories, then the 2nd command is redundant, since /log/dir/tmp would be listed under /log/dir.

Using -r on rm is careless in this situation and will probably delete more than what you want to. Find is already listing all files/directories recursively so you are jumping ahead of find's output by deleting recursively. Stick to rm -f and let find list the files to delete.

What I think you are trying to do is simply: find /log/dir/ -type f -ctime +7 -exec rm -f {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.