LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2006, 06:43 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
cannot execute the find command as expected ?


Hi guys,

I am trying to execute a command to clear the contents of all the files under my /home/user/test directory by giving this command
Code:
find /home/user/test -type f -exec cat /dev/null > {} \;
The commands runs without any error, but unfortunately the files are not cleared. Whereas if I give the following command individually for every file,
Code:
debian:~/test#cat /dev/null > <file-name-to-be-cleared>
it works fine. That is the files are cleared. Now since there are many files under the /home/user/test directory, I want to make use of the command find as mentioned above.

Can anyone tell me what command is it? I used examples from the man find command to build the command, but unfortunately I am not able to use it.

thanks
 
Old 01-25-2006, 07:08 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
Try rm -i `find /home/user/test -type f`. The ` mark is the apostrophe in the upper left of the normal keyboard (on the same key as ~).
 
Old 01-25-2006, 07:13 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by pljvaldez
Try rm -i `find /home/user/test -type f`. The ` mark is the apostrophe in the upper left of the normal keyboard (on the same key as ~).
thank you, but actually, I don;t want to physically remove the files. I just want to erase the contents of file so that I can start afresh.

Anyone???
 
Old 01-25-2006, 07:34 PM   #4
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
Are the files constant? You could set up a script

Code:
#!/bin/bash

cat /dev/null > filename1
cat /dev/null > filename2
cat /dev/null > filename3

You could probably write a script with a loop in it that would read in each filename and then write over it and read in the next filename, etc.

Of course, I'm a , so I'm a little weak with complex scripts...

Last edited by pljvaldez; 01-25-2006 at 07:36 PM.
 
Old 01-25-2006, 07:36 PM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by pljvaldez
Are the files constant? You could set up a script that would run a series of cat /dev/null > filename.

You could probably write a script with a loop in it that would read in each filename and then write over it and read in the next filename, etc.

Of course, I'm a , so I'm a little weak with complex scripts...
That's what I am trying to do, to write a script with a loop which will read the file names in a particular directory, but I am not sure how to read the file names one by one through a script.
 
Old 01-25-2006, 10:33 PM   #6
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
Try

Code:
for i in `find /home/user/test -type f`; do cat /dev/null > $i; done
 
  


Reply



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 execute two commands on exec of a find? eantoranz Programming 3 08-03-2010 04:51 PM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
fontconfig: Didn't find expected font family toffi22 Linux - Software 1 06-27-2005 12:04 PM
command execute without ./ kaon Slackware 11 09-15-2004 05:14 PM
Execute Command lawrencegoodman Linux - Newbie 2 10-23-2003 08:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 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
Open Source Consulting | Domain Registration