Delete a file immediately and other matters

I want to sometimes delete files immediately. Used to be able to do it with Compost under OSX 10.4.11. It was a nifty little program which installed a menu item so that when I control-clicked and chose the appropriate menu item the file disappeared. That feature is no longer possible under 10.6.8 with Compost.


I did discover, however, that when I command-deleted a file in trash it disappeared immediately, so I've been using that. Silly me. All that was happening was that the file was being sent out of Trash to where it came from. So command-delete is actually a toggle function. Now I've ended up with quite a few files that I've sent out of Trash back to their original location.


I have also found that if I am connected wirelessly to a drive, that command-delete immediately deletes a file, after a warning. Shame that a similar function is not available for local drives.


I asked this question a year or so ago, but with no joy. Thought I'd try again: any simple way in OSX 10.6.8 of deleting a file immediately? Simple meaning no more than a click on the item, then 2-3 key strokes at most.

Posted on Feb 18, 2012 4:51 PM

Reply
9 replies

Feb 19, 2012 2:48 AM in response to X423424X

…And, of course, if an Automator workflow is saved as a service, one can even assign a keyboard shortcut to it.


But, as BobHarris pointed out, one cannot warn enough -- rm cannot be undone.


Here's an AppleScript using rm -- feel free to use it as needed. It works both as a script and as a droplet. It should be fairly safe -- it cannot delete locked items and items for which you do not have permissions -- but it might not work as expected with files or folders with Finder names in CJK and RTL languages.


--script begins

on run

tell application "Finder" to set deleteList to selection


display dialog "You are about to destroy the selected items." & return & return & "This operation CANNOT be undone. OK to proceed?" buttons {"OK", "Cancel"} default button 2


my delFiles(deleteList)

set {x, k} to result

my delResult(x, k)

end run


on opendeleteList


display dialog "You are about to destroy the selected items." & return & return & "This operation CANNOT be undone. OK to proceed?" buttons {"OK", "Cancel"} default button 2


my delFiles(deleteList)

set {x, k} to result

my delResult(x, k)

end open


on delFiles(deleteList)

set x to count of deleteList

set k to x

repeat with i in deleteList

try

do shell script "rm -fR " & quoted form of (POSIX path of (i as alias))

on error

set k to k - 1

end try

end repeat

return {x, k}

end delFiles


on delResult(x, k)

if x = k then

display dialog "All selected items deleted" buttons {"OK"} default button 1

else

display dialog "Deleted " & k & " of " & x & " items selected." & return & return & (x - k) & " items could not be deleted." buttons {"OK"} default button 1

end if

end delResult

--script ends

Feb 26, 2012 12:16 AM in response to Guy Burns

Thanks for the suggestions. I've been playing around over the last few days trying the various ideas, but haven't had any success in simplifying the procedure. I can get Terminal > rm -f to work, and the Applescript to work, but not when I try to use either in Automator/ Service/Run Shell Scripts/Contextual Menus – all of which I don't know much about.


So a few quesitons:


Q1: I have been using "sudo rm -R " in Terminal to remove stubborn files. What is the difference between that command and the "rm -f " mentioned in the various posts?


Q2: What are the steps required to use "rm -f " in Automator? I tried the following, but nothing happened:


1. Opened a workflow in Automator.

2. From Utilities I choose "Run Shell Script" and dragged it into the workflow window.

3. Deleted the "cat" that came up and replaced it with "rm -f ".

4. Selected a file on the desktop.

5. Clicked on Run, and 0.106 seconds later whatever happened was completed, but the file I had selected wasn't removed.


I did a similar thing for the Applescript (from within Automator, Run Applescript and then pasted the script) but Automator always came back with a syntax error.


Should I take this to the Automator or Applescript forums?

Feb 26, 2012 12:54 AM in response to Guy Burns

I worked out why the Applescript within Automator wasn't working: where it said "(* Your script goes here *)", that's where I placed the script; but there were "on run" and "end run" commands before and after. Once I removed those, away it went.


Now, how do I turn that Automator workflow into a Service, and then invoke the Service with a keystroke?

Feb 26, 2012 4:16 AM in response to Guy Burns

Guy Burns wrote:


all of which I don't know much about.

Then perhaps it's a good idea to look into them first; after all, there is no undo for rm, and you want to make sure what's going to be deleted.


For Automator, try this page

<http://www.macosxautomation.com/automator/index.html>


For AppleScript, the manual is here

<http://manuals.info.apple.com/en_US/AppleScriptLanguageGuide.pdf>


For Terminal, try this

<http://guides.macrumors.com/Terminal>

What is the difference between that command and the "rm -f " mentioned in the various posts?

Most programmes you use in Terminal, if properly installed, have a basic manual (manpage in Unixese). So, in Terminal, the command


man rm


will give you a description of what the rm does, its syntax, and available options. In this instance, -f means 'force', ie, delete the file without any prompting, and -R mean 'recursive', ie, if the target is a directory, remove it and everything in it. As to sudo, again, man sudo will give you the gory details.

What are the steps required to use "rm -f " in Automator?

You need to have another action first, to pass on the selected files to rm.

how do I turn that Automator workflow into a Service

You do that before building your workflow, by choosing the Service template.

then invoke the Service with a keystroke?

System Preferences > Keyboard > Keyboard Shortcuts > Services


However, I warn you to think long and hard before you do this. Remember, rm cannot be undone -- choose the wrong file and use the shortcut, and it's gone forever. I wouldn't do that on my Mac; ⌘⌫ and ⇧⌘⌫ are good enough for me.

Feb 26, 2012 7:00 AM in response to Guy Burns

Q1: I have been using "sudo rm -R " in Terminal to remove stubborn files. What is the difference between that command and the "rm -f " mentioned in the various posts?

When using the 'rm' command think of it as you would when using matches. We are taught at a very young age to be careful with matches and fire. No one teaches you at a young age about the 'rm' command.


rm -f says as long as you have write access to the parent directory AND you either have permissions to delete the fle or you own the file being deleted, the 'rm' command will not annoy you with trivial little things like, do you really want to do this. Also the -f will not cause rm to issue any error messages if for example the file you specify does not exist.


rm -R is a bit more dangerous. More like using a blowtorch, in that if you specify a directory to be deleted, it will walk down that directory tree and delete everything it has permission to delete. So you want to be very careful where you point that blowtorch. For example if you were to point at your home directory you would be very unhappy to loose everything you care about.


sudo rm -R is more along the lines of a Flamethrower! By default, sudo gives you absolute permissions to delete anything anywhere on your system. If you were have a 'typo' or point at a system level directory, you would totally hose your entire system. All your work gone, unless you have a recent full backup. sudo rm -R (or sudo rm -r) is one of the most dangerous command in the Unix environment.


And putting rm into a script that blindly does what it is told, just adds a Nuclear option to the 'rm' command. By blindly, I mean a script that does not sanity check what is passed as arguments, by making an attempt at refusing to delete stuff it shouldn't.

Feb 27, 2012 1:10 AM in response to BobHarris

sudo rm -R is more along the lines of a Flamethrower!

Well, looks like I've been using a Flamethrower for the past ten years, ever since I came across the command in MacWorld. What would happen if I used it on the System folder? Would the screen suddenly go black?


For fane_j

Got your script working as a service with a keyboard shortcut, with one minor problem. When I am asked:


"You are about to destroy the selected items."


the window is not active i.e. neither of the buttons is blue. When I click on OK, the item is deleted. It just looks strange not having one of the buttons appear as selectable. How do I make that window active within Automator/Applescript or wherever, so that the default button is blue?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Delete a file immediately and other matters

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.