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:
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:
- Linux delete directory with rmdir command
- Delete or remove a directory Linux command
- UNIX: Remove a file with a name starting with - character
- Linux: Delete user password
- Howto delete empty lines using sed command under Linux / UNIX
Discussion on This FAQ
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!
Tags: abc, bsd operating system, command syntax, delete files, filesystem, linux delete file, linux freebsd, linux remove file, oses, rm command, Solaris, unix bsd, unix command, unix delete file, unix remove file



February 21st, 2008 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.