Delete Command In Linux
Saturday, Aug 10, 2024 | 3 minutes read | Update at Saturday, Aug 10, 2024
Linux provides different delete commands for files and directories. rm , rmdir or find commands can be used to delete files and directories easily.
Here are 10 examples of the delete command (typically handled using rm, rmdir, or similar commands) in Linux:
-
Delete a single file: The
rmcommand is used to remove files and directories. We can simply call rm as the Linux delete command. WE can delete single file with the rm command like below.rm filename.txtThis command deletes the file named
filename.txtfrom the current directory. -
Delete multiple files: We cand use the rm command in order to delete multiple files. The file names are provided to the rm command as parameters.
rm file1.txt file2.txt file3.txtThis command deletes
file1.txt,file2.txt, andfile3.txtsimultaneously. -
Delete all files in a directory: We can delete all files with the rm command. The
*glob sign is provided as parameter to the rm command which simply means delete all files. The following command deletes all files in the current working directory. Keep in mind that directories are not deleted with the following command.rm *This command deletes all files in the current directory. It does not delete subdirectories or hidden files.
-
Delete a directory: We can delete a directory in Linux with the
rmdircommand. But there is a recstriction about the rmdir command. This command can only delete empty directories. If there are some child content like files or directories thermdircommand can not delete.rmdir directory_nameThis command deletes an empty directory named
directory_name. -
Delete a directory and its contents recursively: We can use the
rmcommand with the-roption to delete directories and its contents. The-roption is used for recursive deletion.rm -r directory_nameThis command deletes the directory
directory_nameand all of its contents, including subdirectories and files. -
Force delete a file: Some files and directories may be protected or ask approval for deletion. The
-foption is used to force deletion of the files. This will bypass or accept prompts about deletion.rm -f filename.txtThis command forces the deletion of
filename.txt, bypassing any prompts or warnings. -
Force delete a directory and its contents: We can use
-rand-foptions to force and recusively delete files and directories. The-rfoption is very powerfull to delete files and folders.rm -rf directory_nameThis command forcefully deletes
directory_nameand all of its contents, suppressing any confirmation prompts. -
Delete files with a specific extension: We can delete files with specific extension. Instead of the file name we specify the extension like be
*.log. In the following example we delete all files with the.logextension.rm *.logThis command deletes all files with the
.logextension in the current directory. -
Delete hidden files in a directory: Linux hidden directory names start with
.. We can delete the hidden directories with the.*which simply means delete all files and directories starting with..rm -rf .*This command deletes all hidden files in the current directory.
-
Delete files interactively: Sometimes there may be lots of files to delete but we have to preserve some of them. We can use
-ioption to delete interactively. This will prompt or ask before deletion of every file.rm -i *.txtThis command prompts you for confirmation before deleting
filename.txt. For multiple files, it will ask for each file.
These commands should be used carefully, especially when using rm -r or rm -rf, as they can lead to irreversible data loss.