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:
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 11-29-07

{ 4 comments… read them below or add one }
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.
To remove all files in a directory:
rm -f path/to/my/directory/*.*
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.
Hi,
How to remove all the files in a directory except a single file using rm command.