Linux rm command - Delete a file or directory

Linux rm command: Delete a file or directory.

Linux rm command Function Description

The rm command can delete one or more files or directories in a directory, or it can delete a directory and all files under it and its subdirectories. For linked files, only the entire linked file is deleted, while the original file remains unchanged.

Be careful with the rm command. This is because once a file is deleted, there is no way to recover it. Therefore, before deleting a file, it is a good idea to look at the contents of the file again to determine if you really want to delete it. rm command can be used with the -i option, which is especially useful when deleting multiple files using the file extension character. With this option, the system will ask you to determine if you want to delete them one by one. At this point, you must enter y and press Enter to delete the file. If you only press Enter or another character, the file will not be deleted.

Linux rm command Syntax

rm [option] [file|directory]

The meaning of each option in the command is shown in the table.

Option Description
-f Forced deletion. Ignore non-existent files, no message is given
-r Recursive deletion of directories and their contents
-i Confirm the information before deleting it

Linux rm command Examples

Delete the file4 file from the current directory

[root@rhel ~]# rm file4
rm: Does it delete the ordinary empty file "file4"? y           //Enter y to confirm deletion of the file

Delete the directory /root/apidemos

rm -rf /root/apidemos

Output:

Linux rm command

Interactively delete the files test and example from the current directory

rm -i test example
Remove test ?n(Do not delete the file test)
Remove example ?y(Delete the file example)

Delete all files and subdirectories in the current directory except for the implied files

# rm -r *

It should be noted that it is very dangerous to do so!

Delete the package-lock.json file in the current directory

find .  -name "package-lock.json" -exec rm -rf {} \;

Find the file ending with *.html and delete it

find ./docs -name "*.html" -exec rm -rf {} \;

Delete files ending with *.html under the current project

rm -rf *.html

Remove the node_modules directory from the current directory

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

delete directory

rm -r [directory name] -r means to recursively delete all files and directories in the directory. -f indicates forced deletion

rm -rf testdir
rm -r testdir

Confirmation prompt before delete operation

rm -i [file/directory]

rm -r -i testdir

Batch delete the data folder in the subfolder of the icons folder

rm -rf icons/**/data

rm Ignore non-existent files or directories

The -f option (force) makes this operation forceful, ignoring error prompts

rm -f [files...]

Confirm deletion in certain scenarios only

The -I option ensures that only one confirmation is prompted when deleting more than 3 files or when deleting recursively (e.g. deleting a directory).

rm -I file1 file2 file3

rm Show details of the current delete operation

rm -v [file/directory]
Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?