rm: cannot remove `/usr/local/tmp/': Directory not empty

来源:互联网 发布:淘宝店铺图标大全 编辑:程序博客网 时间:2024/05/22 08:08

问题

删除目录是遇到错误提示:

rm: cannot remove `/usr/local/tmp/’: Directory not empty

即使使用rm -rf ,还是会出现相同提示。

原因

要删除的目录下有文件正在被使用。

验证

情况1:

创建tmp目录,并在其下创建文件test.txt

$ mkdir tmp$ echo “123” > tmp/test.txt

删除目录,非常顺利

$ rm -rf tmp/

情况2:

接着看,如果有文件被占用的情况。

写一段占用文件的代码如下。

#include <stdio.h>#include <string.h>#include <unistd.h>int main(){        int i = 0;        char buffer[10] = {0};        while(1)        {                FILE *fp = fopen("tmp/test.txt", "w");                if (fp == NULL)                {                        printf("open failed.\n");                        return -1;                }                memset(buffer,0,10);                sprintf(buffer,"%d\n", i);                fputs(buffer, fp);        fflush(fp);                fclose(fp);                sleep(0.5);                ++i;        }        return 0;}

编译,执行

$ gcc test.c -o test$ ./test &[1] 16293

该程序打开文件,写入内容,关闭文件,一直循环,保持占用文件。

尝试删除目录,此时得到错误提示

$ rm -rf tmp/rm: cannot remove `tmp': Directory not empty

由此证明,当目录下有文件正在被使用时,如果要删除目录,会得到错误提示的问题。

0 0
原创粉丝点击