Remove not empty folder linux

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.

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.

Читайте также:  Установка тонкого клиента 1с linux

Источник

How to Fix “Rmdir: Directory Not Empty” Error in Linux

rmdir directory not empty

The rmdir command is used to remove empty directories. If you attempt to remove a non-empty directory with this command, you’ll encounter the Rmdir Directory Not Empty error.

That much is intended behavior, but some users have reported cases where they faced this error despite using rmdir on an empty directory. This happens due to reasons like file system errors or broken symbolic links.

We’ve detailed various ways to solve or work around this error on both Linux and Windows systems in the sections below.

Fix Rmdir Directory Not Empty Error on Linux

The easiest fix is to use the rm command instead of rmdir . If this workaround doesn’t appeal to you, or the rm command doesn’t work either, the rest of the solutions will be helpful.

Use rm Command

The base syntax for the rm command is rm . The -d flag removes the directory if it’s empty. But in the case of non-empty directories, you can use the -r flag to recursively delete the specified directory and its contents.

  • For instance, here’s a basic example:
    rm -r directory
    rm-r-remove-directory
  • To force delete a directory, add the -f flag as such:
    rm -rf directory
  • If your directory path or name contains spaces, use quotes “” as such:
    rm -rf “directory”
  • Add sudo to resolve any permission issues as such:
    sudo rm -rf directory
    sudo-rm-rf

Finally, make sure you don’t use sudo rm -rf / as this would cause the contents of the root directory, i.e., every mounted filesystem, to be deleted.

Delete via GUI

When things don’t work in the GUI, we turn to the command line. As unusual as this may sound, things are the opposite in this case. Numerous users have reported that deleting the folder via the File Browser worked for them. It’s worth trying this before checking out the other solutions.

move-to-trash

Check File System

File system errors are a common reason for file deletion and similar issues. As such, using the fsck (File System Consistency Check) utility can be helpful. Here are the necessary steps for this:

sudo-fsck

  1. Use the df -h command and note the file system on which you want to run fsck .
  2. Let’s take /dev/sda4 as an example. Use the sudo umount /dev/sda4 command to unmount it. If it’s a root filesystem, you’ll need to boot from a live CD to be able to unmount it.
  3. Next, use sudo fsck /dev/sda4 and accept the prompts to approve the repair actions.
  4. After the repair is complete, use the sudo mount /dev/sda4 command to remount the device and check if the issue is resolved now.

Delete from Fileserver

There have been cases where the directory couldn’t be deleted because it was part of a filesystem mounted with CIFS (Samba). As there was a file that was a broken symbolic link, ls failed to mention it.

Over CIFS, you wouldn’t be able to see this and thus could be mistakenly assuming that the directory is empty. In such cases, you would need to delete the file directly from the fileserver.

Читайте также:  Создать пользователя linux команда

Node Fixes

Users have reported cases where the directory was open in a node.js app or where a node server was periodically accessing the files in the directory, causing minor meta files to exist inside the directory constantly.

In such cases, shutting down the application or server that’s accessing the directory at the moment would resolve the issue.

Fix Rmdir Directory Not Empty on Windows

While the application slightly differs, the idea behind fixing this error on Windows is very similar to the methods listed for Linux.

Resolve Common Issues

First, make sure no process is currently accessing the directory. Also, make sure you’re not trying to delete the directory you’re currently on, as that would also prompt this error. In such cases, change to a different directory before trying this.

Using the CHKDSK utility to resolve any file system issues has fixed this error for numerous errors. You can do the same with the chkdsk /f /r /x command.

chkdsk-f-r-x

Work Around the Error

Rmdir’s /s flag is meant to let you delete the specified directory and all the subdirectories and files. You can try a couple of things in cases where this doesn’t work as intended.

You could delete the contents of the directory before deleting the directory itself as such:

del /f /s /q directory 1>nul rmdir /s /q directory

Alternatively, you could also try repeating the operation as shown below. Assuming there aren’t any permission issues, you should be able to delete the directory:

rmdir /s /q c:\directory
if exist c:\directory rmdir /s /q c:\directory

Anup Thapa primarily covers Windows systems, networking, and computer hardware at TechNewsToday. Anup has been writing professionally for almost 5 years, and tinkering with PCs for much longer. His love for all things tech started when he got his first PC over 15 years ago. It was a Pentium IV system running Windows XP on a single 256 MB stick. He spent his formative years glued to this PC, troubleshooting any hardware or software problems he encountered by himself. Professionally, Anup has had brief forays into a variety of fields from coding and hardware installation to writing. In doing so, he’s worked with people of different backgrounds and skill levels, from average joes to industry leaders and experts. This has given him not just a versatile skill set, but also a unique perspective for writing that enables him to concisely communicate complex information and solve his reader’s problems efficiently. You can reach out to him at anup@technewstoday.com.

Источник

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.

Читайте также:  Linux fingerprint scanner driver

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.

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

Источник

Why am i getting «Directory not empty» error in terminal when using rmdir?

I am a walking through a tutorial and it lets me delete two directories(im using one 10 deep, all empty) but once i try to remove the third it gives me that error message, even though there is not content in the directory and i am in the directory above it. Why is this? By the way i am using terminal.

Go into the directory in question and try typing «ls -a» to see if there are any hidden files or folders (ls -a » would also work).

Im not sure what this is but this is what that command returned . .. .DS_Store i am guessing this is a hidden file?

Correct. The first two are links to the current directory and one above it respectively, and .DS_Store is a Mac file that has to do with indexing I think. In any case, you can remove DS_Store safely, and the other two shouldn’t cause you problems.

Please accept the answer (or write an answer of your own and accept that) so that this doesn’t show up as unresolved. Thanks.

3 Answers 3

That error is reported when the directory is not empty.

To find out what files are in that directory use ls -a . The -a flag tells ls to list hidden files (aka «dot files»). Remove those files then rmdir can be used.

Another option is to simply use rm -rf to recursively delete the directory and all of its files. NOTE: this can be dangerous if you give the wrong parameters — resulting in deleting more than you intended.

Trying to delete the Library folder from an old drive, this procedure fails. Thoughts? guido@iMac guido % ls -rla Library/Application\ Support/icdd total 0 drwx—— 3 guido staff 102 May 12 09:32 .. drwx—— 3 guido staff 102 May 12 09:29 . guido@iMac guido % sudo rmdir !$ sudo rmdir Library/Application\ Support/icdd rmdir: Library/Application Support/icdd: Directory not empty guido@iMac guido % paul@Pauls-iMac paul % sudo rm -rf Library/Application\ Support/icdd rm: Library/Application Support/icdd: Permission denied

Источник

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