How to Remove Files or Directories Using Linux Command Line

March 11, 2022

In this tutorial, I’m going to show you how to use the unlink, rm and rmdir and to Remove Files or Directories Using Linux Command.

How to delete a file in Linux

In Linux, you have a few options for removing files: you can use the rm command to delete a single file or multiple files. Using rm will prompt you for confirmation before deleting a file. Make sure you move the file to a different directory before using rm. Alternatively, you can use regular commands to delete files. However, it is important to remember that rm can be risky to use on your system. So, be careful and take the necessary security measures before beginning! 

You can remove or delete a Linux file from the command line using either the rm (remove) or the unlink commands.

The unlink commands allow you to delete a single file only, while rm lets you remove multiple files at the same time..

You should be extra cautious when deleting files or directories. Once deleted, it can’t be easy to recover the file.

  • You can delete one file by using unlink or rm commands.
unlink filename
rm filename

If the file is protected by writing, you will be asked for confirmation, as shown below. To delete the file, type y and hit Enter. If the file isn’t write-protected, it will be deleted automatically.

rm: remove write-protected regular empty file 'filename'?
  • You can delete multiple files simultaneously using the rm command, followed by the file names separated with space.
rm filename1 filename2 filename3

A wildcard ( * ) or regular expansions can match multiple files. The following command can be used to delete all .pdf files from the current directory:

rm *.pdf

Regular expansions require that you first list all files using the ls command. This will help you to see which files will be deleted before running the rm command.

  • To confirm that each file is deleted, use the rm option with the -i option:
rm -i filename(s)
  • You can remove files without prompting, even if they are write-protected. Pass the -f (force option) to the rm (remove) command.
rm -f filename(s)
  • You can also combine and rm options. The following command can remove all .txt files from the current directory, and it does not require a prompt in verbose mode.
rm -fv *.txt

How to remove directories (folders) in Linux

How to remove directories (folders) in Linux

Read on if you’re wondering how to remove a directory or folder. There are a couple of different methods. In this tutorial, we’ll cover the basic command line methods. Once you know the command, you’ll be able to delete files with it. Before you delete a file, you’ll want to ensure that you’re not deleting an essential application or system file. Where you can override the prompt with the -f option.

You can delete/remove directories in Linux using the rmdir or rm.

rmdir can be used to delete empty directories from the command line, while rm allows you to remove guides and their content recursively.

  • You can remove an empty directory using rmdir or rm -d, followed by the directory name.
rm -d dirname
rmdir dirname
  • Use the rm command to remove directories that are not empty and all files contained within them. This command is combined with the -r (recursive) option.
rm -r dirname

If the directory or file is protected by write, you will be asked to confirm the deletion.

  • To delete non-empty directories without being asked, you can use rm and the -r (recursive) and -f options.
rm -rf dirname
  • You can remove multiple directories simultaneously by using the Rm -r command, followed by the directory names separated with space.
rm -r dirname1 dirname2 dirname3

Similar to files, you can also use regular expansions and a wildcard ( * ). This allows you to match multiple directories.

How to Fix a ‘Linux Remove Directory Not Empty’ Error?

How to fix a ‘Linux remove directory not empty’ error? You can use rmdir command, which deletes empty directories and files. This command is also useful to remove files from system drives. It should work as expected when the directory is not empty. You can also use the find command to remove all files from the directory. If ‘rmdir’ command doesn’t work, try ‘dd’ or ‘switch’ instead of rmdir.


Now, you should have a solid understanding of how to remove files and directories using the Linux commands rmrmdirunlink (Remove options)

If you have any further information, please feel free to leave a comment.

The right decision!
If you're ready to make an impact in your business, it's time to get started now! Discover our offers and make the right decision!
Get Started Now!
payment gateway method for lgvps company
payment gateway method for lgvps company
Stripe paypal method for lgvps
A BeinCart LLC Company
Copyright © 2022 BeinCart LLC | Designed by LGVPS Team