Linux / UNIX: Delete a file

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:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 4 comments… read them below or add one }

1 abdul khader Naik 02.21.08 at 11:45 am

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.

2 Robert Dyke 12.18.08 at 10:24 pm

To remove all files in a directory:

rm -f path/to/my/directory/*.*

3 Mad93 06.16.09 at 8:02 pm

It should be:

rm -f path/to/my/directory/*

Since in linux files doesn’t need file extension. Also, if you want to remove folders inside you have to add the -r flag.

4 Suresh 06.17.09 at 6:52 pm

Hi,

How to remove all the files in a directory except a single file using rm command.

Leave a Comment

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

Tagged as: , , , , , , , , , , , , , ,

Previous post: Linux LAN card: Find out full duplex / half speed or mode

Next post: How to: Add or display today’s date from a shell script