I want to delete 1 file at a time from my directory which contains so many files, so I want to remove 1 file at a time. Just to avoid too many reads and failed at too many arguments.

    find ./Backup/ -name '*.csv' -maxdepth 1 -exec rm {} \;
    find: warning: you have specified the -maxdepth option after a non-option argument -name, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

    find: cannot fork: Cannot allocate memory

I don't want to delete recursively from a child directory thats why -maxdepth 1. Any help and suggestion?

  • 1
    It's weird that you're getting that fork error but -delete instead of -exec rm {} \; should solve it. Other than that, just move -maxdepht 1 before -name. Don't forget to test without -delete first. – PSkocik Oct 13 '15 at 12:27
  • The -exec ... '{}' \+ form of find might save on forks, as this emulates xargs in (modern) versions of find. – thrig Oct 13 '15 at 15:48
  • @thrig It saves successive forks, which doesn't really help with a process limit. – Gilles 'SO- stop being evil' Oct 13 '15 at 21:45
  • 1
    Do you have stringent limits on your account? What operating system and distribution is the system running? Are you running a lot of other processes? – Gilles 'SO- stop being evil' Oct 13 '15 at 21:46
  • Yes,Its a production env. There are so many procsses running on a system. – Aashu Oct 14 '15 at 5:07

The cannot fork message might appear when the swap area is full. The tmpfs filesystem (probably) mounted on /tmp might not have the necessary space for the kernel to create new processes.

Check if /tmp has still room using df -h /tmp. If it is full, try to cleanup /tmp first.

  • 1
    Swap and tmpfs are not related except in that they are both subsets of available memory. Room in /tmp is not necessary to create new processes. Room in memory is, but the amount is so small that removing things in /tmp is very unlikely to make a fork possible. – Gilles 'SO- stop being evil' Oct 13 '15 at 21:48
  • /tmp is not even close to full. – Aashu Oct 14 '15 at 5:08
  • 2
    @Gilles, On Solaris /tmp is actual tmpfs and when it is completely (+100%) utilized you will get unable to fork; not enough space messages. The messages the OP get are cannot fork; unable to allocate memory. Apologies that I suggested a Solaris solution and did not ask for the OS first. The OP might check the values of ulimit -a and ulimit -aH to see if there is a memory limit which can be hit during this process. It is also worth trying to look at /etc/security/limits.conf to see if some limits are set. – Lambert Oct 15 '15 at 7:53
  • I have had the same problem with lots of space in /tmp available, but no swap activated. [code]swapon /dev/sdXn[/code] solved the issue. – user43326 Jul 24 '20 at 17:04

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.