English
+370 5 205 5502 sales@monovm.com

How to Remove Files and Directories [Linux Delete Directory]

Are you looking to delete or remove Linux directory/Delete Linux file? This blog post will help you to do that and achieve your goals in a very minimal time duration to delete files or delete Linux directory.

14 Dec, 21 by Antoniy Yushkevych 9 min Read

List of content you will read in this article:

The rm stands for remove, so the rm command is used to delete files and directory entities from a file system like UNIX, such as folders, directories, symbolic connections, etc. To be more specific, rm eliminates references to objects from the filesystem where such objects may have several references, such as a file with two names. It does not delete folders by chance.

This Linux command usually operates quietly, and you should take caution when using it because if you erase files, you won't be able to restore the contents of certain folders. So in this tutorial, we will explain everything about how to delete Linux directory and how to delete files in Linux/remove Linux directory and remove files in Linux.

The rm command has the following syntax:

rm [OPTIONS]... FILE...

when we use rm command with the file name as an argument to erase a single file, below is the syntax listed:

rm file

You'll get an "Operation not allowed" error if you don't have write permissions on the parent directory. The file will be deleted without warning if it is not written safely. If the instruction succeeds, it produces no output and returns empty. The command will ask for clarification before deleting write-protected files, as seen below:

rm: remove write-protected regular empty file 'file'?

Type y and press Enter to delete the file.

-f and the -v Option

The -f alternative instructs rm never to prompt the user and to ignore any files or arguments that aren't there.

rm -f file

Using the -v (verbose) alternative to get more detail on what's being removed:

rm -v file1

It will remove the 'file1' file.

How to Delete Multiple files?

rm command helps you to remove multiple files at the same time. To do so, use the following syntax to transfer the filenames as arguments, separated by a space:

rm file1 file2 file3

Regular expressions can be used to align different files. To delete all .jpg files in the current directory, for example, type:

rm *.jpg

It is usually a good idea to list the files with the ls command before executing the rm command by using regular expressions to see which files can be removed.

How to Delete Linux Directories?

Using the -d alternative to delete one or more empty directories:

rm -d dirname1

The rm -d command is similar to the rmdir command in terms of functionality.

Use the -r (recursive) alternative to deleting non-empty folders and all files within them recursively:

rm -r dirname1

rm -rf Command

The rm command would ask you to validate the procedure if the specified directory or a file inside it is write-protected. Using the -f alternative to delete a directory without being prompted:

rm -rf dirname1

The rm -rf command must be used with great care as it is very dangerous.

Linux commands (rm/rmdir) come with various options that we will be discussing in this article.

While using this command, you must understand that running rm/rmdir commands will not move the files and directories to the trash. They will be immediately removed from your system leaving no option to recover them. If you delete the files and directories accidentally then you cannot restore them. So make sure before permanently deleting the files, you create some backup.

Let’s understand the use of rm/rmdir commands along with various options available in the Linux operating system.

1. One of the simple tasks of deleting a file is from the current working directory. You only need to run the rm command along with the file name that you want to delete. You can consider the below example.

rm file_name_1.txt

2. If the desired file that you want to delete is not present in the current working directory, then you need to enter the complete path to that file name as shown below.

rm ./path/to/the/file/file_name1.txt

3. Not only this, you can use the rm command for deleting multiple files that you can specify after the rm command with a space between each file name as shown below.

rm file_name_2.txt file_name_3.txt

4. If you want to delete a bunch of files that are having the same extension for example files ending with “.pdf”. You can run the below rm command along with the extension. This command will help in deleting all the pdf files in the current working directory.

rm *.pdf

5. This command will delete all the files that is having the pdf extension. But if you enter the wildcard character like “?”, it will delete the files that is having a single character extension.

rm *.?

6. If a specific file is having write-protected permissions, then you will be asked for confirmation for deleting that file permanently. If you want to proceed, you can press “y” and hit enter accordingly.

For reducing the risk of using the rm command directly to deleting the file permanently, it is better that you use the “-i” option along with the rm command to ask you to confirm the deletion.

rm -i *.dat

7. If you want to delete a file forcefully then you can use the “-f” option along with the rm command even the specific file has the write-protected permission for that file.

rm -f filename

1. If you are removing the directories that are empty then you can specify the “-d” option along with the rm command. Also, you can use the wildcards (* and ?) within the names of the directory similar to the filenames.

rm -d directory

2. If you want to delete multiple directories then you can specify different directories after the rm command separated by spaces as shown below.

rm -d directory1 directory2 /path/to/directory3

3. If you are removing a directory that is not empty, you can use the “-r” option along with the rm command for the recursive operation. It will remove the directories along with the files within it.

rm -r directory1 directory2 directory3

4. If the directory is having write-protected permission then you will be prompted for permission to delete that directory. For deleting directories that are not empty along with suppressing the prompt, you can use the following command with “-r” and “-f” options.

rm -rf directory

5. For running the above command, you have to be careful as it will delete file in Linux without any prompt and cannot be recovered. So make sure you have the proper understanding of the directory structure. For solving this problem, you can use the tree command.

But first, you need to install the package for using the tree command on your Unix or Linux distribution.

sudo apt-get install tree

6. If you run the "tree" command, you will get a tree diagram of the directory structure and the files stored within it.

tree

For deleting the directory, you can also use the rmdir command. But the main difference between using rm and rmdir commands is that the rmdir command will help in deleting those directories that are empty. It cannot be used for deleting files.

  • In Linux, one of the easiest commands for deleting the empty directory is rmdir command. But you cannot use this command for deleting multiple directories as it is possible with rm command. 

rmdir directory

  • For deleting multiple empty directories, you can use the following format of the command.

rmdir directory1 directory2 directory3

  • If the specific directory that you want to delete is not in the current working directory, then you can provide the path of that directory as shown below.

rmdir /path/to/directory

  • If you use the rmdir command to delete Linux directory that is not empty, you will get an error message.

If you want to delete the directory along with its parent directory, then you can use the “-p” option with the rmdir command. It is possible as the rmdir command will start from the target directory and then move to the parent directory. The target directory now will be empty so it can be deleted by the rmdir command. The process will keep on repeating till the path is provided to the rmdir command.

We've seen how we can delete files and directories on Linux operating system using the rm or rmdir commands.

When deleting sensitive data or folders, use strict caution because it cannot be restored if a file is destroyed. We have also seen how to use rm functions with different opinions commands available in the Linux operating system. The regular expression can also be used with the rm command in Linux.

People Are Also Reading:

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com