递归清空多个文件的命令 [英] Command to empty many files recursively

查看:10
本文介绍了递归清空多个文件的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想递归地清除给定目录的许多日志文件的内容,而不删除每个文件.一个简单的命令可以做到吗?

I would like to clear the content of many log files of a given directory recursively, without deleting every file. Is that possible with a simple command?

我知道我可以做到>logs/logfile.log 一个一个,但是那个文件夹里有很多日志,这不是一目了然.

I know that I can do > logs/logfile.log one by one, but there are lots of logs in that folder, and that is not straightforward.

顺便说一下,我正在使用 macOS Sierra.

I am using macOS Sierra by the way.

推荐答案

感谢@chepner 向我展示了防止文件名中出现双引号的更好方法:

Thanks to @chepner for showing me the better way to protect against double quotes in the file names:

你可以用find来做

find start_dir -type f -exec sh -c '> "$1"' _ {} ;

如果你不想要所有文件,你可以添加额外的限制,比如如果你只想要以 .log 结尾的文件,你可以这样做

And you could add extra restrictions if you don't want all files, like if you want only files ending in .log you could do

find start_dir -type f -name '*.log' -exec sh -c '> "$1"' _ {} ;

这篇关于递归清空多个文件的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆