Ошибка directory not empty in

I’ve written a script that is mostly working for me. However, the two rmdir commands at the end are sometimes returning Directory not empty errors. How do I beat this? I tried adding r and then -rf which returned Illegal option errors.

I’m a rookie, and I’d be grateful for any help. Here is the full shell script, which, again, is mostly working beautifully:

if [[ -z "${1}" ]]; then
  die "FolderName Required"
fi

newDirName="DirectoryName"
newBaseDir="/Users/JSG/Desktop/DataFarm/$1/"
/bin/mkdir -p $newBaseDir/{ProtectedOrig,Data}

echo -n "---Data Folder Setup

---Data Introduction

---Data Audit/Manipulation

---Data Queries" > $newBaseDir/Data/$1_DataJournal.txt

ditto NewData/ NewDataCopy
fab deploy_data_to_s3:data=*
mv NewData/ $newBaseDir/ProtectedOrig/NewData
mv NewDataCopy/ $newBaseDir/Data/NewDataCopy 
mv $newBaseDir/Data/NewDataCopy/* $newBaseDir/Data/
rmdir $newBaseDir/Data/NewDataCopy
mv $newBaseDir/ProtectedOrig/NewData/* $newBaseDir/ProtectedOrig/
rmdir $newBaseDir/ProtectedOrig/NewData
chflags -R uchg $newBaseDir/ProtectedOrig/
mkdir NewData

What am I missing? And thanks in advance!

asked Oct 12, 2011 at 18:36

Jeff Severns Guntzel's user avatar

For the rmdir command, you need to add the --ignore-fail-on-non-empty flag so it deletes the directory even if files are in there like so:

rmdir --ignore-fail-on-non-empty $newBaseDir/Data/NewDataCopy

You could also just use rm -r too:

rm -r $newBaseDir/Data/NewDataCopy

From the Wikipedia Entry:

rmdir will not remove a directory if it is not empty in UNIX. The correct way to remove a directory and all its contents recursively is with the rm command.

answered Oct 12, 2011 at 18:48

Suhail Patel's user avatar

Suhail PatelSuhail Patel

13.6k2 gold badges43 silver badges49 bronze badges

2

Check for any files in the directory that start with .. I note you’re moving *, but if there’s a file called, for example, .hello, then * will not match this file and as a result the directory will not be empty when you come to do an rmdir.

answered Oct 12, 2011 at 18:48

Chris J's user avatar

0

I am experiencing the same issues after upgrading from 2.3 to 2.8. These are my dependencies:

"require": {
    "php": ">=5.5.0",
    "doctrine/data-fixtures": "1.1.*",
    "doctrine/doctrine-bundle": "1.6.*",
    "doctrine/doctrine-fixtures-bundle": "2.3.*",
    "doctrine/doctrine-migrations-bundle": "1.1.*",
    "doctrine/orm": "2.5.*",
    "guzzle/guzzle": "~3.9",
    "jms/di-extra-bundle": "1.6.*",
    "jms/security-extra-bundle": "1.5.*",
    "sensio/distribution-bundle": "4.0.*",
    "sensio/framework-extra-bundle": "3.0.*",
    "symfony/assetic-bundle": "2.7.*",
    "symfony/monolog-bundle": "2.8.*",
    "symfony/swiftmailer-bundle": "2.3.*",
    "symfony/symfony": "2.8.*",
    "twig/extensions": "1.3.*"
}

On Windows x64 with PHP 5.6. Looks to me as if some cache filenames are absurdly long and cause issues. For example, I have a dev_old/annotations/f0/5b5b435d566973755265616c5c5765627365727669636542756e646c655c436f6e74726f6c6c65725c436f6e6e656374696f6e436f6e74726f6c6c657223757064617465456d61696c436f6e6e656374696f6e405b416e6e6f745d5d5b315d.doctrinecache.data file which I cannot even edit manually with Notepad anymore.

I have this empty directory, but I keep getting the following error message:

Cannot remove test: Directory not empty

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:
enter image description here

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.

αғsнιη's user avatar

αғsнιη

34.9k41 gold badges129 silver badges192 bronze badges

asked Dec 29, 2014 at 4:34

rbrick's user avatar

7

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:

  1. Right click on folder
  2. Move to trash
  3. Empty trash

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

David Foerster's user avatar

answered Feb 19, 2016 at 10:13

Marco Ottina's user avatar

9

I have win 10 + ubuntu dual system installed. And both systems share the windows parititions.

Recently, i also ran into unable to delete empty folders in those partitions under ubuntu. I can’t find out solution to solve it under linux.

However, after i switch to windows, and run

chkdsk

via cmd for the target disk. Some errors checked out.
and then i run

chkdsk /F

to fix disk error.

After it finish, i am able to delete those folders now.

answered Jul 12, 2017 at 2:31

e-cloud's user avatar

e-cloude-cloud

2312 silver badges8 bronze badges

2

I had the same issue not able to remove directory as it is not empty.

This sequence of operations worked for me.

  1. From command line first

    sudo rmdir  --ignore-fail-on-non-empty folder-name-to-be-deleted
    

The above command helps ubuntu ignore directory is not empty.

  1. Then just go to the folder and Shift + Del. That is all.

Anwar's user avatar

Anwar

75.4k31 gold badges191 silver badges308 bronze badges

answered Apr 15, 2017 at 3:58

Amit's user avatar

AmitAmit

911 silver badge2 bronze badges

2

You could delete it by typing sudo rm -rf {dir_name}. The directory might have been set to read-only permission. I hope the given command can delete the folder.

answered Dec 29, 2014 at 23:32

Wolverine's user avatar

WolverineWolverine

6544 silver badges15 bronze badges

4

If the directory is part of a filesystem mounted with CIFS (aka samba), and it contains a file that is a broken symbolic link, then ls fails to mention that file. (I observe this bug on a CIFS client running 14.04.2 LTS, and a server running 12.04.5 LTS.)

So the directory is not empty, but (over CIFS) you have no way to see that. The file can only be seen, and thus can only be deleted, by a command running on the fileserver hosting that filesystem.

answered Jun 19, 2015 at 20:10

Camille Goudeseune's user avatar

2

If you are using btrfs, it is possibly an empty directory with a non-zero i_size. You can check whether this is the case with:

stat -c %s test

The i_size of an empty folder in btrfs should be zero. In my case, I got 6160 with ~/.config/chromium/Default.

The suggested solution is to unmount the filesystem, run btrfs check to confirm the issue and check for other problematic directories, and finally run btrfs check --repair to fix. This operation is risky, though, so it’s a good idea to backup files first.

Source: Btrfs Problem FAQ

Zanna's user avatar

Zanna

68.9k56 gold badges215 silver badges327 bronze badges

answered Feb 14, 2016 at 9:29

wzhd's user avatar

wzhdwzhd

1461 silver badge5 bronze badges

GUI solution

  1. Move or cut & paste the folder to trash folder
  2. empty the trash

it is done.

Command-line solution

sudo mv folder_error/ .local/share/Trash

you can clear with trash-cli: trash-empty
or

sudo rm -fr ~/.local/share/Trash/*

David Foerster's user avatar

answered Jul 18, 2017 at 13:09

Kadir Y.'s user avatar

Kadir Y.Kadir Y.

4061 gold badge6 silver badges10 bronze badges

2

try this command:

sudo lsof | grep deleted 

Check in the list if your directory is still in use. :D

If so, stop the service and you will be able delete the directory.

Eric Carvalho's user avatar

Eric Carvalho

53.4k102 gold badges136 silver badges162 bronze badges

answered Dec 29, 2014 at 10:49

ashwin2011's user avatar

ashwin2011ashwin2011

591 silver badge4 bronze badges

1

This problem appears when those folders or files are not copied completely. It’s Input/Output Error. I tried to delete with Shift+Del or through commands, but these did not work. I tried right click and «Move to Trash» and it worked.

techraf's user avatar

techraf

3,30610 gold badges26 silver badges37 bronze badges

answered Apr 9, 2016 at 4:28

lhodeniz's user avatar

I had the same issue on Ubuntu 16.04 and I fixed it by:

  1. emptying the trash folder
  2. rebooting

Opening and closing the file manager did no good—only rebooting worked.

answered Aug 2, 2016 at 21:20

dale's user avatar

daledale

112 bronze badges

Check if directory is not active for any running application before deleting. I was unable to delete a directory with same message, I found out the reason was that the directory was open in an node.js application. After closing/exiting the application I was able to delete the folder.

answered Jan 18, 2020 at 23:42

Ajit Kumar's user avatar

In my case, I had a separate command that ran concurrently and added files while the delete was ongoing; likely rm finished deleting everything it had planned to delete, then tried to delete the folder at the end, saw it had contents, and then threw an error saying ‘yo, something is not right’.

(It had to do with running code in serial vs. accidentally in parallel for me.)

You may consider this if you’re dealing with this problem.

answered Jun 9, 2020 at 23:42

Kyle Baker's user avatar

Kyle BakerKyle Baker

3853 silver badges13 bronze badges

1

I ran into this issue and found that the solution was quite simple for me.

I had a Node server running that periodically accessed the files in the folder I was trying to remove. This caused several small meta files to constantly exist inside the folder. The purpose of these files is beyond me.

Even rm -rf folder/* would not remove these files permanently, despite showing the message removed folder/file.xyz.

I suspected the server, shut it down and removed the files and the folder without fail.

answered May 18, 2021 at 17:32

rasmniel's user avatar

2

Maybe the directory or a file in it has an immutable bit set?

root@lina:~# mkdir test
root@lina:~# chattr +i test
root@lina:~# rmdir test
rmdir: konnte 'test' nicht entfernen: Die Operation ist nicht erlaubt
root@lina:~# lsattr -d test
----i---------e---- test
root@lina:~# chattr -i test
root@lina:~# rmdir test

then you should remove the immutable bit with »chattr -i» first.

answered Sep 24, 2020 at 14:27

Ralph's user avatar

you may use rm command recursive flag i.e.

rm -r folder/

Example

answered Aug 13, 2021 at 14:15

Darkcheftar's user avatar

From the rm manual

       -d, --dir
              remove empty directories

So, to extend your example, try removing the directory with rm -drf test/

answered Oct 31, 2022 at 0:53

Michael Hall's user avatar

I solved a similar problem:

The actual error I got is:

find: cannot delete ‘/home/lucid/x/SecurityCameras/B-Yard’: Directory not empty

The command I used was:

find /home/lucid/x/SecurityCameras/B-Yard -mtime +15 -delete

The directory inside /home/lucid/x/SecurityCameras/B-Yard is 2021, which is put there by the camera.
Deleting OR renaming the directory to something else and then back to 2021 FIXED the problem. Renaming and back again for B-Yard didn’t do anything.
So just renaming the offending directory to something else, and then back to the original name fixes the problem. No need to delete anything.

Zanna's user avatar

Zanna

68.9k56 gold badges215 silver badges327 bronze badges

answered Dec 30, 2021 at 0:57

Norman's user avatar

0

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.

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.

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:

  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.
    sudo-fsck
  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:

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

mv (or move) is a Unix/Linux command that moves one or many files or directories from one place to another.

mv is usually used for two purposes:

  • It rename a file or folder.
  • It moves group of files to different directory.

mv syntax and usage should be straightforward. However, when you’re learning to use Linux for the first time or when you’ve come to Linux with a solid background in another operating system, you may run into some things that it doesn’t allow.

If you’ve ever received a Directory not empty when trying to move files or directories, then this guide can help you.

Why does “Directory not empty” happens?

This is one of the most common errors with mv. Usually, Directory not empty occured when you’re moving directories, and the target directory already contains files.

$ mv network_files/ tax_network
mv: cannot move <code>network_files/' to </code>tax_network/network_files': Directory not emptyCode language: HTML, XML (xml)

You can try --force trying to overwrite files and directories without explicit permission like this, but it won’t work either.

$ mv --force network_files/ tax_network
mv: cannot move <code>network_files/' to </code>tax_network/network_files': Directory not emptyCode language: HTML, XML (xml)

mv, as its name suggests, do one thing and one thing only : move files and directories (also rename them, which is another way of saying move).

This is mv‘s built-in protective mechanism to prevent you from accidentally use mv for purposes it isn’t built for. It refuses to work in this case because you’re merging contents of one directory to another.

How to move files/directories to a non-empty directory

Having known the cause of the error, we have several ways to do what we intended to do.

Method 1 : Using rsync

One of the best way is using rsync instead. rsync is built for copying files from source to destination, and have the ability to retain the attributes of many files and folders. Overall, rsync is better than cp because it only copies the files that is not present in the target directory instead of all files.

rsync is available in all major Linux distributions, including all recent versions of Ubuntu. If you don’t know which Ubuntu version is running on your machine, check out our guide on find Ubuntu version number.

Installing rsync on Ubuntu is easy and straightforward, just type in this command in the terminal :

sudo apt-get install rsync -yCode language: JavaScript (javascript)

Enter this command to your terminal to sync contents from source to target :

rsync -a source/ target/

After that, removes the source directory by :

rm -rf source/*

Method 2 : Using built-in commands

Instead of using rsync, you also can do this classical command which earns you more geek points. It’s quick and dirty, if you know what you are doing:

(cd backup && tar c .) | (cd backupArchives && tar xf -)

FAQ

Why does “Directory not empty” occurs?

Usually the message means that the target directory already contains files and writing to it could cause confusion.

How to move files safely in Linux without accidentally overwrite files?

Using rsync, you could use “rsync -a source/ target/”, which synchronize the contents of 2 directory.

Fastest way to move directories (or folders) in Linux?

Without installing any other program, you can use this command :
(cd backup && tar c .) | (cd backupArchives && tar xf -)
But be careful, you could be overwriting files without knowing it. Using rsync to do the job is safer and easier.

Понравилась статья? Поделить с друзьями:
  • Ошибка direct3d initialization error при запуске игры
  • Ошибка direct3d device has been removed or reset
  • Ошибка direct при запуске игры
  • Ошибка direct draw init failed 80004001
  • Ошибка different from the server