About Linux FAQ

Browse More FAQs:

Linux / UNIX: Delete a file

Posted by Vivek Gite [Last updated: November 29, 2007]

Q. How do I delete a file under Linux / UNIX / BSD operating system?

A. To remove a file or directory in Linux, FreeBSD, Solaris or Unixish oses use rm command.

rm command syntax

rm (short for remove) is a Unix command used to delete files from a filesystem. Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant.)

rm -f -r {file-name}

Where,

  • -f: Forcefully remove file
  • -r: Remove the contents of directories recursively

Remote or Delete a file

To remove a file called abc.txt type the following command:
$ rm abc.txt

To remove all files & subdirectories from a directory (MS-DOS deltree like command), enter:
$ rm -rf mydir

To remove empty directory use rmdir and not rm:
$ rmdir mydirectory

Read a list of file to delete from text file

rm command is often used in conjunction with xargs to supply a list of files to delete:
$ cat file.txt
List of to delete:

file1
/tmp/file2.txt
~/data.txt

Now delete all file listed in file.txt, enter:
$ xargs rm < file.txt

Never run rm -rf / as an administrator or normal UNIX / Linux user

WARNING! These examples will delete all files on your computer if executed.

$ rm -rf /
$ rm -rf *

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands.

See also:

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. abdul khader Naik Says:

    Will you please tell me the command(in unux/linux) which delete the content of directory without deleteing the directory itself.
    other than rm -rf(it will delete directory too) that i dont want.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.