LinuxQuestions.org
Review your favorite Linux distribution.
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 03-16-2020, 01:05 AM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
use command line to change last modified time of files


The gf had a lot of old exams to scan to pdf and watermark. The pdfs have between 4 and 8 pages.

I helped her with a bit of Python.

The boss thinks she is scanning each page by hand, like 200 pages, cutting, joining tidying ....

Now she says, the boss will see that they all have the same 'last modified time'.

How can I specifically change the last modified time from Python or bash?

I would like to set the 'last modified time' to some random number of minutes apart, around 30 minutes.

I don't really want to modify them, just change the 'last modified' time.

Can that be done? Best way to do this?
 
Old 03-16-2020, 01:21 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,773

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
this is the command: touch (see man page about details)
 
Old 03-16-2020, 01:59 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,288
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Then you can get the current (or any other) time in seconds and loop over it while subtracting a base of 900 each time along with a random interval using the built-in Bash variable $RANDOM modulo the interval.
 
Old 03-16-2020, 02:03 AM   #4
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thanks, looks good.

But I'd like to call touch from a bash script or Python.

This looks like it can do the job:

touch -t STAMP

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

So I think STAMP should look like:

202003$day$hour$minute$second

$hour between 09 and 19

$minute between 1 and 59

$second between 1 and 59

Does that look like it could work??
 
Old 03-16-2020, 02:07 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,773

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
why not? Just try it on a tmp file (but touch is only for bash). For python you need something else: https://stackoverflow.com/questions/...h-using-python
 
Old 03-16-2020, 02:10 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,288
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
I'd go with the shell. I perceive it to be quicker to accomplish this task using Bash or POSIX shell than digging through Python.

Again, you could just start with a time, measured in seconds, and subtract a span of seconds between touching each file. See %s in "man date". The @-sign is not mentioned in "man touch" but you could use it like this:

Code:
dt=1084342325
dt=$(($dt - $RANDOM % 900 - 900))

f=/tmp/x

touch -d @$dt $f
 
1 members found this post helpful.
Old 03-16-2020, 05:18 AM   #7
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
no hours or minutes

Thanks for the bash code.

I would like a more human readable time than dt="1583012819"

A bit too cryptic for me!

This is what I have so far. It works, the year, month and day change, but I do not get the hours or minutes or seconds: they are 00 00 00 when I look. Can you see why??

If you could give me a tip about how to randomize any 1 of yy, mm, dd, hour that would be great too!

Quote:
echo "change the modified time of a file."

# time in seconds since 00:00 January 01 1970
# this was this afternoon, 2020, March 16 14:28 according to my php upload file which time stamps the upload
# dt="1583012819"

# "2004-02-29 16:21:42"

# info touch
#‘-d TIME’
#‘--date=TIME’
# Use TIME instead of the current time. It can contain month names,
# time zones, ‘am’ and ‘pm’, ‘yesterday’, etc. For example,
# ‘--date="2004-02-27 14:19:13.489392193 +0530"’ specifies the
# instant of time that is 489,392,193 nanoseconds after February 27,
# 2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes
# east of UTC. *Note Date input formats::. File systems that do not
# support high-resolution timestamps silently ignore any excess
# precision here.


#hour="($RANDOM % 24)"

yy="2019-"
mm="03-"
dd="28"
space=" "
hour="20:"
mins="21:"
secs="42"

dt=$yy$mm$dd$space$hour$mins$secs

echo $dt

cd /home/pedro/babystuff/watermarkedPDFs/

for i in *.pdf; do

#dn=$(($dt - $RANDOM % 7200))

#f=/home/pedro/babystuff/watermarkedPDFs/test3_wmed.pdf

#touch -d @$dn $i; done
# this works, but hours, minutes and seconds are all 00

touch -c --date=$dt --no-create $i; done

echo "All done!"
Thanks for any tips!
 
Old 03-16-2020, 05:45 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,288
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Remember to keep track of the date format for the date utility. If you are feeding it Epoch seconds then you feed it with an at (@) sign. Otherwise use a normal date format.

Untested, but I think you needed quotes around the date variable for touch:

Code:
#!/bin/sh

echo "change the modified time of a file."

yyyy="2019"
mm="03"
dd="28"
hour="20"
mins="21"
secs="42"

dt="$yyyy-$mm-$dd $hour:$mins:$secs"
echo $dt

cd /home/pedro/babystuff/watermarkedPDFs/

for i in *.pdf; do
        touch -c --date="$dt" --no-create $i; 
done

echo "All done!"

exit 0
You can keep track of the hours, minute, second, and even days, months, and years separately. But since this is about randomization, using Epoch seconds exclusively makes the process easier.

Code:
#!/bin/bash

# starting date, expressed in Epoch seconds
dt=1561237200

for file in *.pdf; do

        # use Bash's RANDOM built-in and step back in time some interval
        # 900s = 15 minutes
        dt=$(($dt - $RANDOM % 900 - 900))

        touch -c --no-create -d @$dt "$file"

done

exit 0
That deals with the file system's metadata.

One afterthought, now that I see it is a matter of PDFs, there is metadata embedded inside the PDF itself. That will have to be addressed, too. Perl is easiest but Python ought to have tools available too.
 
Old 03-16-2020, 05:59 AM   #9
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thanks!

Her boss won't look at the metadata in the pdf, he is not a computer person!

He bought transparent sheets with a 'watermark' on them. She was supposed to scan each page with that underneath to watermark the pdfs!

About 200 pages this time, next time, who knows how many?? Scanner is a Canon single page flatbed usb scanner!

My primitive Python routine watermarked 31 pdfs, nearly 200 pages, in about 2 seconds.

Since we are in China, and we can't go out much now, I suppose she can take her time!

However, the time of the last modification is seen in Windows usually, I think! That might be noticed if they are all exactly the same!

Thanks again!

ps: I have the Python reportlab module, I could probably change the pdf internal data with that. I'll look at the reportlab manual.
 
  


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
How to check the last modified time of the file. anandg111 Linux - Newbie 5 01-05-2012 04:55 PM
script to execute a command based on the last modified time of a folder midadala Linux - Newbie 1 10-19-2010 07:28 AM
[SOLVED] file properties - last access/modified time gmsa Programming 2 04-22-2010 07:27 AM
utility to update the last modified time of a directory RajRed Linux - Newbie 4 02-20-2006 04:34 PM
PHP last time modified script time way off GabeF Programming 2 11-05-2002 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:32 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
Open Source Consulting | Domain Registration