Remove non empty directory linux

How to Remove a Non-Empty Directory in Linux

In Linux, whenever you want to delete a file, you’d use the rm command. When it comes to deleting a directory, however, things get a bit complex. To delete a directory, there’s a dedicated tool rmdir that can remove empty directories. What if the target directory contains a bunch of unnecessary files?

In this guide, check out how to remove non-empty directories in Linux.

Removing a Directory in Linux

First, let’s have a look at how to remove directories in Linux.

Removing an Empty Directory

In this example, I have set an empty directory:

To remove the directory, we can use rmdir:

Removing a Non-Empty Directory

When it comes to a non-empty directory, the previously mentioned methods won’t work.

Here, I’ve set a non-empty directory for demonstration:

Try to remove normally, use the following command:

To remove the non-empty directory, use the following rm command instead:

  • -r: Instructs rm to recursively delete the contents of the directory, both the files and sub-directories.
  • -v: Instructs rm to run in verbose mode.

If you don’t want any prompt for action, then add the flag “-f”, as well:

If you want to have prompts for actions, then use the flag “-i” instead. Note that rm will ask for all the files and sub-directories it’s about to remove:

Instead of “-i”, the flag “-I” is less intrusive while still protecting against mistakes:

Conclusion

In Linux, removing a directory is different than removing files. The removal tools and methods are also different depending on whether the target directory is empty or not. This guide demonstrates how to remove both empty and non-empty directories. Note that the directories and the data contained within and removed in this manner will have no way of being recovered except by using special tools and methods. Thus, before removing, ensure that it’s not something important.

Читайте также:  Linux только уникальные строки

The man pages of rm and rmdir contain further information about various supported options:

Источник

How to delete a non-empty directory in Terminal?

I’m unable to remove a directory like «New Folder» using all the above detailed commands. It’s double worded. But I want to remove that directory. Any suggestions will be welcomed. T.Divakara, Bengaluru, India

Its the blank space in the file name, try using ‘quotes’ > rmdir ‘New Folder’ < then the folder disapers, or use escape characters for non-vissible characters.

4 Answers 4

Use the below command :

It deletes all files and folders contained in the lampp directory.

In case user doesn’t have the permission to delete the folder:

Add sudo at the beginning of the command :

Otherwise, without sudo you will be returned permission denied. And it’s a good practice to try not to use -f while deleting a directory:

Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:

sudo rm -r /path/to/folderName 

FYI: you can use letters -f , -r , -v :

  • -f = to ignore non-existent files, never prompt
  • -r = to remove directories and their contents recursively
  • -v = to explain what is being done

In my humble opinion, it’s a good practice never to add the «f» on first attempt. Its purpose is to ignore certain warning prompts that may be important, especially if you’ve accidentally done it on/from the wrong directory. In my opinion it’s good to try without the «f» first, then only if you are encountering a lot of warning prompts and you’re sure it’s OK to ignore them all, Ctrl+C out of it and repeat the command with the «f».

@thomasrutter . Agree. A file «xxx» owner: root and group: root can BE deleted with the -f switch; and without sudo. This is the message without -f: «rm: remove write-protected regular file ‘/home/william/.cache/netbeans/v08.01/tmp/xxx’? _». _Tread gently.

However, you need to be careful with a recursive command like this, as it’s easy to accidentally delete a lot more than you intended.

It is a good idea to always double-check which directory you’re in, and whether you typed the command correctly, before pressing Enter.

Читайте также:  Linux восстановить при загрузке

Safer version

Adding -i makes it a little safer, because it will prompt you on every deletion. However, if you are deleting many files this is not going to be very practical. Still, you can try this first.

Many people suggest using -f (combining it into -Rf or -rf ), claiming that it gets rid of annoying prompts. However, in normal cases you don’t need it, and using it suppresses some problems that you probably do want to know about. When you use it, you won’t be warned if your arguments supply a non-existing directory or file(s): rm will just silently fail to delete anything. As a general rule, try first without the -f : if there are problems with your arguments, then you’ll notice. If you start getting too many prompts about files without write access, then you can try it with -f . Alternatively, run the command from a user (or the superuser using sudo) that has full permissions to the files and directories you’re deleting to prevent these prompts in the first place.

Источник

Delete Non-Empty Directory In Linux – “rmdir directory not empty” Solution

Delete Non-Empty Directory In Linux -

The rmdir command is used to remove directories in Linux operating system. But when we try to use the rmdir command to remove the specified directory we get the “rmdir directory not empty” error. In order to remove a non-empty directory, what should I do? Can I use the rmdir in order to remove a directory with some folders or files?

The short answer is yes there is solution about the rmdir directory not emptyy” error but it is not with the rmdir command.

Delete Non-empty Directory with rm Command

rmdir and rm are similar commands but the rm provides more features and capabilities where rmdir only used to delete empty directories or folders. The rm command can be used to delete non-empty directories with some options. The most crucial option to remove a non-empty directory is the -r option which means recursive remove. Recursive remove means delete given directories and subdirectories with their files. Also the -f option can be provided to force the removal of some minor warnings.

Читайте также:  Linux kernel по русски

Alternatively the -r and -f options can be specified like below.

Delete Non-empty Directory with Confirmation

By default the “rm -rf” command deletes or removes the given directory and all its contents without asking any question or confirmation. This can be very harsh in some cases. We can delete files and folders in a more secure way by using confirmations for every deleted file or directory. The -i option is used for confirmation of the given directory and file delete.

The output will be like below. We can see that for every removal or deletion a confirmation is required which can be provided with “y” or “yes“.

rm: descend into directory 'nmap-7.91'? y rm: remove regular file 'nmap-7.91/FingerPrintResults.cc'? y rm: remove regular file 'nmap-7.91/nse_openssl.cc'? y rm: remove regular file 'nmap-7.91/NmapOps.cc'? y rm: remove regular file 'nmap-7.91/TargetGroup.cc'? y rm: remove regular file 'nmap-7.91/nse_nsock.cc'? y rm: remove regular file 'nmap-7.91/portreasons.h'? y rm: remove regular file 'nmap-7.91/timing.cc'? y rm: descend into directory 'nmap-7.91/liblinear'? y rm: remove regular file 'nmap-7.91/liblinear/tron.cpp'? y rm: remove regular file 'nmap-7.91/liblinear/predict.c'? y rm: remove regular file 'nmap-7.91/liblinear/README'? y rm: remove regular file 'nmap-7.91/liblinear/train.c'? y rm: remove regular file 'nmap-7.91/liblinear/COPYRIGHT'? y rm: remove regular file 'nmap-7.91/liblinear/tron.h'? y rm: remove regular file 'nmap-7.91/liblinear/Makefile.win'? y rm: remove regular file 'nmap-7.91/liblinear/linear.cpp'? y rm: remove regular file 'nmap-7.91/liblinear/Makefile'? y rm: remove regular file 'nmap-7.91/liblinear/liblinear.vcxproj'? y rm: remove regular file 'nmap-7.91/liblinear/linear.h'? y rm: remove regular file 'nmap-7.91/liblinear/linear.def'? y rm: descend into directory 'nmap-7.91/liblinear/blas'? y

Delete Non-empty Directory with Full/Absolute Path

Also, the non-empty directories can be provided as a full or absolute path. This is a more secure way to delete non-empty directories because the path is absolute and can not specify with the different directory.

The deleted non-empty directories can be also printed with the -v option. The -v option is the short form of the verbose mode.

removed directory 'nmap-7.91/liblinear/blas' removed directory 'nmap-7.91/liblinear' removed 'nmap-7.91/nmap-protocols' removed 'nmap-7.91/nmap.h' removed 'nmap-7.91/nse_utility.h' removed 'nmap-7.91/CHANGELOG' removed 'nmap-7.91/nse_utility.cc' removed 'nmap-7.91/checklibs.sh' removed 'nmap-7.91/missing' removed 'nmap-7.91/nse_main.cc' removed 'nmap-7.91/service_scan.cc' removed 'nmap-7.91/INSTALL' removed 'nmap-7.91/NewTargets.h' removed 'nmap-7.91/.lgtm.yml' removed 'nmap-7.91/nse_fs.cc' removed 'nmap-7.91/nbase/nbase_addrset.c' removed 'nmap-7.91/nbase/CHANGELOG' removed 'nmap-7.91/nbase/nbase_ipv6.h' removed 'nmap-7.91/nbase/nbase_winunix.h' removed 'nmap-7.91/nbase/snprintf.c' removed 'nmap-7.91/nbase/nbase_str.c' removed 'nmap-7.91/nbase/nbase_winconfig.h' removed 'nmap-7.91/nbase/nbase_time.c' removed 'nmap-7.91/nbase/configure' removed 'nmap-7.91/nbase/nbase_winunix.c' removed 'nmap-7.91/nbase/nbase_rnd.c' removed 'nmap-7.91/nbase/test/nmakefile' removed 'nmap-7.91/nbase/test/test-escape_windows_command_arg.c' removed directory 'nmap-7.91/nbase/test' removed 'nmap-7.91/nbase/inet_ntop.c' removed 'nmap-7.91/nbase/configure.ac'

Источник

Оцените статью
Adblock
detector