Access Keys:
Skip to content (Access Key - 0)

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

  • How do I remove files in Mac OS X using command line commands?
  • How do I remove files from a shell prompt on a Unix system?
  • How do I remove files from the Windows command shell?

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 risks
Most 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
Command What happens Comments
del filename Deletes the file filename Does not prompt for confirmation
del directoryname Deletes all files inside directory directoryname Asks for confirmation
rmdir directoryname Deletes the empty directory directoryname Does not prompt for confirmation

See also

IS&T Contributions

Documentation and information provided by IS&T staff members


Last Modified:

July 29, 2016

Get Help

Request help
from the Help Desk
Report a security incident
to the Security Team
Labels:
c-cyber-security c-cyber-security Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
Feedback
This product/service is:
Easy to use
Average
Difficult to use

This article is:
Helpful
Inaccurate
Obsolete
Adaptavist Theme Builder (4.2.3) Powered by Atlassian Confluence 3.5.13, the Enterprise Wiki