how to remove directory in linux?

So you want to delete Remove a Directory in Linux but while attempting to remove a directory using a command such as the rmdir, you may receive a prompt such as “rmdir: ‘dir’: Directory not empty” and be unable to delete the directory.

To remove a directory that contains other files or directories, use the following command.

remove a directory in Linux

rm -r dir

So in the example above, you will have to replace “dir” with the name of the directory you want to delete. For example, if the directory named file, you would type rm -r file at the prompt

But wait, there is one condition to this. Executing the above command would delete all files and directories in the directory named in the command used by you. However, it would also present a prompt for approval to delete each of the files. So if you don’t want to receive a prompt for each file, use the following command instead.

rm -rf dir

Now in the example above, the “dir” directory, along with all files and directories in that directory, would be deleted with no prompt or message.

 

some of the Sample commands :

you can also use these commands to delete a folder in Linux and each represents the different way to delete a folder

rm -r /path/to/dir/
rm -rf dir1
rm -rfv /path/to/dir
rm -rfv /tmp/delta/

Where,

  • -r – Remove directories and their contents recursively
  • -f – Forceful option i.e. ignore nonexistent files and arguments, never prompt for anything
  • -v – Be verbose. Show what rmdir or rm command doing with the given directory
  • -i – Prompt before every removal of file/dir
  • -I – Prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes. Useful when working on a large number of files on Linux
0 0 vote
Article Rating
nv-author-image

Anurag batra

A Tech Enthusiast who loves to work on his craft and make unique stuff From his Programming Skill at a same time Sharing that Knowledge with the outside world to help others.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments