Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing files by file extension. 2

Status
Not open for further replies.

M0d

IS-IT--Management
Dec 28, 2002
2
NL
Hello.

I would like to run a crontab script which removes certain files by extension in a specific directory.
I want to keep *.zip files and a file called upload.html

i.e

/files/hello.zip <- keep
/files/upload.html <-keep
/files/*.anythingelse <- delete

Thanks very much in advance for any help.
-Greg
 
Greg:

One way: Use find to search the /files directory for all files not equal to the files you want to keep. Ulitimately, pipe the find output to the rm command. xargs ensures not overflowing the command line:


find /files -type f ! \( -name '*.zip' -o -name 'upload.html' \) -print|
xargs rm

I didn't test this from cron, but put the above find command into script, and make sure PATH is set to find the find, xargs, and rm commands.

Regards,

Ed
 
Thanks very much, that works great.
Cheers!
Greg

 
Here is a sample of what you could do. If you test make sure it is tested in a safe enviornment.

hombase

-----------------------------------------------------------
#!/bin/csh

set Lp_CNT = 0;

printf &quot;What directory would you like to clean: \n&quot;
set DIR = $<

cd $DIR

mkdir files

while(1)

if(Lp_CNT == 1)
exit;
endif

foreach file (*.zip)
mv $file TEMP
end

foreach file1 (*.html)
mv $file1 TEMP
end
# check the -f to ensure this the what you want.
if(-f *.* != $file || $file1) # I haven't test this
rm *.* # but it should work,
endif # if not tweak the
# condition.
@ Lp_CNT++

end hombase
 
Sorry I didn't place &quot;then&quot; after the if statements.

printf &quot;What directory would you like to clean: \n&quot;
set DIR = $<

cd $DIR

mkdir files

while(1)

if(Lp_CNT == 1) then
exit;
endif

foreach file (*.zip)
mv $file TEMP
end

foreach file1 (*.html)
mv $file1 TEMP
end
# check the -f to ensure this the what you want.
if(-f *.* != $file || $file1) then # I haven't test this
rm *.* # but it should work,
endif # if not tweak the
# condition.
@ Lp_CNT++

end hombase
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top