Linux remove not empty dir

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.

Читайте также:  Как определить дистрибутив линукс

Источник

Why do I get «Directory not empty» when I try to remove an empty directory?

enter image description here

I know this question has been asked plenty of times but none of them helped. I tried ls -la to make sure there were no hidden files, and there does not seem to be: I tried sudo rmdir test as well as sudo rm -rf test , and I just do not know what is wrong. I read it might be a problem with the file system, but I have no idea how I would go about fixing that.

Is it a normal directory or a mount point. If this is a mount point, it should be unmounted before deleting. If it is a normal directory, sometime there might be some open handles cause this problem. Reboot your machine once and try to delete it.

@rbrick Does test is your login user’s name? what is the output of echo $USER ? and what echo $HOME ?

17 Answers 17

I had the same problem on a external hard disk, I tried so many ways using command line, but I failed every time. That’s what worked for me:

Yes, it’s silly but it worked for me (I don’t really know how and why, but the damned folder no longer exist)

I got the same issue and your answer works. Not quite sure what the silly thing happened, and why «-rf» failed to remove an non-empty folder.

What happened? Honestly, i don’t know. Maybe, the folder’s file was corrupted for some reason: a flag like «is-empty» or the file counter like «how-many-file-inside» has been setted to an erroneous value. Sometimes, errors like this could be caused by damages on the hard disk. Why did it work? I don’t know how commands are implemented (think about C), but I guess they check those flags and counters before acting (i mean, if file counter >0 then «error: directory not empty» and abort), while emptying the trash directly remove the folder and all its file and subfolder without any kind of check.

In my case, the subdirectory in question happened to be in a folder that was shared from my Mac to a linux VM, and I was trying to delete the folder from the linux VM side. It seems that the Mac side was holding onto some folders. Doing the rm -rf from the Mac side worked.

Читайте также:  Wine mono astra linux установка

This just moves the corrupted folders from .Trash-1000/files to .Trash-1000/expunged where they remain undeletable.

Источник

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.

Читайте также:  Смена горячих клавиш смены языка linux

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.

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.

Источник

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