Special offer
popup-offers
English
+370 5 205 5502 sales@monovm.com

How to Delete Directory in Linux [How to Delete File in Linux]

Do you want to find out how to delete files and folders in Linux? Are you interested in how to use the rm command more effectively? Find out in this article.

01 Jun, 21 by Antoniy Yushkevych 3 min Read

List of content you will read in this article:

The command rm stands for remove, so rm command is used to delete 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 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 or folders. So in this tutorial, we will explain everything about the rm command and different factors we should consider while using this command. 

 

The rm (remove) command has the following general syntax:

rm [OPTIONS]... FILE...

Using the rm command with the file name as an argument to erase a single file:

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.

Delete Several Files

rm command helps you to remove several 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.

Delete 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

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.

 

We've seen how we can delete files and folders from your Linux setup using the rm button.

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.

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