LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2010, 12:39 AM   #1
adityavpratap
Member
 
Registered: Dec 2004
Location: Hyderabad, India
Distribution: Slackware 13, Ubuntu 12.04
Posts: 431

Rep: Reputation: 32
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?
 
Old 10-06-2010, 01:06 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Jessie 8.4
Posts: 5,873

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
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 > {} \;
 
1 members found this post helpful.
Old 10-06-2010, 01:42 AM   #3
adityavpratap
Member
 
Registered: Dec 2004
Location: Hyderabad, India
Distribution: Slackware 13, Ubuntu 12.04
Posts: 431

Original Poster
Rep: Reputation: 32
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,:-)

Last edited by adityavpratap; 10-06-2010 at 01:44 AM.
 
Old 10-06-2010, 01:46 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.

Last edited by Dark_Helmet; 10-06-2010 at 01:47 AM.
 
1 members found this post helpful.
Old 10-06-2010, 02:03 AM   #5
adityavpratap
Member
 
Registered: Dec 2004
Location: Hyderabad, India
Distribution: Slackware 13, Ubuntu 12.04
Posts: 431

Original Poster
Rep: Reputation: 32
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 07:58 AM
Hostname defined in file "network" changes after change to file "hosts" qwkfish Linux - Networking 2 10-14-2009 05:07 PM
Command "mail" returns "panic: temporary file seek" kenneho Linux - Software 5 12-23-2008 04:27 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM
Can't locate object method "splitpath" via package "File::Spec" RobJohnston Linux - General 2 06-28-2003 10:59 AM


All times are GMT -5. The time now is 08:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration