linux命令清空目录的所有文件

Lui*_*cía 13 linux command file

我想清空目录中的所有文件.我试过这个:

find myFolderPath/* -exec cat /dev/null > {} ';'
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我该怎么做?

谢谢.

dog*_*ane 30

您不能直接使用重定向(>),find -exec因为它在命令运行之前发生并创建一个名为的文件{}.为了解决这个问题,你需要在新的shell中使用它sh -c.

另请注意,您无需cat /dev/null > file为了破坏文件.你可以简单地使用> file.

试试这个:

find . -type f -exec sh -c '>"{}"' \;
Run Code Online (Sandbox Code Playgroud)

  • 我找到了。-类型f -exec sh -c'>“ {}”'\; 更好地工作。 (2认同)

lyn*_*nks 10

这将做你想要的:

for f in *; do >$f; done
Run Code Online (Sandbox Code Playgroud)