LinuxQuestions.org
Help answer threads with 0 replies.
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 11-26-2007, 09:28 AM   #1
200mg
Member
 
Registered: Jul 2006
Posts: 85

Rep: Reputation: 15
Using find and rm command to delete files over x days old


I'm doing this using unxutils for windows btw.

I have a bunch of files that I would like to periodically purge after they are "x" days old. All of these files have <spaces> in the filename. I currently have a script that works to find multiple files and rm them, however it does not work when the filenames have spaces.

Code:
find "c:\logs" -name *.log -mtime +6 -exec rm {} ;
Any idea how I can get this to recognize spaces in the .log filenames
 
Old 11-26-2007, 09:45 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
I don't know if this is available under unxutils, but you can try something like this
Code:
find "c:\logs" -name *.log -mtime +6 -print0 | xargs -0 echo rm
The -print0 option will print out filenames separated by a null-character and this should take care of the blank spaces. Then the output of find is piped to xargs with option -0 to accept input with null-characters. I intentionally leave an echo command to let you test before actually removing anything.
 
Old 11-26-2007, 09:51 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You need to put double quotes around the name pattern.
Code:
find "c:\logs" -name "*.log" -mtime +6 -exec rm {} ;
First use the find command by itself to make sure the correct logfiles are selected.

FYI: At work, I use cygwin/X in windows. It is handy having find, sort, sed, awk, etc. in windows.
 
Old 11-26-2007, 10:26 AM   #4
200mg
Member
 
Registered: Jul 2006
Posts: 85

Original Poster
Rep: Reputation: 15
Code:
find "c:\logs" -name *.log -mtime +6 -print0 | xargs -0 rm
This is not working, xargs is a command in unxutils. it goes all the way to -print0, prints the output, then when it tries to rm it says file not found for eveverything that has a space after it.

thanks for the replies
 
Old 11-26-2007, 11:00 AM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Try:

Code:
find "c:\logs" -name *.log -mtime +6 -exec rm "{}" ;
or

Code:
rm "`find 'c:\logs' -name *.log -mtime +6`"
HTH

Forrest

Last edited by forrestt; 11-26-2007 at 11:11 AM.
 
Old 11-26-2007, 11:12 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Quote:
Originally Posted by 200mg View Post
I'm doing this using unxutils for windows btw.

I have a bunch of files that I would like to periodically purge after they are "x" days old. All of these files have <spaces> in the filename. I currently have a script that works to find multiple files and rm them, however it does not work when the filenames have spaces.

Code:
find "c:\logs" -name *.log -mtime +6 -exec rm {} ;
Any idea how I can get this to recognize spaces in the .log filenames
This command doesn't work for any filenames.

??

What OS and distribution are you using?

edit:
ah windows ok..

This should be moved to non unix forum then

Last edited by nx5000; 11-26-2007 at 11:13 AM.
 
Old 11-26-2007, 11:25 AM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
nx5000,

The command works for filenames that DO NOT have spaces in the name, but doesn't work if the file has a space in the name (as the text you quoted indicates). Also, I don't think that this will get ANY help from a non unix forum as not many Windows admins are familiar with find.

Forrest
 
Old 11-26-2007, 12:33 PM   #8
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Yes I realized it's not the original "find" I am using. Your initial command does not run on Linux.
From there, I can't help cause I don't have this tool, only cygwin on windows is the closest. Sorry..
Cygwin is quite useful, especially for find command

edit:
ok a guess

find "c:\logs" -name "*.log" -mtime +6 -exec rm "{}" ;
 
Old 11-26-2007, 12:48 PM   #9
200mg
Member
 
Registered: Jul 2006
Posts: 85

Original Poster
Rep: Reputation: 15
I'm using a package called unxutils for windows. It has all kinds of unix commands that will run on windows, I've been using it for quite some time now to do purges with find and -mtime and it has worked great, of course all of those filenames did not have spaces in them. Below is a list of all the exe's if you care

agrep.exe
ansi2knr.exe
basename.exe
bc.exe
bison.exe
bunzip2.exe
bzip2.exe
bzip2recover.exe
cat.exe
chgrp.exe
chmod.exe
chown.exe
cksum.exe
cmp.exe
comm.exe
compress.exe
cp.exe
csplit.exe
cut.exe
date.exe
dc.exe
dd.exe
df.exe
diff.exe
diff3.exe
dircolors.exe
dirname.exe
du.exe
echo.exe
egrep.exe
env.exe
expand.exe
expr.exe
factor.exe
fgrep.exe
find.exe
flex.exe
fmt.exe
fold.exe
fsplit.exe
gawk.exe
gclip.exe
gplay.exe
grep.exe
gsar.exe
gunzip.exe
gzip.exe
head.exe
id.exe
indent.exe
install.exe
join.exe
jwhois.exe
less.exe
lesskey.exe
libfl.a
libfl.lib
ln.exe
logname.exe
ls.exe
m4.exe
make.exe
makedepend.exe
makemsg.exe
man.exe
md5sum.exe
mkdir.exe
mkfifo.exe
mknod.exe
mv.exe
mvdir.exe
nl.exe
od.exe
paste.exe
patch.exe
pathchk.exe
pclip.exe
pr.exe
printenv.exe
printf.exe
pwd.exe
recode.exe
rm.exe
rman.exe
rmdir.exe
sdiff.exe
sed.exe
seq.exe
shar.exe
sleep.exe
sort.exe
split.exe
stego.exe
su.exe
sum.exe
sync.exe
tac.exe
tail.exe
tar.exe
tee.exe
test.exe
touch.exe
tr.exe
type.exe
uname.exe
unexpand.exe
uniq.exe
unrar.exe
unshar.exe
unzip.exe
uudecode.exe
uuencode.exe
wc.exe
wget.exe.vir
wget.hlp
which.exe
whoami.exe
xargs.exe
yes.exe
zcat.exe
zip.exe
 
Old 11-26-2007, 12:54 PM   #10
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
nx5000: I had to use a different directory than "c:\logs" and escape the * in the original find command, other than that it worked fine in Linux.

200mg: Did you try my suggestions above? Either one should work.

Forrest
 
Old 11-26-2007, 01:27 PM   #11
200mg
Member
 
Registered: Jul 2006
Posts: 85

Original Poster
Rep: Reputation: 15
yes i tried them both, then tried rearranging the syntax still no good, thanks for the effort though

what do you mean by "escape the *"
 
Old 11-26-2007, 01:32 PM   #12
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
On Linux I had to run the command like:

Code:
cd to proper directory
find . -name \*.log -mtime +6 -exec rm "{}" ;
 
Old 11-27-2007, 04:51 AM   #13
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Code:
find . -name \*.log -mtime +6 -exec rm "{}" ;
No, this shoudn't work! Because the character ; is used to separate commands so you have to escape (otherwise, it is not part of find parameters and find says error ";" missing)

find . -name \*.log -mtime +6 -exec rm "{}" \;

Which is equivalent to

find . -name "*.log" -mtime +6 -exec rm "{}" \;
find . -name \*.log -mtime +6 -exec rm \{\} ";"
find . -name \*.log -mtime +6 -exec rm \{\} \;

etc..
 
Old 11-27-2007, 04:54 AM   #14
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Quote:
Originally Posted by 200mg View Post
yes i tried them both, then tried rearranging the syntax still no good, thanks for the effort though

what do you mean by "escape the *"
Do you have any documentation about this version of find?

Do this work:

Code:
find "c:\logs" -name "*.log" -mtime +6 | while read i; do rm ${i}; done
 
Old 11-27-2007, 09:30 AM   #15
200mg
Member
 
Registered: Jul 2006
Posts: 85

Original Poster
Rep: Reputation: 15
I really appreciate everyone help with this. I'm heading down another road with this, I should have listened to the previous posts about cyg. I can get it to work through cygwin, now I have to figure out how to get a batch file to launch cyg and run this command..

Code:
find "c:\logs" -name *.log -mtime +6 -print0 | xargs -0 echo rm
 
  


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
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM
find and delete files - stupid question though smshuja Linux - Newbie 5 12-29-2005 01:21 AM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM
delete files in server that is older than 30 days using cronjob latheesan *BSD 2 06-14-2005 12:37 PM
vi command to find and delete all lines beginning with a character geomatt Linux - Software 8 12-20-2004 03:30 AM


All times are GMT -5. The time now is 07:28 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration