Solved

Date change on files

Posted on 2011-03-25
9
360 Views
Last Modified: 2012-05-11
Hi,
 Through shell script I am calling a perl script and once the script is executed, I am changing the permissions on certain folders and also converting the output file to dos format for user readability.

But this is changing the old files too. I mean for example if some files were created yesterday they have that date and timestamps.  But when the script runs today it is changing their date to today's date.

So is there a way we could do based on date and time. by the way this script runs everyday at 6 through cron.

#!/bin/sh
. /usr/nlm/qa/.profile

/usr/nlm/qatest/Scripts/info.pl

chmod -R 755 /usr/qatest/data/manifests/*

chmod -R 755 /usr/qatest/data/output/*

unix2dos /usr/qatest/data/manifests/*
0
Comment
Question by:new_perl_user
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:sdern
ID: 35217744
So what you're trying to do is change permissions on folders + subfolders and files, based on their timestamp?

Or are you trying to change the access permissions without changing the date?
If that is the case what you could try to do is read the date, do the file change permissions then change the date back to what you just read. I can't find any flags in chmod that will leave the date alone and just change the permissions.

touch is the command for changing the time
0
 

Author Comment

by:new_perl_user
ID: 35217823
ok what I am trying to do was. when the script runs it creates a log files under "/usr/qatest/data/manifests" . so once the log file is created I am tring to change the permissions on it and then convert it to dos format .

But issue is .  It is performing the same action on previously generated log files also.

I mean it is changing today and even the old ones.  So the date on all the files are being updated always.

This is happening because I am declaring  chmod -R 755 /usr/qatest/data/manifests/* .  It is working on all the files.

so can ayone tell me how to restrict it by date.
0
 
LVL 2

Expert Comment

by:sdern
ID: 35217873
So generating log files is fine. You just want to change the permissions on the latest log file.
You can try using the find command and look for the latest date.

Here is a post regarding your question

http://www.codingforums.com/showthread.php?t=131078

0
 

Author Comment

by:new_perl_user
ID: 35218029
Hi,
 Thank you for the reply. But I  want to do it  using shell not perl . Because my above script is in shell.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:sdern
ID: 35218148
ls -l -rt | tail -1
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35258088
using the chmod ..../* will by default change everything in the directory
you need to identify todays file
try this



#!/bin/sh
. /usr/nlm/qa/.profile

/usr/nlm/qatest/Scripts/info.pl

todays_file=` ls -ltr | awk '{print $9}' | tail -1`

chmod -R 755 /usr/qatest/data/manifests/$todays_file

chmod -R 755 /usr/qatest/data/output/$todays_file

unix2dos /usr/qatest/data/manifests/$todays_file
0
 

Author Comment

by:new_perl_user
ID: 35259553
Hi,
 How does the above code work. Because when I tried it , it is picking up the newest files from the path where the script runs and trying to append it in $today's file.

I have a bunch of scripts at  /usr/nlm/qatest/Scripts
and I am running the above script as  /usr/nlm/qatest/Scripts/info1.pl

so  the code is becoming then:

chmod -R 755 /usr/qatest/data/manifests/test.pl

chmod -R 755 /usr/qatest/data/output/test.pl

unix2dos /usr/qatest/data/manifests/test.pl


where test.pl is the script which has been modified recently at /usr/nlm/qatest/Scripts
0
 
LVL 8

Accepted Solution

by:
point_pleasant earned 500 total points
ID: 35260535
what the script does is set $today_file to the newest file in the directory the ls -ltr gives listing in reverse order the tail -1 takes the last one listed i.e. the newest pipes to awk to get the file name.  that filename is the handed to chmod.

the call should be run in the target directory i.e.

todays_file=` ls -ltr /usr/qatest/data/manifests | awk '{print $9}' | tail -1`

so

#!/bin/sh
. /usr/nlm/qa/.profile

/usr/nlm/qatest/Scripts/info.pl

todays_file=` ls -ltr /usr/qatest/data/manifests | awk '{print $9}' | tail -1`
chmod -R 755 /usr/qatest/data/manifests/$todays_file

todays_file=` ls -ltr /usr/qatest/data/outputs | awk '{print $9}' | tail -1`
chmod -R 755 /usr/qatest/data/output/$todays_file

todays_file=` ls -ltr /usr/qatest/data/manifests | awk '{print $9}' | tail -1`
unix2dos /usr/qatest/data/manifests/$todays_file
0
 

Author Comment

by:new_perl_user
ID: 35261925

Thank you the above code worked.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now