DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Forums
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OS X | UNIX

back

Unix

Mac OS X How To

How To Fix Stubborn Trash and Why it Won't Delete (page 2 of 2)

The Command Line Approach

If you are not comfortable using the Terminal to issue Unix commands, read the Mac OS X Unix Tutorial .

The commands I give here will look something like:

% cd ~

Don't type the '%', just the command that follows it, then hit 'return'. The character after 'cd' in the tilde character.

Fire up the Terminal in Applications/Utilities and put on your Unix hat.

Background Information - Trash Theory

This section gives technical details of how the Mac OS X Trash works. However, it is not necessary to understand such details in order to proceed to the next section - 'Practical Advice - Trash Destruction' - which states the command to actually empty the trash.

Trash is held in a directory called '.Trash' within your home directory. You can view its contents with:

% cd ~
% ls -al .Trash

You should see listed the same items you see when opening the trash icon from the Dock.

Examine the '.Trash' directory itself:

% ls -ald .Trash
drwx------ 3 melkor staff 58 Jun 25 12:08 .Trash

You will see that the permissions for '.Trash' are read and write for yourself (I am user 'melkor'), and no permissions for others. Having write permission on a directory means you can change it - i.e. create and remove files within that directory. The key point here is that having write access to a directory allows one to delete files within that directory no matter what the permissions on that file.

There may be a file owned by 'root', with no permissions for anyone, in your home directory. You can delete it. Yes, you can. You can't view it or change it, but you can delete it. This may seem odd, but the reason is that you are not changing the file itself, only the directory. You are removing the directory entry for that file, and so the file vanishes and the disk space it occupied becomes free. But you are only changing only the directory, not the file.

For example:

 
Tell Me More...

No Trash?

If your trash is empty, then you will not have directory '.Trash' in your home directory (OS X 10.1). Trash something using the Finder to create it.

ls -al

Unix command 'ls' lists the contents of the current directory. Option '-a' says to list all files in the directory including the hidden dot files such as '.Trash'

Option '-l' says to display a long listing - list file details such as owners, permissions, and modification date as well as the filename itself.

ls -ald

Unix command 'ls -ald' makes use of option '-d', which says to list the details of a directory instead of the contents.

Immutable Files

The Finder (both OS X and OS 9) shows an immutable file as a locked file. Locking and un-locking are equivalent setting and clearing the Unix immutable flag.


terminal


See! I can override the write protection on 'xxx' because I can write to my home directory.

Based on this theory, you should never have a problem emptying the trash. But you do huh? Well, there are four possible scenarios.

1 - Nested Directories You Don't Own

This thing about being able to delete any file - it only applies if you have write permission to the directory containing the file. So, if a file resides within a non-writeable directory within the trash, you can't delete it. And you can't delete the directory either because it's not empty.

2 - Immutable Files

Files can be made immutable. An immutable file has the Immutable flag set - one of the file flags that exist over and above the attributes (permissions and owners) you see when you list files. A special command called 'chflags' is used to set and unset flags.

3 - Open Files

If a file is currently in use by some application (open) then it cannot be deleted regardless of your permissions for that file.

4 - The nightmare Scenario

A file may have the System Immutable flag set. This means that you cannot change it in any way; no user can change it. Root cannot change it. And get this - root can't even unset the flag. So even root can't delete it. (But it can be deleted.....)


Practical Advice - Trash Destruction

Here are four solutions to the four scenarios listed above.

1 - Nested Directories You Don't Own

One solution is to apply write-permission to all files in the trash. Bit if you do not own some of the files you have to run as root. And if you run as root, you may as well simply delete the lot. So here goes.

% sudo rm -ri ~/.Trash

This will zap all files except those that are immutable or in use.

'sudo' executes the command as root and therefore overrides all permissions
'rm' removes files
'-r' does it recursively - ie to all nested files and directories
'-i' is confirm mode - you will be prompted for each file to delete, and I suggest to use this as a safety precaution. If you have many, many files tedium may set in, so you can replace the '-i' with '-f' (force).

2 - Immutable files

You can check which, if any, files have the immutable flag set by specifying the '-o' option to 'ls'.

ls -alo
drwx------ 15 saruman staff - 510 Jan 6 23:35 osxfaq
-rw-r--r-- 1 saruman staff uchg 0 Feb 20 14:04 user-imutable-set

'uchg' indicates that the User Immutable Flag is set. 'schg' indicates that the System Immutable Flag is set (see point 4 below).

Clear the immutable flag of all files, then delete:

% sudo chflags -R nouchg ~/.Trash
% sudo rm -ri ~/.Trash

'chflags' changes a file's flags settings.
'nouchg' clears the user immutable flag ('uchg' would set it)
'-R' does it recursively.

3 - Open Files

You can determine which files in the trash are open, and which application has them open, with the 'fstat' command:

% ls -al ~/.Trash
total 0
drwx------ 5 melkor staff 126 Jun 25 16:43 .
drwxr-xr-x 24 melkor staff 772 Jun 25 16:32 ..
-rw-r--r-- 1 melkor staff 0 Jun 25 16:32 xxx
-rw-r--r-- 1 melkor staff 0 Jun 25 16:43 yyy
-rw-r--r-- 1 melkor staff 0 Jun 25 16:43 zzz

% fstat ~/.Trash/*
USER .CMD PID FD INUM MODE SZ|DV R/W MOUNT NAME
melkor vi 731 3 368860 -rw-r--r-- 0 r / /Users/melkor/.Trash/xxx

...that's the one - 'vi' is editing 'xxx'...

Close the offending files, then attempt to empty the trash again.

If you are unable to close all open files, resort to the method described in the GUI approach.

 
Tell Me More...
warning

sudo

sudo (super user do) is a Unix command that allows you to run another command as root. You must enter your admin password (not the root password) when prompted.

A user without administrative privileges cannot use 'sudo'

If you were to accidentally put a space between '.' and 'Trash' :

%rm -rf ~/. Trash

'rm' would apply to both '~/.' (your home directory) and 'Trash' ( a non-existent directory). You would wipe every single file from your home directory. Oops!

That's one reason why I suggest the '-i' option

% rm -ri ~/.Trash

so you get a prompt for each file. Hopefully you will spot your mistake on the first confirmation and avoid loosing anything


4 - The Nightmare Scenario

If all of the above methods fail, the offending files my have the System Immutable flag set. Such files are locked Fort Knox style. The System Immutable flag cannot be changed by root. Only by super-root - the super-duper-user. What!?!

When your Mac is up and running in multi-user mode (the normal operating mode) it is running at level 1. Some operations even root can't do at level 1, such as turn off the System Immutable flag. You must run at level 0. Switching into single user mode will allow one to run at level 0 and thus change the System Immutable flag.

Do this.

Close all applications and issue the command:

% sudo shutdown +0 "Bye bye"

to shutdown multi-user mode and enter single user mode. You will lose all services such as network connectivity while in single user mode. Alternatively, you can reboot your Mac, then startup with both the Command (Apple) key and the 's' key held down.

% cd /Users/your-name-here/.Trash
% chflags -R noschg *

Then hit control-d to return to multi-user mode. You should now be able to delete the trash with the Finder, or by reapplying the steps above.

Note: when you enter single user mode, type:

% whoami

if the answer is not root type:

% su

and type control-d twice when you exit.

Other Trashes

On some occasions, trash is placed at the root level of your system disc or another partition or drive. The directory is:

/.Trashes/501

and

/Volumes/disc-name-here/.Trashes/501

See if they are empty by listing them.

% sudo ls -al /.Trashes/501
...

'501' is the User ID of the user who did the trashing.

% sudo ls -al /.Trashes

may reveal several such 50x directories.

You can check your own User ID (uid) by issuing the command:

% id

and substitute this if it is not 501.

You can then apply all the techniques given for ~/.Trash to /.Trashes/501.

And Finally

If you still have problems, and you have re-applied the above steps as necessary, you probably have a corrupt file system.

Firstly, enter single user mode and issue:

% fsck

and answer the prompts, or else have 'fsck' run automatically with:

% fsck -y

You can also try commercial disc repair programs.

Happy trashing, and

Enjoy :-)



Discuss this article in the Learning Center forum




previous

How To Fix Stubborn Trash and Why it Won't Delete (page 1 of 2)

end

Copyright © 2000-2009 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.