• Home
  • Health
  • Software
  • Blog
  • mac

    Speed Up rm files on Mac

    Using rm -rf is slow on Mac, it's much faster on Linux. Something about Mac's filesystem, the journaling / metadata or something, I don't really know.


    I made a script to autodelete things if I move them to a certain directory, and made a zsh function to move them:

    # .zprofile
    
    # usage: erm node_modules
    erm() {
        mv -- "$1" "/opt/autoTrash/joe/"
    }
    


    I made a place for the script and the files:

    sudo mkdir /opt/autoTrash
    sudo mkdir /opt/autoTrash/joe
    


    I put this in /opt/autoTrash/autoDelete.sh:

    #!/bin/zsh
    
    while true; do
      echo "Running cleanup..."
      find /opt/autoTrash/joe -mindepth 1 -delete
      echo "Cleanup completed."
    
      echo "Waiting for 30 seconds..."
      sleep 30
    done
    


    I created a plist file for it so it would run on boot:

    sudo mkdir -p /Library/LaunchDaemons
    nano /Library/LaunchDaemons/autoTrash.plist
    


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
       <key>Label</key>
       <string>com.user.autoTrash</string>
       <key>ProgramArguments</key>
       <array>
          <string>/opt/autoTrash/autoDelete.sh</string>
       </array>
       <key>RunAtLoad</key>
       <true/>
    </dict>
    </plist>
    

    And I made sure the permissions were correct:

    # ls -al /Library/LaunchDaemons
    -rw-r--r--   1 root  wheel   388 Jun 20 16:29 autoTrash.plist
    
    # ls -al /opt/autoTrash
    total 16
    drwxr-xr-x  7 root           wheel   224 Jun 20 20:06 .
    drwxr-xr-x  5 root           wheel   160 Jun 20 20:56 ..
    -rwxr--r--  1 root           wheel   182 Jun 20 20:28 autoDelete.sh
    drwxr-xr-x  2 joe            staff    64 Jun 20 20:28 joe
    -rw-r--r--  1 root           wheel  2240 Jun 20 20:44 startup.log
    


    Debugging steps used:

    # loading/unloading plist daemon manually:
    sudo launchctl unload /Library/LaunchDaemons/autoTrash.plist
    sudo launchctl load /Library/LaunchDaemons/autoTrash.plist
    
    # linting plist to see if it's valid:
    plutil -lint /Library/LaunchDaemons/autoTrash.plist
    
    # Inside the autoDelete.sh script, temporarily, to see if it was running
    exec &> /opt/autoTrash/startup.log
    
    # Checking if it's still running:
    ps -ef | grep autoTrash