LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-19-2018, 11:04 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Rep: Reputation: 177Reputation: 177
Can a file's timestamp be auto-updated when moved to a directory?


I have a dropbox directory accessible from the web. I also have a cron script that deletes anything in this folder if more than 7 days old.

The problem is that if someone puts a file into this directory that is already 7+ days old it gets deleted immediately.

Is there a way to auto-touch the timestamp on a file when moved to a directory?
 
Old 09-19-2018, 11:12 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
You could always create a subdirectory of that main Dropbox directory and use a cron script to check the main directory for files and move them to the subdirectory, touching the filestamp as you move then. Your second purging cron script would then work only on the subdirectory.
 
Old 09-19-2018, 11:35 PM   #3
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Yeah, that's basically what I did. I hoped there might be some secret tool I didn't know about. Here's my script:
Code:
. /etc/profile.d/tomcat.sh
DROPDIR="$CATALINA_HOME/webapps/ohprs/dropbox"
CURFILES=`find ${DROPDIR} -type f | sort`

# If our previous list is missing, use the current list

if [ ! -e /root/.dropfiles ]
then
    echo reset .dropfiles
    echo "$CURFILES" >/root/.dropfiles
    exit
fi

# Compare old and new

find ${DROPDIR} -type f | sort | comm -13 /root/.dropfiles - | \
while read
do
    echo `date "+%Y-%m-%d %H:%M"` touching "$REPLY"
    touch "$REPLY"
done

# Reset list
echo "$CURFILES" >/root/.dropfiles

exit 0
This is run from cron every minute between 7:00AM and 6:00PM, Monday through Friday. It doesn't do the purging since there is another job that does that for this and many other temp files. I run this one minutely since the other purging script runs periodically as well.

Last edited by mfoley; 09-19-2018 at 11:38 PM.
 
Old 09-20-2018, 12:50 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You can save one find run:
Code:
# Compare old and new

echo "$CURFILES" | comm -13 /root/.dropfiles - | \
while read
do
Furthermore, maybe you can improve your deletion job, by considering atime and/or ctime in addition to the mtime? Or by skipping file deletion in directories with a recent mtime?
 
Old 09-20-2018, 01:02 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by mfoley View Post
Is there a way to auto-touch the timestamp on a file when moved to a directory?
incron, as opposed to cron, can be configured to trigger your script whenever a file is added to a watched directory. Whether that works with Dropbox's directory, I don't know but you could give it a test.
 
Old 09-20-2018, 08:38 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You could base your deletion on ctime rather than mtime. The ctime stamp does get updated when a file is moved.
 
Old 09-21-2018, 02:45 PM   #7
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by MadeInGermany View Post
You can save one find run:
Code:
# Compare old and new

echo "$CURFILES" | comm -13 /root/.dropfiles - | \
while read
do
Yes, I actually saw that after I posted, but was too lazy to change it. I wondered if anyone would catch that ... and someone did!
Quote:
Originally Posted by Turbocapitalist View Post
incron, as opposed to cron, can be configured to trigger your script whenever a file is added to a watched directory. Whether that works with Dropbox's directory, I don't know but you could give it a test.
Excellent! That exactly the "magic tool" I was looking for. I'll experiment with that.
Quote:
Originally Posted by rknichols View Post
You could base your deletion on ctime rather than mtime. The ctime stamp does get updated when a file is moved.
Also an excellent suggestion!

I'll try both of these ideas. Thanks.
 
Old 09-21-2018, 06:12 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by mfoley View Post
Excellent! That exactly the "magic tool" I was looking for. I'll experiment with that.
I used 'incrond' many years ago. Here is a small pseudo-mini-tutorial that I made in another thread.

It might give you a quick overview of how 'incron' is set up. In the example it was used to change the permissions of a newly created file. Of course, you should change the corresponding command to, e.g.,

Code:
path/to/directory/you/want/observed IN_CREATE touch $@/$#
Let me know if there were same significant changes; as I said, it has been some time since I used it.

PS:
Related documentation:
https://linux.die.net/man/5/incrontab
https://linux.die.net/man/5/incron.conf
https://linux.die.net/man/8/incrond

Last edited by crts; 09-21-2018 at 06:15 PM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Timestamp of Directory is Updated Whereas Files are Still Having Old Timestamps devUnix Linux - General 3 05-19-2016 12:10 PM
Change group or permissions when a file is moved to a shared directory on Linux djole Linux - Server 4 02-17-2014 12:19 PM
script to timestamp files with timestamp from directory eRJe Programming 4 11-13-2013 06:52 PM
accidentally moved entire linux root "/ " file system to another directory bks2010 Linux - Newbie 8 09-19-2010 05:07 AM
updated timestamp jkmartha Linux - Security 1 05-11-2005 11:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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