I want to be able to easily zero-out (make the filesize 0, not fill the existing filesize with 0s) files from the Finder by selecting files from within Finder and selecting an action from the right click menu. I looked at the available Automator actions, but I could only figure out how to delete and recreate files, which would lose any existing attributes. How can I create a contextual menu item to zero out files?

  • Out of curiosity, why would you need this frequently enough to warrant a contextual menu item? Aug 28, 2011 at 21:06
  • Yes, otherwise I wouldn't have asked to make it easier to accomplish :) But hopefully the solution will make it easy to accomplish similar tasks. I see Automator has an action to run a shell script, so maybe I can call 'bash -c ...'.
    – rinzler
    Aug 28, 2011 at 21:40
  • PathFinder a Finder replacement does have this
    – mmmmmm
    Aug 28, 2011 at 21:43
  • Why pay $40 if there's a way to do it from Finder for free?
    – rinzler
    Aug 28, 2011 at 22:13
  • 1
    If you already know how to do this from the command line, then an Automator action is definitely the way to go.
    – NReilingh
    Aug 28, 2011 at 22:27

2 Answers 2

  • Select Run Shell Script
  • Use the default /bin/bash command
  • Select Pass input as arguments
  • Enter the following into the text input box:

    for file in "$@"; do
        cat /dev/null > "$file"

Possible improvements:

  • Display admin prompt if the user account doesn't have permissions to modify the selected files.
  • If a directory is selected, perform the action on all files in the selected directory.
  • 1
    I so love when you can :> a file to truncate a file's contents without disrupting programs with the file already open, changing the file pointers or inode. Unix geeks rejoice when this makes it into an Automator service to let finder feel the power of the shell. There is also a fancy truncate command, but in most cases :> does the trick.
    – bmike
    Aug 28, 2011 at 23:03

Automator is the best way to quickly turn a shell script into a service. Finder services are available as a contextual menu, so I would start there.

enter image description here

Taking a brief bash script like this and turning it into a service is two minor changes in Automator.

for file 
  if [ -f "$file" ]
    then :> "$file"

enter image description here

Change the Pass input: to as arguments instead of stdin and restrict the service to Finder and files/folders. I saved this service as Truncate Files but you can name it as you prefer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .