LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-23-2012, 01:05 AM   #1
carlosmp
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Rep: Reputation: Disabled
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/*'
for f in $NODELOGFILES
do
	> $f
done
I was also trying this
Code:
find ./ -name '*' -type f -execdir "> '{}'" \; -print
But get a bunch of lines like this:
Code:
find: `> \'./sar22\'': No such file or directory
find: `> \'./sa14\'': No such file or directory
find: `> \'./sa20\'': No such file or directory
find: `> \'./sar21\'': No such file or directory
Any ideas?

Thanks,

Carlos.
 
Old 08-23-2012, 01:32 AM   #2
etech3
Senior Member
 
Registered: Jul 2009
Location: Virginia
Distribution: Debian Stable Testing Sid Slackware CentOS
Posts: 1,055
Blog Entries: 2

Rep: Reputation: 45
Did you try rm -rf * in the directory you want?

BE carefull!
 
Old 08-23-2012, 01:51 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
Old 08-23-2012, 08:22 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
Old 08-23-2012, 08:42 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,313

Rep: Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008
eg:
Code:
find ... -exec cp /dev/null {} \;
 
Old 08-23-2012, 08:50 AM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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.
 
Old 08-23-2012, 08:51 AM   #7
kevinbenko
Member
 
Registered: Jun 2005
Location: Fargo, North Dakota
Distribution: Debian Stable
Posts: 239

Rep: Reputation: 42
Try using the
-depth
command under find
 
Old 08-23-2012, 08:56 AM   #8
carlosmp
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
eg:
Code:
find ... -exec cp /dev/null {} \;
Would this keep the file permissions intact? This command is being executed from an openvz host on a template (that is off/stopped), so I don't know what effect this would have on permissions.

Thanks,
Carlos.
 
Old 08-23-2012, 08:58 AM   #9
carlosmp
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
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.
Trying to remove/reset the logs, so logrotate won't get that done. Already deleting the logrotated logs.

Thanks,
Carlos
 
Old 08-23-2012, 08:59 AM   #10
carlosmp
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by etech3 View Post
Did you try rm -rf * in the directory you want?

BE carefull!
Trying not to use that as I want to keep the log files with owner/perm in tact. Simply want to empty their contents.

Thanks,
Carlos.
 
Old 08-23-2012, 09:36 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
If file permissions are your concern, you could try to "cat /dev/null" inside your files, instead of using cp.
 
Old 08-23-2012, 10:02 AM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If you want to empty an existing file, but preserve the file's permission state and ownership, this should work
Code:
echo -ne > theFile
--- rod.

Last edited by theNbomr; 08-23-2012 at 10:04 AM.
 
Old 08-23-2012, 10:10 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,313

Rep: Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008
When in find, redirection is a bit tricky:

Code:
bad:    find ... -exec echo -n >{} \;
good:   find ... -exec sh -c 'echo -n >{}' \;
better: find ... -exec cp /dev/null {} \;
 
Old 08-23-2012, 05:11 PM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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

	>"$fname"

done < <( find ./ -name '*' -type f -print0 )
(The above example depends on bash's process substitution, but it should also be safe to feed the loop through a pipe in this case.)


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
 
  


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
Recursively move files michaelsanford Linux - General 5 02-17-2016 02:24 AM
Recursively rename some files mail4mz Programming 21 07-15-2011 11:18 AM
[SOLVED] recursively cp all directories, files and hidden files WildDrake! Linux - Newbie 9 05-18-2010 05:39 PM
list recursively files with for xeon123 Programming 6 04-04-2007 04:38 PM
how to rm backup files recursively wazoo Linux - Newbie 6 12-31-2004 11:03 AM

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

All times are GMT -5. The time now is 11:45 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