I have a folder which has some text files and other folders in it which in turn have more text files in them. I need a command which will recursively loop over the folder and clear all the contents from all the files present in that directory and its subdirectories.How do I do this?
How to clear data from all files present in a directory?
files;io
You can do this with find:cd top_level_dirfind . -type f -exec bash -c echo -n '' > {} \;For every filename it invokes Bash (not very efficient) and echos, without newline because of the -n, essentially nothing to the file, thereby overwriting its contents and creating a zero length file.If you have a lot of files and/or have to this often a small (C/Python/Perl/Ruby) utility that reads from input a list of NUL terminated filenames and writes zero bytes to each of them would spead things up considerably ( find -type f -print0 | your_zeroing_util )You can e.g. use the following as your_zeroing_util:#!/usr/bin/env pythonimport sysinp = sys.stdin.read()while inp:   if '\0' in inp:      file_name, inp = inp.split('\0', 1)      # print(emptying [{}].format(file_name))      with open(file_name, 'w') as fp:         pass   inp += sys.stdin.read()