Reputation: 1264
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?
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.
I am using macOS Sierra by the way.
Upvotes: 2
Views: 699
Reputation: 208077
As macOS includes Perl anyway:
perl -e 'for(<logs/*log>){truncate $_,0}'
Or, more succinctly, if you use homebrew and you have installed GNU Parallel (which is just a Perl script), you can do:
parallel '>' ::: logs/*log
Upvotes: 1
Reputation: 14520
Thanks to @chepner for showing me the better way to protect against double quotes in the file names:
You can use find
to do it
find start_dir -type f -exec sh -c '> "$1"' _ {} \;
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"' _ {} \;
Upvotes: 4