How to Remove Files and Directories Using Linux Command Line 2024

How to Remove Files and Directories Using Linux Command Line

In the world of Linux, a command line is a powerful tool for managing files and directories on your system. One of the most common tasks you may need to perform is deleting files and directories, which can be accomplished using various command line utilities such as rm , unlink , and rmdir.

In this article, we will explore the different commands available for deleting/ removing files and directories, their respective syntaxes, and how to use them effectively. We will also cover some important considerations to keep in mind when using these commands, including how to avoid accidentally deleting important files and how to handle permissions and ownership issues. Whether you are a beginner or an experienced Linux user, this article will provide you with a comprehensive guide to safely and efficiently remove files and directories from your system using the command line.

How to Remove Files

In Linux, you can use either the rm (remove) or unlink command from the command line to delete or remove a file. While the unlink command can delete a single file, the rm command provides the flexibility to remove multiple files simultaneously.

It is important to exercise caution when removing files or directories, as once a file is deleted, it cannot be easily recovered. Therefore, it is recommended to double-check the file name and path before executing the deletion command to ensure that the intended file(s) are selected for removal.

  • To remove a single file, you can use the rm or unlink a command followed by the file name. Here’s the basic syntax:
rm file_name

unlink file_name

In case the file has write-protection enabled, a confirmation prompt will appear, as demonstrated below. To proceed with deleting the file, type y and then press the Enter key. However, if the file does not have write protection enabled, it will be deleted without any further prompts.


rm: remove write-protected regular empty file 'file_name'?
  • To delete multiple files simultaneously, you can use the rm command followed by the names of the files you want to delete, with each file name separated by a space.
rm file_name1 file_name2 file_name3

Using a wildcard (*) and regular expressions can help you match and delete multiple files at once. For example, to delete all files with the .pdf extension in the current directory, you can use the command:

rm *.pdf

When using regular expressions, it’s always recommended to first list the files you’re targeting with the ls command. This allows you to verify which files will be affected before running the rm command.

  • You can use the rm command with the -i option to confirm each file before deleting it. This prompts you to confirm the deletion of each file individually, reducing the risk of accidentally deleting the wrong files.
rm -i filename(s)
  • You can use the rm command with the -f (force) option to remove files without prompting, even if the files are write-protected. This option overrides any write-protection or confirmation prompts, so it’s important to use it with caution.
rm -f filename(s)
  • You can combine multiple options when using the rm command to customize its behavior according to your needs. For example, to remove all files with the .txt extension in the current directory without a prompt and in verbose mode, you can use the following command:
rm -fv *.txt

How to Remove Directories or Folders

You can delete directories in Linux by using either the rmdir or rm command.

The rmdir utility is a command-line tool that is used to delete directories that are empty, meaning they do not contain any files or subdirectories. On the other hand, the rm command can be used to delete both directories and their contents recursively, including all subdirectories and files within them. This makes rm a more powerful and versatile tool for removing directories and their contents.

  • You can use either rmdir or rm -d followed by the directory name to remove an empty directory.
rm -d dirname

rmdir dirname
  • You can use the rm command with the -r (recursive) option to remove directories and all the files within them, even if they are non-empty. This option allows you to delete the entire directory tree, including all its subdirectories and files.
rm -r dirname

When attempting to delete a directory or a file within a directory that is write-protected, a confirmation prompt will be displayed to confirm the deletion.

  • You can use the rm command with the -r (recursive) and -f options to remove non-empty directories and all their files without being prompted for confirmation.
rm -rf dirname
  • To delete multiple directories at once, you can use the “rm” command with the “-r” option followed by the names of the directories you want to delete, separated by spaces. This will remove the directories and their contents recursively.
rm -r dirname1 dirname2 dirname3

Similarly to files, you can use a wildcard (*) and regular expressions to match multiple directories as well.


In conclusion, the Linux command line provides a powerful and efficient way to remove files and directories from your system. By understanding the basics of how the command line works, you can easily navigate through your directories and remove unwanted files or directories using a few simple commands.

Using the rm command, you can remove files and directories recursively, force the removal of read-only files, and even remove files based on a pattern or wildcard. Additionally, the rmdir command provides a straightforward way to remove empty directories.

It’s important to use caution when using these commands, as they can permanently delete files and directories from your system. Always double-check your commands before executing them, and be sure to back up any important files or directories before removing them.

By following these guidelines, you can use the Linux command line to efficiently manage your files and directories and keep your system organized and clutter-free.



Hey there! My name is Alex and I'm a professional content writer. I'm also lucky enough to be part of an amazing trio team! I absolutely love what I do and I'm passionate about creating content that engages, informs, and entertains. Whether it's writing blog posts, website copy, or social media content, I always strive to deliver high-quality work that exceeds expectations.

We will be happy to hear your thoughts

Leave a reply