Linux delete file command (super detailed)

Insert image description here

In Linux, we can use the rm command to delete files. rm command allows us to delete one or more files. However, be careful when using the rm command, as once deleted, the file cannot be recovered. Make sure you confirm that you no longer need the files before deleting them. Let’s talk about it in detail:

1. Delete a single file

To delete a single file, just use the rm command followed by the name of the file you want to delete:

rm 文件名

For example, to delete a file named "example.txt", execute the following command:

rm example.txt

2. Delete multiple files

If you want to delete multiple files, you can put the file names after the rm command and separate them with spaces:

rm 文件1 文件2 文件3

For example, to delete the three files "file1.txt", "file2.txt" and "file3.txt", execute the following command:

rm file1.txt file2.txt file3.txt

3. Delete the folder and its contents

If you want to delete an entire folder and all its contents, you can use the -r parameter (recursive deletion) to ensure that subfolders are also deleted:

rm -r 文件夹名

Please note that recursive deletion is very dangerous as it completely deletes the folder and all its contents beyond recovery. Make sure you think twice before doing this.

For example, to delete a folder named "my_folder" and all its contents, execute the following command:

rm -r my_folder

4. Confirm deletion

By default, rm command will delete the file directly without asking for confirmation. If you want to get a confirmation prompt when deleting a file, use the -i parameter:

rm -i 文件名

For example, to delete the "important.txt" file and confirm before deletion, execute the following command:

rm -i important.txt

rm The command will prompt information similar to the following:

rm: 是否删除 "important.txt"? 

Enter "y" to confirm the deletion or "n" to cancel the deletion.

5. Forced deletion

If you want to delete the file without receiving any confirmation prompt and force the file to be deleted, you can use the -f parameter:

rm -f 文件名

For example, to force deletion of a file named "temp.txt", execute the following command:

rm -f temp.txt

Please use the -f parameter with caution as it will delete the file immediately and cannot be recovered.

Guess you like

Origin blog.csdn.net/lcmaijia/article/details/134319542