如何删除所有与其父文件夹同名的文件夹?

如何删除所有与其父文件夹同名的文件夹?

不知怎的,我得到了里面每个文件夹的副本。

我可以运行什么 shell 命令来删除同名文件夹内的所有文件夹?

问题截图

lnp_eventbrite例如,在上面的例子中,从和的父级运行命令libraries,仅删除其自身内的lnp_eventbritelibraries文件夹。

答案1

tcsh可以这样做:

foreach i (*)
   if ( -e "$i/$i" ) rm -rf "$i/$i"
end

答案2

如果要删除匹配的子目录,则需要确保 1) 子目录为空 2) 子目录与其父目录同名。并且需要从最深层开始(例如:A/A/A/A/A/:从最深层开始。

以下是在 bash + grep + find 中执行此操作的一种方法(非常基本,因为我在旧机器上进行了测试):

recdeldir ()
{
    _tab_="`printf '\011'`";
    find . -type d -depth -print | grep -v '^\.$' | while IFS= read dir; do
        echo "======= $dir =============";
        pushd "${dir}";
        if [ -d "../../`basename ${dir}`" ]; then
            echo "Matching subdir: `pwd`";
            if ( find . -print | grep -v '^\.$' | wc -c | grep "[ ${_tab_}]*0$" >/dev/null ); then
                echo "empty, deleting `pwd`/";
                cd ..;
                rmdir "`basename ${dir}`";
            else
                echo "`pwd`/ is not empty. doing nothing.";
            fi;
        else
            echo "Not matching subdir : `pwd`";
        fi;
        popd;
    done
}

下面是该函数的使用示例:我创建了一个/tmp/DIR,并在其中创建了一个小测试用例(其中有几个同名的目录,一些以“。”开头,在其中一些目录中我添加了其他文件以确保这些文件不会被删除并被考虑在内,等等。

换句话说,我确保在该脚本中,如果子目录不为空,则不会删除与父目录名称匹配的子目录,以确保我们不会删除不应删除的内容(我相信这符合提问者的愿望)。如果我们应该删除而不考虑内容/是否为空,则应进行修改,使其不关心“空/不空”的区别。

在你的 bash 会话中,只需复制/粘贴上述函数即可在你当前的 shell 中创建它(或将其添加到你将要获取的文件中,或添加到你的 bashrc 中),然后你就可以使用它了:

然后我 cd 进入/tmp/DIR,并启动:recdeldir

bash-2.05b$ find . -ls
 8192    1 drwxrwxr-x  5 user      group          256 Dec 18 12:03 .
 8193    1 drwxrwxr-x  3 user      group          256 Dec 18 11:31 ./tre
 8194    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsd
 8195    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsdq
 8196    1 drwxrwxr-x  2 user      group          256 Dec 18 12:10 ./tre/tre
 8199    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/tre/i
 8200    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.O
 8203    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.O/.O
 8204    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.O/.O/.O
 8205    0 -rw-rw-r--  1 user      group            0 Dec 18 12:11 ./.O/.something
 8201    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.OOO
 8198    1 drwxrwxr-x  3 user      group          256 Dec 18 12:11 ./.OOO/.OOO
 8202    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.OOO/.OOO/.OOO
 8197    1 -rw-rw-r--  1 user      group          967 Dec 18 12:11 ./result
bash-2.05b$ recdeldir
======= ./tre/tre =============
/tmp/DIR/tre/tre /tmp/DIR
Matching subdir: /tmp/DIR/tre/tre
/tmp/DIR/tre/tre/ is not empty. doing nothing.
/tmp/DIR
======= ./tre =============
/tmp/DIR/tre /tmp/DIR
Not matching subdir : /tmp/DIR/tre
/tmp/DIR
======= ./.O/.O/.O =============
/tmp/DIR/.O/.O/.O /tmp/DIR
Matching subdir: /tmp/DIR/.O/.O/.O
empty, deleting /tmp/DIR/.O/.O/.O/
/tmp/DIR
======= ./.O/.O =============
/tmp/DIR/.O/.O /tmp/DIR
Matching subdir: /tmp/DIR/.O/.O
empty, deleting /tmp/DIR/.O/.O/
/tmp/DIR
======= ./.O =============
/tmp/DIR/.O /tmp/DIR
Not matching subdir : /tmp/DIR/.O
/tmp/DIR
======= ./.OOO/.OOO/.OOO =============
/tmp/DIR/.OOO/.OOO/.OOO /tmp/DIR
Matching subdir: /tmp/DIR/.OOO/.OOO/.OOO
empty, deleting /tmp/DIR/.OOO/.OOO/.OOO/
/tmp/DIR
======= ./.OOO/.OOO =============
/tmp/DIR/.OOO/.OOO /tmp/DIR
Matching subdir: /tmp/DIR/.OOO/.OOO
empty, deleting /tmp/DIR/.OOO/.OOO/
/tmp/DIR
======= ./.OOO =============
/tmp/DIR/.OOO /tmp/DIR
Not matching subdir : /tmp/DIR/.OOO
/tmp/DIR
bash-2.05b$ find . -ls
 8192    1 drwxrwxr-x  5 user      group          256 Dec 18 12:03 .
 8193    1 drwxrwxr-x  3 user      group          256 Dec 18 11:31 ./tre
 8194    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsd
 8195    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/sdfsdq
 8196    1 drwxrwxr-x  2 user      group          256 Dec 18 12:10 ./tre/tre
 8199    0 -rw-rw-r--  1 user      group            0 Dec 18 11:31 ./tre/tre/i
 8200    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.O
 8205    0 -rw-rw-r--  1 user      group            0 Dec 18 12:11 ./.O/.something
 8201    1 drwxrwxr-x  2 user      group          256 Dec 18 12:11 ./.OOO
 8197    1 -rw-rw-r--  1 user      group          967 Dec 18 12:11 ./result
bash-2.05b$

相关内容