LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-11-2004, 03:57 PM   #16
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45

try it like this once once from the command line to set it up initially (if testscript is the name you have given it)

/srv/scripts/testscript.sh init

then after that when ever you want

/srv/scripts/testscript.sh update

from a cron job

P.S You can remove that

echo $i

bit as It's only there to see the output when testing (I should also have tested for the existence of the differences file but it shouldn't cause too much of a problem)

Try running the update bit from the command line over a period of 5 minutes or so to see if it's picking up the changes
 
Old 02-17-2004, 12:55 PM   #17
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
looking lost - THANK YOU SO MUCH - the script does indeed seem to be working for single files under certain circumstances - this is a good start.

Unfortunately it DOES NOT properly handle files or folders that contain spaces.

any ideas?

the help is much appreciated.... sorry to be such a pain

Last edited by t3___; 02-17-2004 at 01:19 PM.
 
Old 02-17-2004, 01:18 PM   #18
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
if there was any way for the script to handle files or folders containing spaces properly - I would be %100 set.

it seems that when the script is encountering a file or folder with a space, it is creating and touching a "ghost file" for word in the filename (see example below:

directory listing before script
~~~~~~~~~~~~~~~~~~~~~~

test word document.doc 2:49pm

~~~~~~~~~~~~~~~~~~~~~~


and after the script runs at midnight

~~~~~~~~~~~~~~~~~~~~~~

test word document.doc 2:49pm

test 12:00am

word 12:00am

document 12:00am

~~~~~~~~~~~~~~~~~~~~~~

is there anyway to fix this in the script?


~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~
code:--------------------------------------------------------------------------------

#!/bin/bash



ftpDir="/var/lib/ftp" #set this to your ftp directory

workingDir="/home/myhome" #set this to the directory to hold the temp files in

init()
{
for i in `ls $ftpDir`
do
touch $ftpDir/$i
done

ls $ftpDir > $workingDir/lastListing

}



update()
{
ls $ftpDir > $workingDir/currentListing



comm -1 -3 $workingDir/lastListing $workingDir/currentListing > $workingDir/differences

for i in `cat $workingDir/differences`
do
touch $ftpDir/$i
echo $i
done

mv $workingDir/currentListing $workingDir/lastListing

rm $workingDir/differences
}



case $1 in
init)
init
;;


update)
update
;;


*)

echo $"Usage: $0 init|update"

exit 1

esac

exit $?
 
Old 02-17-2004, 04:31 PM   #19
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
try this to move/rename the files with spaces in it (needlessly to say not with important files less it goes wrong, until you're happy it works correctly)


Code:
#!/bin/bash
                                                                                
IFS="
"
                                                                                
ls /var/lib/ftp/  | grep [" "] > /home/myhome/spaceNames
                                                                                
for i in `cat  /home/myhome/spaceNames`
 do
   mv /var/lib/ftp/$i `echo  /var/lib/ftp/$i | tr -d " " `
 done
                                                                                
 rm /home/myhome/spaceNames
 
Old 02-17-2004, 04:55 PM   #20
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Slight alteration

Change the grep [" "]

to
grep [" "*]

for as it stands it only works if there's one space between each word in the name but that should sort it out
 
Old 02-17-2004, 06:18 PM   #21
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
looking lost - hats off for you patience and tenacity... yet again, I appreciate you dedicating time to this and helping me learn.

just a question for clarifiaction purposes...

the above noted (grep...) code is a script that I would run just before executing the script that is working on files and/or folders without spaces...right?

in essense "correcting" the spacing issue so that the touch script can execute properly...

am I on track here?

THANKS AGAIN - how could I thank you?
 
Old 02-18-2004, 02:10 AM   #22
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Just had a homer simpson moment there when you mentioned directories, there was no need for the last bit I did with grep, setting IFS to a newline is enough and a couple of alterations, changed ls command to find command to take into account directories and get the format in a usable fashion, hopefully that's it now - filenames with spaces touched and all...


Code:
#!/bin/bash

IFS="
"

 ftpDir="/var/lib/ftp" #set this to your ftp directory

 workingDir="/home/myhome" #set this to the directory to hold the temp files in

 init()
  {
   for i in `find $ftpDir`
   do
   touch $i
   done

   find $ftpDir > $workingDir/lastListing

  }



 update()
 {

  find $ftpDir > $workingDir/currentListing



  comm -1 -3 $workingDir/lastListing $workingDir/currentListing > $workingDir/differences

  for i in `cat $workingDir/differences`
   do
    touch $i
   
   done

  mv $workingDir/currentListing $workingDir/lastListing

  rm $workingDir/differences
 }



case $1 in
init)
init
;;


update)
update
;;


*)

echo $"Usage: $0 init|update"

exit 1

esac

exit $?
 
Old 02-18-2004, 02:42 AM   #23
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
P.S Warning

When you're deleting files at the moment make sure you're deleting only files more than two days old and not directories and you use rm NOT rm -rf, subdirectories older than two days have to be got around yet.
 
Old 02-18-2004, 12:52 PM   #24
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Well, final one Should be safe now and delete empty directories too, cut and paste the one below

to do the actually deleting of files run it as

/srv/scripts/testscript.sh cleandir

everything else is the same, hopefully I've been of some help

Code:
#!/bin/bash

IFS="
"

 ftpDir="/var/lib/ftp" #set this to your ftp directory

 workingDir="/home/myhome" #set this to the directory to hold the temp files in

 init()
  {
   for i in `find $ftpDir`
   do
   touch $i
   done

   find $ftpDir > $workingDir/lastListing

  }



 update()
 {

  find $ftpDir > $workingDir/currentListing



  comm -1 -3 $workingDir/lastListing $workingDir/currentListing > $workingDir/differences

  for i in `cat $workingDir/differences 2>/dev/null`
   do
    touch $i
   done

  mv $workingDir/currentListing $workingDir/lastListing

  rm $workingDir/differences 2>/dev/null
 }

cleandir()
{
 #Not using the -exec form of find as I'm damned if I can get it to work
 #Find all files that are more than two days old
 
 for i in `find $ftpDir -type f -ctime +2`
   do
     rm $i
   done

 #Because of the way I'm using find don't use -exec even if can get it to work as it 
 #It wil potentially delete the whole ftp dir eventually if it becomes  empty
   
  for i in `find $ftpDir -type d -ctime +2 ` #find directories last accessed over 2 days ago
   do
     if [ $i != $ftpDir ]                     #make sure we aren't trying to remove the actual ftp dir
      then 
        rmdir $i 2>/dev/null                  #and if the directory is empty delete it
     fi					      #because of the way find works some subdirs will hang
         				      #around longer than expected, but will eventually be
   done					      #deleted if they haven't seen any activity	
 }

case $1 in
init)
init
;;


update)
update
;;

cleandir) 
cleandir
;;
*)

echo $"Usage: $0 init|update"

exit 1

esac

exit $?

Last edited by Looking_Lost; 02-18-2004 at 12:58 PM.
 
Old 02-18-2004, 01:23 PM   #25
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
hopefully you have been some help? Are you kidding? You saved my ass and helped us put linux in production in a "all novell" shop - thank you so so much.

so to implement, I would:

-first run it with the init switch to set up the tmp files etc,

-then cron it to run [however frequently I want] with the update switch,

-then cron it to run again, but with the cleanup switch to actually handle the deletion of files

right?


oh, and it definately seems to be working %100 - great work - Im astounded
 
Old 02-18-2004, 01:26 PM   #26
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
That's the theory on how it should work

It could be improved no end but if it works.... like if you just want to delete empty directories no matter how old you can remove the time check in the find that looks for directories....


either way let us know how it gets on (no doubt something else will crop up )
 
  


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
One Touch backup versus One Touch II q6supvr Linux - Software 4 12-16-2007 08:28 PM
"multiboot utility" and "dell utility partition" on dell i5150 xround Linux - Laptop and Netbook 5 04-21-2005 09:19 AM
flock and touch Arcades Programming 6 03-11-2005 08:13 PM
touch screen Teus Linux - Software 1 12-04-2004 12:19 PM
Error - can't touch touch: /var/lock - filesystem problem? xanas3712 Linux - Newbie 0 05-10-2004 05:26 AM


All times are GMT -5. The time now is 07:55 AM.

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