Home  >  Article  >  System Tutorial  >  What is the command to delete files in linux

What is the command to delete files in linux

WBOY
WBOYOriginal
2024-02-21 18:51:031419browse

In the Linux environment, the command to delete files is rm. Use this command to free up disk space by removing files that are no longer needed from the file system.

The basic syntax of the rm command is:
rm [option] file or directory

Among them, the commonly used options are:
-r, -R: recursively delete directories and their Content;
-f: Forced deletion, no confirmation required;
-i: Interactive deletion, ask for confirmation before deletion;
-v: Display the name of each deleted file.

The following are some commonly used sample code instructions:

  1. Delete a file:

    rm file1.txt

    The above code will delete the file named "file1 in the current directory .txt" file.

  2. Delete multiple files:

    rm file1.txt file2.txt file3.txt

    The above code will delete "file1.txt", "file2.txt" and "file3.txt" in the current directory files.

  3. Delete a directory and all its contents:

    rm -r dir1

    The above code will delete the directory named "dir1" and all its contents in the current directory. When there are subdirectories or files in the directory, you will be asked to confirm whether to delete them. If you want to force the directory to be deleted, you can add the -f option.

  4. Delete all files in the directory (retaining the directory itself):

    rm -v dir1/*

    The above code will delete all files in the "dir1" directory, retaining the directory itself. The -v option can be used to display the name of each deleted file.

  5. Delete a directory and all its contents (no confirmation required):

    rm -rf dir1

    The above code will delete the directory named "dir1" and all its contents under the current directory, And no confirmation is required. The -f option is used to force deletion, and the -r option is used to recursively delete subdirectories and files.

Summary:
In Linux systems, use the rm command to delete files and directories. When deleting a file, you only need to specify the file name; when deleting a directory, you need to add the -r option, and choose whether to add -f, -i, -v and other options as needed. When performing a deletion operation, please confirm whether the operation object is correct and ensure that important files have been backed up.

The above is the detailed content of What is the command to delete files in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn