LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Overwriting a given file with "0" (https://www.linuxquestions.org/questions/linux-newbie-8/overwriting-a-given-file-with-0-a-836454/)

adityavpratap 10-05-2010 11:39 PM

Overwriting a given file with "0"
 
Hi,
I want to overwrite a set of files with the character "0" in order to delete their contents. For individual files I can do -
Code:

$ echo "0" > /path/to/file
However for multiple files in a particular directory if I try the following -
Code:

$ find . -type f -name "*\.log" -exec echo "0" > {} \;
the files are not getting overwritten. Any suggestions?

bigrigdriver 10-06-2010 12:06 AM

You can also overwrite a file with 0 this way: /dev/zero > /path/to/file.

Or, overwrite it with nothing: /dev/null > /path/to/file (leave an empty file).

So, maybe this would work: find . -type f -name "*\.log" -exec /dev/zero > {} \;

adityavpratap 10-06-2010 12:42 AM

Hi
Thanks for the prompt reply!
/dev/null > /path/to/file doesn't seem to be working on my system. So I tried -
cp /dev/null > /path/to/file
This time it worked.
So I did
$ find . -type f -name "*\.log" -exec cp /dev/null {} \;
Thanks once again,:-)

Dark_Helmet 10-06-2010 12:46 AM

You can't do what you're trying to do because your command is ambiguous. Exactly which command does the '>' apply to? You want it to apply to the echo. The shell is applying it to the find command.

To accomplish what you want, you should create a simple script. Something like:
Code:

#!/bin/bash

echo "0" > "${1}"

name the script whatever you like, make it executable, and then reformat your find command to something like:
Code:

find . -type f -name "*\.log" -exec myscript.bash {} \;
EDIT: Or, as you posted, change the command to use some way other than output redirection.

adityavpratap 10-06-2010 01:03 AM

Thanks Dark_helmet,
I tried out your suggestion and created a script to echo the "0" to the name entered as the first argument. Then used that script (after chmod +x) with the find command. And it worked.


All times are GMT -5. The time now is 10:09 AM.