删除所有文件,但把所有的目录在bash脚本? [英] Delete all files but keep all directories in a bash script?
问题描述
我试图做一些这可能是很简单的,我有一个目录结构如:
DIR /
subdir1 /
subdir2 /
文件1
文件2
subsubdir1 /
文件3
我想运行一个bash脚本将从下来目录递归删除所有文件的命令,但保留所有目录。即:
DIR /
subdir1 /
subdir2 /
subsubdir1
什么是合适的命令呢?
找到DIR型的F -print0 | xargs的-0 RM
找到
列出匹配给定的目录中的部分前pression的所有文件,递归。 型的F
匹配常规文件。 -print0
是用打印出的名称 \\ 0
作为分隔符(任何其他字符,包括 \\ n
,可能是一个路径名)。 的xargs
是从标准输入收集文件名,并把它们作为一个参数。 -0
是要确保的xargs
就会明白一起的 \\ 0
分隔符。
的xargs
是明智调用 RM
多次,如果参数列表中会得到太大。因此它比试图调用某物好得多。像 RM $((...找到)
。此外,它比调用更快 RM
通过自身的每个文件,像找... -exec RM \\ {\\}
。
I'm trying to do something which is probably very simple, I have a directory structure such as:
dir/
subdir1/
subdir2/
file1
file2
subsubdir1/
file3
I would like to run a command in a bash script that will delete all files recursively from dir on down, but leave all directories. Ie:
dir/
subdir1/
subdir2/
subsubdir1
What would be a suitable command for this?
find dir -type f -print0 | xargs -0 rm
find
lists all files that match certain expression in a given directory, recursively. -type f
matches regular files. -print0
is for printing out names using \0
as delimiter (as any other character, including \n
, might be in a path name). xargs
is for gathering the file names from standard input and putting them as a parameters. -0
is to make sure xargs
will understand the \0
delimiter.
xargs
is wise enough to call rm
multiple times if the parameter list would get too big. So it is much better than trying to call sth. like rm $((find ...)
. Also it much faster than calling rm
for each file by itself, like find ... -exec rm \{\}
.
这篇关于删除所有文件,但把所有的目录在bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!