Recursively clear files
Hi,
I was trying to find a simple way of starting in a directory and recursively clearing every file. Not deleting the file, but clearing it's contents. Working on some projects, and need to clear/reset log files. I was trying th efind command, but couldn't get it to execute the command i needed. If i'm in the directory, this works, but I need to go down into the subdirecotries and do the same for all files found. Code:
NODELOGFILES='$NODELOGS/test/*' Code:
find ./ -name '*' -type f -execdir "> '{}'" \; -print Code:
find: `> \'./sar22\'': No such file or directory Thanks, Carlos. |
Did you try rm -rf * in the directory you want?
BE carefull! |
The error message shows that it's looking for e.g. './sar22' (including the quotes).
What if you change it to the below? Not tested, so it might totally not work! Code:
find ./ -name "*" -type f -execdir "> {}" \; -print |
OK,
Some time to test it and it did not work Below does work to delete and recreate the files Code:
find ./ -name "*" -type f -execdir rm {} \; -execdir touch {} \;-print |
eg:
Code:
find ... -exec cp /dev/null {} \; |
Uhm, couldn't you use logrotate?
Also, specifying "*" as name to search is the same as not specifying it, so you can remove the -name directive. |
Try using the
-depth command under find |
Quote:
Thanks, Carlos. |
Quote:
Thanks, Carlos |
Quote:
Thanks, Carlos. |
If file permissions are your concern, you could try to "cat /dev/null" inside your files, instead of using cp.
|
If you want to empty an existing file, but preserve the file's permission state and ownership, this should work
Code:
echo -ne > theFile |
When in find, redirection is a bit tricky:
Code:
bad: find ... -exec echo -n >{} \; |
find is not a shell, so its -exec option cannot directly execute shell syntax and functions. That's why the redirections don't work.
You can do it by launching a separate shell for the command, as explained in this link. How do I invoke a shell command from a non-shell application? http://mywiki.wooledge.org/BashFAQ/012 Another way to do it is to process the list with a while loop. Use null separators to be completely safe. Code:
while IFS='' read -r -d '' fname; do Here are a few more BashFAQ entries that may relate to this topic: How can I read a file (data stream, variable) line-by-line (and/or field-by-field)? http://mywiki.wooledge.org/BashFAQ/001 How can I find and deal with file names containing newlines, spaces or both? http://mywiki.wooledge.org/BashFAQ/020 I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read? http://mywiki.wooledge.org/BashFAQ/024 How do I use 'find'? I can't understand the man page at all! http://mywiki.wooledge.org/BashFAQ/075 |
All times are GMT -5. The time now is 07:09 PM. |