LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-24-2004, 04:01 PM   #1
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 291

Rep: Reputation: 30
Quarantine files based on their extension and location


Dear visitors.

I'm using Linux Quotas to limit the ammount of space my hosted sites have

However, some sites tend to upload ZIP files and I really don't like that

I have a crontab program that executes "updatedb" everynight, therefore my "locate" database is always up to day.

What I'd like to do is use /bin/bash or /bin/sh to:

1) Locate files with certain extensions (.zip, .exe, .rar) ONLY in certain directories (/home/hosted/*)

2) Move those files to a special folder (/home/hosted/quarantine/*)

This is an example of how I'll do this on bash. However I want a script that will automate this process:


1) locate .zip .rar .exe | grep /home/hosted/

2) That will show the following output
/home/hosted/site1/file.zip
/home/hosted/site1/file.exe

3)
mv -v /home/hosted/site1/file.zip /home/hosted/quarantine/site1/
mv -v /home/hosted/site2/file.zip /home/hosted/quarantine/site2/

Please notice that each site has it's own subdirectory in the "quarantine" folder

This is my attempt to do this automatically with bash:

#!/bin/bash
locate .zip .rar .exe | grep /home/hosted/site1 > site1_files.txt;
echo "site1_files.txt created ok";
for i in site1_files.txt; do mv -v $i /home/hosted/testing; done;
locate .zip .rar .exe | grep /home/hosted/site2 > site2_files.txt;
echo "site2_files.txt created ok";
for i in site2_files.txt; do mv -v $i /home/hosted/testing; done

If I run this script, the system copies the actal files (site1_files.txt, site2_files.txt...) to the respective "quarantine" directories

Regards
 
Old 06-24-2004, 04:11 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Use cat to read file content
Code:
for i in `cat site1_files.txt`; do
    mv -v $i /home/hosted/testing
done
 
Old 06-25-2004, 01:44 AM   #3
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 291

Original Poster
Rep: Reputation: 30
Thanks for the suggestion

Here's the script now:

#!/bin/bash
cd /home/quarantine
locate *.zip *.exe *.rar *.tar *.gz | grep /home/hosted/site1 > site1.txt
for i in '/bin/cat site1.txt'; do
mv -v $i ./site1
done

And here's the output

`/bin/cat' -> `./site1/cat'
`site1.txt' -> `./site1/site1.txt'
mv: can't stat source /bin/cat


Any ideas?
 
Old 06-25-2004, 01:52 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You're using the wrong quotes

Change this:
Code:
for i in '/bin/cat site1.txt'; do
to this:
Code:
for i in `/bin/cat site1.txt`; do
or this:
Code:
for i in $(/bin/cat site1.txt); do
 
Old 06-25-2004, 02:15 AM   #5
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 291

Original Poster
Rep: Reputation: 30
Thanks!

That surely worked!

`/home/hosted/site1/Prohibidden-File.zip' -> `./home/qurantine/Prohibidden-File.zip'
 
Old 04-13-2005, 04:51 PM   #6
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 291

Original Poster
Rep: Reputation: 30
Question

What if I want this script to work with names that have spaces?
I tried sed and tr but I don't know much about bash scripting, I will also like to receive an email with this

Regards
 
Old 04-16-2005, 08:34 PM   #7
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi xbaez,

Files with spaces into their names is a little tricky. The shell "for" command always break any word on the space, except if it is masked. I had this problem few years ago and I couldn't get any satisfactory solution with this command. So, I changed to use the "while read" command instead of it. Your script will be changed to:
Code:
/bin/cat site1.txt | while read i; do
This change will put each line of the file into the variable "i", instead of each word.

Good luck!

Osvaldo.
 
Old 07-07-2005, 12:36 PM   #8
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 291

Original Poster
Rep: Reputation: 30
#!/bin/sh
cd /var/www/
for i in `ls -1 /home/hosted/`; do
if ($i!="backup" && $i!="quarantine") {
tar cpzf /var/backup/domains/$i.tgz $i/httpdocs $i/cgi-bin
echo $i #testing
done

Any help will be highly appreciated
I have another script that I'm doing now (right now I'm allowing my hosted sites to publish downloads). I just want to backup all the folders in the /var/www/ folder, with exception of the "backup" and "quarantine" folder
 
  


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
remove all files with same extension? scratchnz Linux - General 1 11-04-2005 03:40 AM
files with extension rar ankscorek Linux - Software 5 07-25-2005 09:41 PM
Extension files BC1 Linux - Software 5 12-20-2004 03:41 AM
What to do about these files with .new extension? Slovak Slackware 1 11-23-2004 04:50 PM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 11:42 AM

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

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