Linux - SoftwareThis 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
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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
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
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.