Home -> Articles -> MacOSX Tips -> Terminal Commands

MacOSX Tips

Terminal Commands

 

Welcome to the OSX Novice Terminal notes.

Some Background- When you create your OSX account, it is as administrator. But OSX is set up so that certain parts of the OS (such as System files) are inaccessible as admin. There are also strict ownership rules of files/folders that you will not be able to over come with admin level access. Ownership problems may pop up if you delete one of your owners. All of their files will be effected. You may be allowed to place these files in the trash, but not to delete them. And then you may not be allowed to remove them from the trash back onto the desktop.

The way around this problem is to obtain Root Access- the highest level of access. When you obtain Root Access, you can do anything you like, including destroying the OS by accident, if you're not carefull. But if your dealing with lots of older files with ownership issues, Root Access will become necessary. If you take it step by step, there should be no tragedies.

There are three ways to obtain Root Access. These two links take you back to the the MacOSX Tips page.

  • When it involves this specific issue (emptying stubborn files from the trash), the first and easiest way to obtain root access is by using one of the many OSX utilities (such as BatChmod) that deal with file ownership manipulation.
  • But if you'd like to expand your horizons- The second way is to enable a Root account in the NetInfo Manager. Then log into OSX as Root.
  • The third way is to invoke Root Access in the Terminal Application as I'll describe below. Logging in as Root is separate from invoking Root in the Terminal and is explained on the MacOSX Tips page.

Please don't confuse me for a Terminal expert! I don't have a big desire to learn Unix. I had a problem with my trash. It became necessary to learn more about Root Access.

This particular discussion focuses on the Terminal. The OSX Terminal application is a direct window into OSX's Unix's underpinnings. There are tons of Unix commands that I don't have a clue why or what they are for. The Terminal can be accessed at any time. Logging on as Root requires shutting down all of your applications, logging out and then back in. I'm not suggesting that one way is better than the other.

As I become more knowledgeable, I'll clarify items as needed. This article has already been updated several time. If anyone sees glaring errors, please drop me a note! I want this to be right.

Unix Commands- A large source of info on Unix commands comes from the Terminal itself by using the "man" command (for System Reference Manual). Read the entire paragraph before attempting.

  • Open the Terminal (Go> Applications> Utilities> Terminal) and type "man command" <Return>- where "command" is replaced with the UNIX command. For Example "chown" is the command for Change Owner. In the Terminal type: man chown <Return>. You'll get several pages of info on Change Owner.

Warning!- Proceed at your own risk! This is serious stuff. But nothing bad happened to me. The "rm" command removes items from a directory. If you are in the wrong directory or if you type the wrong thing, you delete important files, or worse, you can break OSX. However, the drag-file-from-trash method is relatively safe. I mean I've not been burned yet. <g> You've been fairly warned. :-)

With the terminal and UNIX there are several ways to skin the cat. Any of the methods can be used to remove trash that OSX won't allow you to as administrator.

Important! I've not found a situation where you have to type a file/folder directory path when working in the Terminal. When it is time to designate the file/folder to manipulate, the simple way is to navigate to the file/folder's location on the hard drive and simply drag it's icon into the Terminal window. The file/folder's path will appear in the Terminal window. In the Terminal, each command must be seperated by a space. See specific examples below.

Invoke Root with sudo -s

  1. Open the Terminal Application (located in the Utilities Folder in the Applications folder). You should see your user name % sitting to the left of the cursor. Type the commands below.
  2. Note- A space separates each Unix command and the file/folder names.
  3. Terminal Commands
    • sudo -s <Return> (space between "sudo" and "-s")
    • Enter Admin password when asked, <Return>
    • rm -R .Trash <Return>(space between "rm" and "-R" and between "-R" and ".Trash")

  4. If all the files in the trash don't delete:
    • See the Terminal picture below.
    • There is a directory issue. We address this by dragging files from the trash to the terminal window.
    • Double click the trash to open its window. Have the Terminal and Trash windows sitting side by side.
    • Terminal Commands
      • As per above, you've already used the sudo -s so "root" should be listed to the left of the cursor in the Terminal window.
      • Type: rm -R (space between "rm" and "-R" and leave a space after the "-R")
      • Grab a stubborn file from the trash and drag it to the Terminal Window. The file and the directory where it came from will appear in the Terminal window after the "rm -R " string you all ready typed.<Return>
      • exit <Return> The name "root" will revert to your user name. Quit out of the Terminal program.
    • Note- Save time by dragging more than one file/folder to the trash at a time. Drag them all at once!
    • Terminal Command Definitions:
      • sudo -s= Invokes permanent root for current Terminal Session.
      • rm= Remove.
      • -R= Recursively.
        • Includes all subdirectories of the directory about to be named.
        • Required for folders with contents. Not required for files.
      • .Trash= the Trash folder in your user directory.


Change Ownership of Files/Folders in Trash and then delete them in the Finder- Type these commands in the Terminal. Each line followed by Return.

  1. cd .Trash - (Space between "cd" and ".Trash") Changes to the Trash directory in your User Folder.
  2. sudo -s -Grants permanent root access for current terminal session.
  3. chown -R username * - Replace "username" with your user name.You should now own all files in the trash.
  4. Command Descriptions:
    • chown= change owner
    • dpeck= my user name
    • -R= Recursive (required for folders with contents)
    • *= all files/folders
  5. Go to the Finder and delete the trash.
  6. If everything in the trash does not change to your ownership- Sometimes the command listed above will not work due to directory confusion. Plan "B: involves dragging files from the trash to the terminal window. Type chown -R username but leave a space after your username. Then drag the file(s) from the Trash to the Terminal window and hit <Return>. This should take care of any stubborn files. Then empty the trash from the Finder.


Change Ownership of Files/Folder/Volumes
I recently discovered a folder that I could not trash. It could be opened, but no files could be added to it. And it could not be trashed. It was owned by a User that I had deleted. I used the Terminal to change the ownership of everything in the folder to user: dpeck .

  1. Open the Terminal App.
  2. Your user name appears followed by %.
  3. Type sudo -s. <return>
  4. Your user name changes to root#.
  5. Type cd (leave a space after cd)
  6. On the desktop, navigate to the file/folder/volume you want to change onwership of. In the example, the folder is named: MDOCSiB. Drag the item to the terminal window. <Return>
    • Note: In the example displayed, the first time I tried this I did not use cd and I got back the message Permission denied. The 3rd line below the Password: line is the correct format.
  7. The directory changes and appears to the left of root in the brackets [ ].
  8. Type chown -R username * <Return> Spaces between each group- chown, -R, DPeck, *. Use your user name. Mine is dpeck).
  9. If the command is accepted, a new line appears ending in: root#. If it is not accepted a message will appear. In the above example, when I first tried this command, I did not leave a space between dpeck and *. The result was: chown: No match, because there is no owner named dpeck*.
  10. When finished type exit <Return> and quit the Terminal.

Delete Deleted-User's Home Folder- (MA 08/02, p21) In this example, the User's name is JJohnson. When the User, JJohnson is deleted (System Preferences>Users) , the User folder is renamed JJohnson Deleted. The System may not allow you to delete this folder (located in the Users Folder).

  • Navigate to the Users Folder (top level of OSX hard drive)
  • Open the Terminal Application (Go>Applications>Utilities)
  • Type sudo rm -R (leave a space after -R)
  • Drag the Deleted User's Folder (JJohnson Deleted) to the Terminal and hit <Return>, enter Admin Password.
  • The Deleted User's Folder will be deleted.
  • Type exit <Return> and quit the Terminal.

Sudo to delete files from Trash- sudo rm -r [leave space after r, drag file from trash to terminal] -Deletes file/folder from the trash.


Unlocks File/Folder- sudo chflags -R nouchg [leave space after nouchg, drag file from trash to terminal.]

Terminal Command Descriptions:

  1. -R= recursively
    1. all subdirectories of the directory about to be named
    2. Required for folders with contents
  2. uchg= immutable flag (locked)
  3. no= turns off flag (used with uchg = nouchg)

Terminal Pics

This is what the terminal looks like when it first starts. A new line in the terminal has a couple of brackets with some numbers, colon and tilde, followed by your user name, here is an example. My user name is dpeck.The input cursor sits to the right

Using the sudo -s Command In this example I did not use the rm rm -R .Trash command as describe above. Instead I dragged files from the trash to the terminal window.

Three folders- "Genealogy", "Stuffit" and "Utilities" refused to be deleted. They did not respond to previous attempts.

  • I opened both the Terminal and Trash folders. Then I typed "sudo -s" (space between "sudo" and "-s"), to get root access, and hit Return. Terminal asked for my administrative password. Notice in the next line, my user name is gone, replaced with "root".
  • I typed in "rm -R " (leaving a space between "rm" and "-R" and after the "-R"), and then dragged the Genealogy folder from the trash to the Terminal Window. See the terminal line with "Genealogy" in it. I hit Return and it disappeared from the trash.
  • On the next line I repeated the command: rm -R and dragged the Stuffit folder from the trash to the terminal window. The verbage starting with "/Volumes" and ending with "/Stuffit" filled in the line.
    • When I hit Return a new line appeared: [66-191-145-4:~] root:
      • At this point the Stuffit folder has been deleted although you can still see it in the trash.
    • And then I did the same thing for the Utilities folder.
      • Although both folders are visible in this picture sitting in the trash, when I clicked back into the trash window, they went away.
  • To finish up, I typed: "exit" and hit Return.
  • In the next line, "Root" is now replaced with my user name.
Using chown to change file ownership-
This series of commands is used to to change the ownership of all files/folders located in the Trash.

The trashed files can then be deleted normally in the the Finder.

If this does not change all the files, they can be dragged from the trash the terminal window as described above.
Dragging Multiple Files- I found that when using the sudo command (not sudo -s), if you drag multiple files from trash to the terminal window at the same time, dialog boxes will pop up in the terminal asking for permission to dump individual files. Here is an example. You respond by typing y <Return> for each instance.

Terminal Links/Resources

 

 

 

 

 

Top