Directory not empty linux как удалить
Перейти к содержимому

Directory not empty 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.

Table of Contents

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 <options> <file> . 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.

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 <drive letter> /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:

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:

  • Instagram
  • LinkedIn

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.

Related Posts

how to change boot order on linux

How to Change Boot Order on Linux

make-command-not-found

How to Fix “Make: Command Not Found” Error

linux terminal for windows

How to Install Linux Terminal on Windows

How-to-force-Quit-an-app-on-Linux

How to Force Quit an App on Linux

best linux for virtualbox

8 Best Linux Distros to Try in VirtualBox

ifconfig command not found

How To Fix “ifconfig Command Not Found” Error?

2 Comments

I have a Perl script that I am running on a Linux system. The script does some simple analysis, and then gathers all of the data up, moving it into a final output folder. The script has a cleanup section at the end, which is meant to remove all the running/temp directories that were created throughout the analysis. The lines I have in that cleanup section that are causing errors are as follows:
`rm -rf $CaseName` if (!$Debug );
chdir( ” ..” );
`rm -rf $TempFolder` if (!$Debug );

The folder structure, from the root directory where the script is executed from, is:
$TempFolder/$CaseName
(the script creates the above folders during the analysis. The analysis is actually done within the $CaseName folder)

So essentially, I am trying to clean up/remove the contents of $CaseName (which lives within $TempFolder), step up one directory, and remove $TempFolder.

When I execute the script and it runs as it’s supposed to, I get errors during this cleanup portion saying that it cannot rm the folder because it is not empty (despite me having the -rf flag in there, which should force it to remove the folder anyway). But when I put a die statement right before that cleanup portion, and then execute each of the above commands manually on the command line, everything works out fine and the folders are removed as intended.

Do you have any suggestions or ideas on what my issue might be here?

Hi Austin,
The cleanup section seems fine. First, I recommend checking the things mentioned in the article if you haven’t yet done so (sudo, fsck, NAS shares, etc.). You could also run lsof /path/to/dir or lsof | grep «directory name» to check if any processes are running from said directory.

Use the below command :

Wajeeh Ahsan

It deletes all files and folders contained in the folderName 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:

"cannot remove 'some_directory': Directory not empty"

When I try to rm .fuse_hidden0001d4bf00000006 , it is deleted, but another new .fuse_hidden0001d4bf00000007 created.

So I wonder what happened, and how to fix this problem?

Note: it is a newly bought external portable HDD, and I just copy some files to it using a data recovery program.

Tim's user avatar

5 Answers 5

Files of the form .fuse_hidden* are created by FUSE filesystems when a file is deleted but still in use somewhere and must still have a directory entry. This is similar to .nfs* files on directories exported over NFS.

Run df -T . to see the type of filesystem that’s mounted on the current directory and its mount point. For an external hard disk, chances are that this is an NTFS filesystem mounted through the NTFS-3G driver, which is base on FUSE.

The name is a fake name that the filesystem driver invents for a deleted file. You can’t delete the file (or rather, if you create the file, it reappears under another name). You can’t delete the directory either, since it isn’t empty. You’ll need to find what is using this file. The most likely cause of being in use is if it’s open by some application. Run lsof /media/mount-point where /media/mount-point is the filesystem mount point and look for an open file in that directory.

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.

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.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *