Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
Out of curiosity, why would you need this frequently enough to warrant a contextual menu item? –  Sören Kuklau Aug 28 '11 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 '11 at 21:40
    
PathFinder a Finder replacement does have this –  Mark Aug 28 '11 at 21:43
    
Why pay $40 if there's a way to do it from Finder for free? –  rinzler Aug 28 '11 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 '11 at 22:27
add comment

2 Answers

  • 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"
    done
    

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.
share|improve this answer
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 '11 at 23:03
add comment

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 
do
  if [ -f "$file" ]
    then :> "$file"
  fi
done

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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