对于这种类型的dir结构:
/config/filegroups/filegroupA/files/fileA1.txt
/config/filegroups/filegroupA/files/fileA2.txt
/config/filegroups/filegroupB/files/fileB1.txt
/config/filegroups/filegroupB/files/fileB2.txt
...我知道我可以用rm -rf /config/filesgroups删除父文件夹和所有子文件夹.
但我只想删除/filegroupA、/filegroupB等,而不想删除/config/filegroups。
发布于 2013-08-16 08:12:56
rm -rf /config/filegroups/*如果您只想删除目录(以及指向目录的符号链接),而将/config/filegroups中的任何文件保持不变,则可以使用尾随斜杠:
rm -rf /config/filegroups/*/如果您也想删除名称以.开头的目录,假设您有一个最近的bash,您应该使用dotglob选项:
shopt -s dotglob
rm -rf /config/filegroups/*/
shopt -u dotglob发布于 2014-11-09 03:09:58
我更喜欢在find中使用-exec,这会使您的调用如下所示:
find /config/filegroups/ -maxdepth 1 -mindepth 1 -type d -exec rm -rf {} \;发布于 2013-08-16 02:08:39
这将删除/config/filegroups下的所有文件和目录,包括“隐藏”文件和目录(名称从.开始)。
find /config/filegroups -mindepth 1 -maxdepth 1 | xargs rm -rf如果文件或目录名包含空格,则必须这样做:
find /config/filegroups -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf奖励:您可以首先检查将被删除的内容如下:
find /config/filegroups -mindepth 1 -maxdepth 1如果您想保存某些文件或目录,可以这样做:
find /config/filegroups -mindepth 1 -maxdepth 1 -not -name "keep"https://unix.stackexchange.com/questions/86923
复制相似问题