LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-22-2004, 01:03 PM   #1
t3___
Member
 
Registered: Sep 2003
Posts: 240

Rep: Reputation: 30
Help with the TOUCH utility


Im trying to acomplish a particular task with the (newly discovered for me) Touch utility.

here is my objective:

I have created a directory on my SUSE9.0 box - internal users and external clients/subcontractors/etc will use this directory for quick get/put file exchanges... external users will copy files to the directory via FTP (have vsftpd installed and configured), internal users connect to the directory via samba (also installed and working).

For security reasons, managment has insisted that files be automatically deleted after aproximately 2 days/ 48 hours. I have come up with a script that will delete files with a modified/created date older than 2 days, but I still have a problem.

When copied to the directory, whether by ftp or samba, the files are retaining thier original create/modified date. Therefore the above noted script will delete, essentially, any file every time it is run.

Someone suggested that I use TOUCH to modify the datestamp. I came up with the idea to run TOUCH once a night (say at 11:58 pm) to modify all new files with the current days datestamp, then a minute later run my script to delete all files older than 48 hours. Users could be told that the files would stay for 2 callendar days.

~~~~~~~~~~~~~~~~
HERE IS MY DILEMMA: I cant figure out how to instruct TOUCH to only modify files it has not previously modified. Otherwise all files will get the current days datestamp, and nothing will ever get autodeleted. Ive tried the -c option... it doenst work.
~~~~~~~~~~~~~~~~

Im really hoping that someone can give me the exact syntax to try - im a brand newbie - Ive copied the man pages for touch below.

Someone please help..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update the access and modification times of each FILE to the current time.

Mandatory arguments to long options are mandatory for short options too.

-a
change only the access time

-B, --backward=SECONDS
Modify the time by going back SECONDS seconds. For example, touch -r foo -B 5 bar will make the file bar 5 seconds older than file foo.

-c, --no-create
do not create any files

-d, --date=STRING
parse STRING and use it instead of current time

-F, --forward=SECONDS
Modify the time by going forward SECONDS seconds. For example, touch -r foo -F 5 bar will make the file bar 5 seconds newer than file foo.

-f
(ignored)

-m
change only the modification time

-r, --reference=FILE
use this file's times instead of current time

-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time

--time=WORD
set time given by WORD: access atime use (same as -a) modify mtime (same as -m)

--help
display this help and exit

--version
output version information and exit
Note that the -d and -t options accept different time-date formats.
 
Old 01-22-2004, 02:47 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I haven't tested it at all, but this should do what
you want if run as a cron-job ... play around with
it for a bit ;)


Quote:
quick & dirty hack: kill_old.sh
Code:
#!/bin/bash
find /<path>/<to>/<download-dir> -ctime 0 -exec touch -m {} \;
find /<path>/<to>/<download-dir> -ctime 2 -exec rm -f {} \;

Cheers,
Tink

 
Old 01-23-2004, 07:42 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Just out of curiosity ... does it work? :)


Cheers,
Tink
 
Old 01-23-2004, 07:46 PM   #4
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
thank you so much for your reply...

I substitued the cmin switch, and while minutes cant represent the exact scenario, each line of the script was working independantly... I also wrote my first bash script!!!

thanks... monday I will know for sure (after 48 hours).

your help is much appreciated!!!
 
Old 01-24-2004, 12:25 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I'm not quite sure I understand what you're
saying about the cmin switch.

Keep me posted please!


Cheers,
Tink
 
Old 02-02-2004, 03:22 PM   #6
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
***update***

unfortunately the script file is not working...

basically it is touching all files (regardless of whether it was previously "touched" or not...

any ideas?
 
Old 02-04-2004, 03:28 PM   #7
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
bump, bump bump...
 
Old 02-04-2004, 07:05 PM   #8
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I think it was me who replied to your original post before, I actually made a script up before that did it but ya'd gone (although not going the extra step of taking into count files being delete by users in between runs)

Process:

touch all the files

Do a directory listing and save it into a file

at whatever time needed do another directory listing and save it

compare the two files in this case using comm -3 file1 file2 > differences

use the contents of differences to touch only new files that have appeared

cron job sorts out the 2 day bit

A wee bit of round-robin deleting/renaming the files that contain the directory listing and the process starts again

Unfortunately don't have the script anymore so...
 
Old 02-04-2004, 11:59 PM   #9
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Rep: Reputation: 30
i dont know if you want to do this but you could try making a hidden file with the same name as the real file after you have edited its timestamp and then in the script check if a .(dot) file with that name exists,for example if your file is foo you can create a .foo by doing touch .foo and then when you check...just look for .foo..then dont mess with it else...go ahead and change its timestamp.
i dont think this should use up too much space since the file will be 0 bytes.
my 2 cents
 
Old 02-05-2004, 11:47 AM   #10
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
Looking_Lost - I included your original post below...

I definately will need some help getting started on the script... in particular how to dump a directory listing to a file (sorry folks - im very exp in the computer world, but VERY new to the linux planet). Im assuming that COMM is the command to compare a pair of files... in what context/what syntax would this be used to tell touch to only modify files that are the difference of the compare?

any complete or partial help would be greatly appreciated!!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I guess you could have a file containg a directory listing taken at time A and compare it against a file containing the current directory listing taken at time B and use directly/send the output to another file which should contain the differences. If and buts start coming in though to complicate matters such as can the users delete the file themselves inbetween runs? The principle is there though to work around though and maybe some one will jump in with a one line does it all answer

comm -3 timeA timeB > differencesInListings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Old 02-05-2004, 11:51 AM   #11
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
oh... and rmanocha... if I went your direction....

how exactly would I tell touch to both:
touch a file/modify it's date stamp,
{and symutaneously}
create the hidden file?

and ....

how would I instruct touch to NOT modify files with a twin hidden file?

btw... Newcastle, not Guiness, is the best beer in the world

thanks

Last edited by t3___; 02-05-2004 at 11:53 AM.
 
Old 02-05-2004, 12:51 PM   #12
rmanocha
Member
 
Registered: Oct 2003
Location: Austin,TX
Distribution: Debian SID-->fully content-->Love APT,kernel 2.6.4
Posts: 327

Rep: Reputation: 30
Quote:
Originally posted by t3___
oh... and rmanocha... if I went your direction....

how exactly would I tell touch to both:
touch a file/modify it's date stamp,
{and symutaneously}
create the hidden file?

and ....

how would I instruct touch to NOT modify files with a twin hidden file?
well maodify the date stamp as usual and then just take the filename(i believe you can rip it off ...i am not really an authority in bash scripting) and append a .(dot to it)
checking for the hidden file should be easy....again rip the filename of the file and then use the if command with the -f(i think it is) to check if there is a similar file name with a .(dot) appended to it.
i know i am not answering your question but this is the best i can do now...i can try to write the script...if and when i get time.


Quote:
btw... Newcastle, not Guiness, is the best beer in the world
well actually it is carlsberg who goes by this ad(and that beer is great too) but i personally think guisness kicks everyones a**es.but that could just be me.never tried newcastle...maybe i should give it a try...which county is it from?
 
Old 02-05-2004, 12:57 PM   #13
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
bro... what planet are you from?

http://www.newcastlebrown.com/home_f.php?flash=yes

its from England... the one and only
 
Old 02-05-2004, 02:52 PM   #14
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Okay, I had another go at it, try this. Change the values of ftpDir and workingDir appropriately. Save it as filetoucher.sh and make it executable ensuring that your workingDir exits. Run it once from the command line as

./filetoucher.sh init

after that you can run it in a cron job like
filetoucher.sh update

it should touch only those files that have been created since the last touch aswell as taking into account any files deleted by users in between runs the -1 parameter passed to comm should ensure that. Give it a whirl anfd let me know how it does as I don't have the time/or patience to sit creating/deleting files to test it absolutely fully.

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 $?

Last edited by Looking_Lost; 02-05-2004 at 02:54 PM.
 
Old 02-11-2004, 03:41 PM   #15
t3___
Member
 
Registered: Sep 2003
Posts: 240

Original Poster
Rep: Reputation: 30
looking lost - ok. I tested the script file you wrote above - first off, THANK YOU SO MUCH FOR YOU HELP!

It appears to not be working.

When I run the command, the output is the following:

Usage: /srv/scripts/testscript.sh initlupdate

I can be certain that I specified the ftp and working directory properly. I have only a basic understanding of these bash scripts, but I can say for certian that the script is NOT dumping the directory listing to the working dir, and is not touching any of the files.

any help would be greatly appreciated...

Tom
 
  


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 04:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration