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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
11-26-2007, 09:28 AM
|
#1
|
|
Member
Registered: Jul 2006
Posts: 85
Rep:
|
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
|
|
|
|
11-26-2007, 09:45 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
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.
|
|
|
|
11-26-2007, 09:51 AM
|
#3
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
11-26-2007, 10:26 AM
|
#4
|
|
Member
Registered: Jul 2006
Posts: 85
Original Poster
Rep:
|
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
|
|
|
|
11-26-2007, 11:00 AM
|
#5
|
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
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.
|
|
|
|
11-26-2007, 11:12 AM
|
#6
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Quote:
Originally Posted by 200mg
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.
|
|
|
|
11-26-2007, 11:25 AM
|
#7
|
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
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
|
|
|
|
11-26-2007, 12:33 PM
|
#8
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
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 "{}" ;
|
|
|
|
11-26-2007, 12:48 PM
|
#9
|
|
Member
Registered: Jul 2006
Posts: 85
Original Poster
Rep:
|
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
|
|
|
|
11-26-2007, 12:54 PM
|
#10
|
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
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
|
|
|
|
11-26-2007, 01:27 PM
|
#11
|
|
Member
Registered: Jul 2006
Posts: 85
Original Poster
Rep:
|
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 *"
|
|
|
|
11-26-2007, 01:32 PM
|
#12
|
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
On Linux I had to run the command like:
Code:
cd to proper directory
find . -name \*.log -mtime +6 -exec rm "{}" ;
|
|
|
|
11-27-2007, 04:51 AM
|
#13
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
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..
|
|
|
|
11-27-2007, 04:54 AM
|
#14
|
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Quote:
Originally Posted by 200mg
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
|
|
|
|
11-27-2007, 09:30 AM
|
#15
|
|
Member
Registered: Jul 2006
Posts: 85
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:19 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|