Q: How do I remove files from the command line?

Context

This article talks about using command line commands to remove files under Mac OS X, Unix, and Windows. It also talks a little about the risks entailed therein.

Answer

A quick summary of the risksMost graphical desktops these days provide a safety net when deleting files or folders, such as the Trash or Recycle Bin. These safety nets are usually bypassed when removing files using the command line. In many cases the file is immediately and irretrievably deleted. In others the file can possibly be retrieved, but only with great difficulty and special tools.

Use command line commands with caution, especially when removing or moving files

Be especially careful when using them as an administrator or as the root user

Be especially careful when working with system files or files not in your user home directory

Always keep a recent and complete backup of your system

On Mac OS X

To bring up a command line / command prompt under Mac OS X launch the Terminal application located in the Utilities folder inside your Applications folder

The table below lists command line commands to remove files and folders

Command

What happens

Comments

rm -i filename

Removes file filename after confirmation

The -i is optional but recommended as it asks you to confirm before removing the file}

rmdir foldername

Remove the empty folder foldername

No confirmation needed, but only works if the folder is already empty

rm -i -r foldername

Removes the folder foldername and all of its contents

Again the -i is optional, recommended, but in this case annoying, especially if there are a lot of items in the folder. You will be asked to confirm each item separately.

For more information look at the man (manual) pages for these commands

To bring up the man pages for rm or rmdir use:

man rm
man rmdir

On Unix/Linux

Steps to bring up a command prompt in a terminal window on a Unix or Linux system vary; look for and launch something called Terminal, XTerm, or similar

The table below lists basic command line commands to remove files and folders

Command

What happens

Comments

rm -i filename

Removes file filename after confirmation

The -i is optional but recommended as it asks you to confirm before removing the file}

rmdir directoryname

Remove the empty directory directoryname

No confirmation needed, but only works if the directory is already empty

rm -i -r directoryname

Removes the directory directoryname and all of its contents

Again the -i is optional, recommended, but in this case annoying, especially if there are a lot of items in the directory. You will be asked to confirm each item separately.

For more information look at the man (manual) pages for these commands

To bring up the man pages for rm or rmdir use:

man rm
man rmdir

On Windows

To open the Windows command line shell select the Run item from the Start menu

In the box that opens asking you what to run, enter "cmd"

A window with a command prompt will open

If there is no Run item in your Start menu but there is a search box you can type into when you open your Start menu (the default in Windows Vista) you can type "cmd" directly into the search box and press Return to launch a window with a command prompt.

The table below lists basic command line commands to remove files and folders