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?– Sören KuklauCommented 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 ...'.– rinzlerCommented Aug 28, 2011 at 21:40
-
PathFinder a Finder replacement does have this– mmmmmmCommented Aug 28, 2011 at 21:43
-
Why pay $40 if there's a way to do it from Finder for free?– rinzlerCommented Aug 28, 2011 at 22:13
-
1If you already know how to do this from the command line, then an Automator action is definitely the way to go.– NReilinghCommented Aug 28, 2011 at 22:27
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.
-
1I 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 fancytruncate
command, but in most cases:>
does the trick.– bmike ♦Commented 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.
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
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.