Subtracting a date based on ls output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subtracting a date based on ls output
# 1  
Old 03-07-2017
Subtracting a date based on ls output

I am trying to achieve to get only the month and the day. Example Feb 5 (as you can see if it is feb 1-9) the space is 2. If it is feb 10-28, the space is only 1. I am trying to right a script that will list a directory and shoot an email if there is an activity in last 7 days. I dont really trust this command "find . -type f -mtime +7" . That is why i wanted to create a script



Code:
 date --date="30 days ago"
Sun Feb  5 16:01:34 UTC 2017

Code:
ONE=15
date1=`date`
TODAY=`date -d "$date1 - $ONE"  +%b %d`
ls -l |grep '$TODAY'

My desired output ( if fall between 1-9) -double space.
Code:
Feb  5

# 2  
Old 03-07-2017
Hi,

Before continuing with addressing your scripting question, can I ask what it is about the use of find in this situation that puts you off using it ? The -mtime flag was literally made for exactly this kind of situation, and I've never found it to be un-reliable myself in any scenario where I've had to deploy it. For example, I regularly use it in scripts that prune backups older than a certain age.

It will of course only pick up on files that have been modified or created within that time-frame rather than accessed, but then that's exactly what you'd see in the output of ls as well, so there doesn't seem to be any advantage to writing your own solution here that I can see.
# 3  
Old 03-07-2017
Code:
find . -type f -mtime +7 -exec ls -l {} \;

Is that command really accurate? can it really list files on last 7 days?
Thanks


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-07-2017 at 12:37 PM.. Reason: Added CODE tags.
# 4  
Old 03-07-2017
Hi,

The command:

find . -type f -mtime +7 -exec ls -l {} \;

will find all files underneath the current working directory that were last modified more than seven days ago and perform a long directory listing on them. So if that's what you're wanting, then this should absolutely do what you need.
# 5  
Old 03-07-2017
Yes, but i want the files created in last 5 days..or 7 days..but that command will give you all the output een more than 7 days..it will give you the output last 100 days or 200... that is why im gonan create a script to give me the files...exactly last 7 days..example..from march1 2 3 4 5 6 7.. it will not include feb28 feb27 etc..
# 6  
Old 03-07-2017
Try -mtime -7.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. Shell Programming and Scripting

Subtracting months from passed Date

Hi All, I am getting a date from environment variable and want to do some processing by subtracting 2 months from the date passed through the environment variable. I am trying the following syntax : date_var=2014-08-31 date_2M_ago='$date_var+"%d%m%y" --$date_var="2 months ago" '... (3 Replies)
Discussion started by: Rahul Raj
3 Replies

3. UNIX for Dummies Questions & Answers

Subtracting number of days from current date

Hi All, I am using KSH shell and need to subtract n number of days from the current date .. kindly suggest ! Thanks in advance!:) (20 Replies)
Discussion started by: dev.devil.1983
20 Replies

4. Shell Programming and Scripting

Adding/ Subtracting from Date

Hi , How can I add/substruct x number of days with date? For example My_Date=`date` Now I need Hope it's clear. (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

5. Shell Programming and Scripting

SQL one liner for subtracting 1 from date

I got a statement like below to subtract 1 from given date using teradata. I am looking for a one line unix command to perform the same. select 'parse_this_record', (DATE '${FILE_DATE}' - 1) (FORMAT 'YYYY-MM-DD'); Input: 2012-02-21 Expected Output: 2012-02-20 PS: One liner because I am... (2 Replies)
Discussion started by: siteregsam
2 Replies

6. UNIX for Dummies Questions & Answers

Static date adding and subtracting years

Hi Gurus! I have a static date in a YYYYMMDD format; and I want get the date 2 years in the past and 2 years in the future. static_date=20010203 old_date=$static_date - 3 years future_date=$static_date + 2 years I was only able to research on dates that are current and not on static... (3 Replies)
Discussion started by: kokoro
3 Replies

7. Shell Programming and Scripting

Getting date output based on time zone

Hi, We have a server in US and hence while the command "date" is given it gives the output in EDT. If I want the date output in MET, how can I get it. Please let me know how I could do it in the script which is ksh. Thanks. (1 Reply)
Discussion started by: jmathew99
1 Replies

8. UNIX for Dummies Questions & Answers

adding or subtracting days in the o/p of date

how can we add or subtract days from the output of date command in unix... like if i want to subtract a day from the result of date command like this.. v_date=`date +%Y%m%d` this wud give me 20080519 now i want to subtract one day from this.. so tht it wud give me 20080518.. how do i do... (1 Reply)
Discussion started by: St.Fartatric
1 Replies

9. Shell Programming and Scripting

Subtracting date / timestamps

I have looked through the forums and found many date / time manipulation tools, but cannot seem to find something that fits my needs for the following. I have a log file with date time stamps like this: Jun 21 17:21:52 Jun 21 17:24:56 Jun 21 17:27:59 Jun 21 17:31:03 Jun 21 17:34:07 Jun... (0 Replies)
Discussion started by: roadcyclist
0 Replies

10. Shell Programming and Scripting

subtracting a days from current date

Hi i am trying to subtract days from current date. For example todays date is 10/03/2006. If i subtract 2 days it should give 8/03/2006. I am also trying to find the access date of a file in dd/mm/yyyy format. Can any one please help in how to do this. Ramesh (1 Reply)
Discussion started by: rameshspal
1 Replies
Login or Register to Ask a Question