Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux is it possible to empty the contents of all files in a directory [closed]

Tags:

linux

Is it possible in a single command (no loop) to clear the contents of each file in a directory?

like image 587
Marty Wallace Avatar asked Mar 16 '13 00:03

Marty Wallace


2 Answers

Use truncate:

truncate -s 0 directory/* &> /dev/null
like image 186
perreal Avatar answered Oct 03 '22 02:10

perreal


This is ugly as hell, but it works:

find . -type f -exec sh -c 'echo -n "" > $1' sh {} \;

This will clear every file in every subdirectory.

To just clear the files in the current directory:

for i in *; do cat /dev/null > $i; done

(Yes, it's a loop, but it's one line.)

like image 23
nneonneo Avatar answered Oct 03 '22 02:10

nneonneo