Delete Command In Linux

Saturday, Aug 10, 2024 | 3 minutes read | Update at Saturday, Aug 10, 2024

@ İsmail Baydan

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:

  1. Delete a single file: The rm command 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.txt
    

    This command deletes the file named filename.txt from the current directory.

  2. 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.txt
    

    This command deletes file1.txt, file2.txt, and file3.txt simultaneously.

  3. 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.

  4. Delete a directory: We can delete a directory in Linux with the rmdir command. 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 the rmdir command can not delete.

    rmdir directory_name
    

    This command deletes an empty directory named directory_name.

  5. Delete a directory and its contents recursively: We can use the rm command with the -r option to delete directories and its contents. The -r option is used for recursive deletion.

    rm -r directory_name
    

    This command deletes the directory directory_name and all of its contents, including subdirectories and files.

  6. Force delete a file: Some files and directories may be protected or ask approval for deletion. The -f option is used to force deletion of the files. This will bypass or accept prompts about deletion.

    rm -f filename.txt
    

    This command forces the deletion of filename.txt, bypassing any prompts or warnings.

  7. Force delete a directory and its contents: We can use -r and -f options to force and recusively delete files and directories. The -rf option is very powerfull to delete files and folders.

    rm -rf directory_name
    

    This command forcefully deletes directory_name and all of its contents, suppressing any confirmation prompts.

  8. 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 .log extension.

    rm *.log
    

    This command deletes all files with the .log extension in the current directory.

  9. 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.

  10. Delete files interactively: Sometimes there may be lots of files to delete but we have to preserve some of them. We can use -i option to delete interactively. This will prompt or ask before deletion of every file.

    rm -i *.txt
    

    This 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.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.