0

I am in a situation where I have the /tmp directory having atleast 25,000 - 50,000 directories in it. I am trying to use the following command to delete the directories which are older than 2 days in that directory.

find /path/to/tmp/* -type d -ctime +2 -delete

But I keep running into the error that the argument list is too long. How can I specifically limit the number of directories being deleted? I tried using the maxdepth 1 option as well and that didn't seem to work.

1

You can remove the * as it will return everything and that's what's causing that error to appear.

Linux doesn't keep track of creation times for files or folders although MacOS does.

The ctime +2 in your command will cause it to find and delete directories which have had anything about them changed more than two days ago except for their access times. That includes changes in permissions, ownership, or the directory names having been changed. It won't do anything to the directories based on their creation date/age.

  • If you remove the * you should add -mindepth 1 to get the same behaviour of not deleting the root directory. And Linux does track creation times for some filesystems like ext4 and btrfs, but notably not tmpfs. – Tavian Barnes Apr 17 '18 at 16:01
  • There is no reason to include the * in this case as he is only searching for directories. The filesystem that he's using isn't specified and even if it were, the Linux API doesn't provide a way to get at them by default so he'd need to write a function to do so or use the debugfs command. Either way, in his case, he's not going to be able to use the find command to delete files based on their creation time. – Nasir Riley Apr 17 '18 at 16:20
  • find foo/* ... does not include foo whereas find foo ... does. If you don't want to delete foo then add -mindepth 1. – Tavian Barnes Apr 17 '18 at 16:38
  • I'm not making any claims to the contrary. I'm only stating that * isn't needed and neither is mindepth. It's not a matter of whether it's desired to delete the top directory but of that error occurring. I think it's safe to say that removing the * takes care of that considering that the answer has been accepted. – Nasir Riley Apr 17 '18 at 16:58
1

Unless you need to exclude dot directories, the * is unnecessary there. With the *, the shell is expanding the wildcard to all files and directories in that directory and passing those as arguments to find, causing the argument length problem. Without it, find will recurse in that directory and match the subdirectories by itself.

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.