How do I delete a file under Linux operating system?
To remove a file or directory in Linux use the rm command.
rm command syntax
rm (short for remove) is a Unix / Linux command which is 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). The syntax is as follows:
rm -f -r {file-name}
Where,
- -f: Forcefully remove file
- -r: Remove the contents of directories recursively
Remove 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 command:
$ rmdir mydirectory
Read a List of All Files To Delete From a Text File
The rm command is often used in conjunction with xargs to supply a list of files to delete. Create a file called file.txt:
$ 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 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 Linux 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.
Originally posted 2016-03-07 02:15:51.