Answers:
尝试:
find /var/myfolder -type f -delete
这会将所有常规文件放在/ var / myfolder下,并将其删除,仅保留目录。
-type f
!=! -type d
对于zsh,请使用.
glob限定符仅匹配常规文件:
rm -- **/*(.)
这将递归删除当前目录及其子目录中的所有(非隐藏)常规文件。添加D
glob限定符也可以删除隐藏的常规文件(以及隐藏目录中的常规文件)。
find . -depth -exec rm {} + 2>/dev/null
rm
不会删除目录-因此只需在所有内容上运行它即可。
要保留目录的符号链接:
find . ! -type d -exec sh -c '
for f do [ -d "$f" ] ||
set "$@" "$f"; shift
done; rm "$@"' sh {} +
我认为,如果有很多文件需要在上述版本中进行测试,那么这也许也可以以一种稍微优化的方式工作:
find . ! \( -type l -o -type d \) \
-exec rm {} + -o -exec sh -c '
for f do [ -d "$f" ] ||
unlink "$f";done' sh {} +
find
提供了一个-xtype
选项。
# This will delete all directory contents, including hidden files and
# subdirectories, without deleting the directory itself
# With GNU find:
find /path/to/directoryToEmpty -mindepth 1 -delete
# OpenBSD (and probably other BSDs)
find /path/to/directoryToEmpty -mindepth 1 -depth -exec rm -f {} \;
# To see what it deletes, in the order it will delete it:
find /path/to/directoryToEmpty -mindepth 1 -depth -print
您还应该能够将cd转到有问题的目录树的顶部,然后运行:
find . -type f -print0 | xargs -0 rm -rf
并且应该删除所有文件,同时保持目录树完整。如果您确信没有包含空格的任何文件或目录名称,则可以忽略-print0和-0选项。
find命令可区分文件(-type f)和链接(-type l),因此应保留链接(如果有)完整。虽然没有测试。如有疑问,请运行:
找 。型l
并在运行之前的find命令之前查看是否有任何显示。
rm
国旗如何-r, -R, --recursive - remove directories and their contents recursively
适应吗?