Listing files older than 30 days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing files older than 30 days
# 1  
Old 10-23-2017
Listing files older than 30 days

Hi,

Could someone help me that what the problem is in this code?

Code:
#!/bin/sh 

FOLDER=/abc/datasource/checkstatus
 
TIMESTAMP=$(date +%s) 

for filename in $(find $FOLDER -maxdepth 1 -type f -name "CHECK_STATUS*"); do

f1=$($filename -Eo "[0-9]{4}[0-9-]+")

f2=$(date -d "$f1" +%s)

if $TIMESTAMP-$f2 -gt 2592000; then 

echo $(basename "$filename")

fi

done

exit 0;


Thank you.

Last edited by Home; 10-24-2017 at 08:41 AM..
# 2  
Old 10-23-2017
Wow Smilie

Before I try to work with it, have you considered using the -mtime flag on find? It might save you a lot of effort.



Kind regards,
Robin

Last edited by rbatte1; 10-23-2017 at 08:18 AM.. Reason: Eek!
# 3  
Old 10-23-2017
What do you think yourself what the errors might be? What error messages did your shell issue when you tried to run the code? Did you try? Did you consider the -x option running it?
# 4  
Old 10-23-2017
Quote:
Originally Posted by rbatte1
Wow Smilie

Before I try to work with it, have you considered using the -mtime flag on find? It might save you a lot of effort.



Kind regards,
Robin

Yes, I've tested with -mtime and it works fine. but sometime we are moving files to other folder and then the -mtime doesn't work to list them, then the best way is to extract the date from filename.
# 5  
Old 10-23-2017
Quote:
Originally Posted by Home
Hi,

Could someone help me that what the problem is in this code?

Code:
#!/bin/sh 

FOLDER=/abc/datasource/checkstatus
 
TIMESTAMP=$(date +%s) 

for filename in find $FOLDER -maxdepth 1 -type f -name "CHECK_STATUS*"; do

f1=${$filename -Eo "[0-9]{4}[0-9-]+"} 

f2=$(date -d "$f1" +%s)

if TIMESTAMP-$f2 > 2592000; then 

echo $(basename "$filename")

fi

done

exit 0;


Thank you.
It would be helpful if you post a few CHECK_STATUS* entries to examine.
It would be helpful if you post what the errors are. You can post the output of the code running with the flag -x.
It would be helpful if you post the os and shell type you are using.
Code:
for filename in $(find $FOLDER -maxdepth 1 -type f -name "CHECK_STATUS*");


Code:
f1=${$filename -Eo "[0-9]{4}[0-9-]+"}

Take a look how Parameter substitution works.

Code:
if TIMESTAMP-$f2 > 2592000; then

Take a look at arithmetic expressions
This User Gave Thanks to Aia For This Post:
# 6  
Old 10-24-2017
Quote:
Originally Posted by RudiC
What do you think yourself what the errors might be? What error messages did your shell issue when you tried to run the code? Did you try? Did you consider the -x option running it?
I changed some part of the code but the for loop shows all files and not those that are older than 30 days and also the result for $(TIMESTAMP-$f2) is the same for all file name that is listed, here is an example of output:
Code:
./list.sh: line 11: /abc/datasource/checkstatus/CHECK_STATUS_PARTY_CODE_2017-08-31_114506.txt: Permission denied
./list.sh: line 15: 1508845131-1508796000: command not found
./list.sh: line 15: command not found
./list.sh: line 11: /abc/datasource/checkstatus/CHECK_STATUS_PARTY_CHANGE_2017-10-17_114526.txt: Permission denied
./list.sh: line 15: 1508845243-1508796000: command not found


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


---------- Post updated at 07:08 AM ---------- Previous update was at 06:41 AM ----------

Quote:
Originally Posted by Aia
It would be helpful if you post a few CHECK_STATUS* entries to examine.
It would be helpful if you post what the errors are. You can post the output of the code running with the flag -x.
It would be helpful if you post the os and shell type you are using.
Code:
for filename in $(find $FOLDER -maxdepth 1 -type f -name "CHECK_STATUS*");


Code:
f1=${$filename -Eo "[0-9]{4}[0-9-]+"}

Take a look how Parameter substitution works.

Code:
if TIMESTAMP-$f2 > 2592000; then

Take a look at arithmetic expressions

I've changed the code before reading your comments :-)
but I ran the code with ./list.sh -x and results were the same.

Last edited by RudiC; 10-24-2017 at 08:53 AM.. Reason: Added CODE tags.
# 7  
Old 10-24-2017
Would you prefer to re-write this as Perl, or can you alter the f1= statement to meet your needs?

If you say something like "It doesn't work" without the current code, input, output & error messages it can be extremely hard to follow through. Can you help us out here?

Please paste them in CODE tags. The tutorial is below:-




Kind regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

listing processes older than n days

Hello; trying to find processes older than n days, mostly user shells Tried the following code on 11.31 box: in this case older than 5 days UNIX95= ps -ef -o user,pid,ppid,cpu,etime,stime | grep "-" | awk '{print $2}' | xargs ps -ef|grep -v '?' |\ awk '$5 !~ ""' | awk '($5 ~ "$(date "+%b")")... (6 Replies)
Discussion started by: delphys
6 Replies

2. UNIX for Dummies Questions & Answers

Files older than 50 days

Hi All, OS :- HP-UX wm5qa B.11.23 U ia64 1119805695 unlimited-user license I need to search files older than 50 days. I've used following command in order to search desired files, I also discoverd, it's showing today's files as well. Do you have any clue with this ? wmqa1> find .... (4 Replies)
Discussion started by: alok.behria
4 Replies

3. Shell Programming and Scripting

deleting files older than 7 days

Hi Guys, I am new to unix I am looking for a script to delete files older than 7 days but i also want to exclude certain directories (like arch,log .....) and also some files with extensions ( like .ksh, .ch, ..............) Thanks (1 Reply)
Discussion started by: MAYAMAYA0451
1 Replies

4. Shell Programming and Scripting

How to find files older than 30 days

Dear Friends, I have two queries. 1) I want to see the list of folders which were created 29 days ago. 2) I want to see the folders in which last created file is older than 29 days. Can it be done? Thank you in advance Anushree (4 Replies)
Discussion started by: anushree.a
4 Replies

5. Shell Programming and Scripting

delete files more than 15 days older

i have to delete files which are older than 15 days or more except the ones in the directory Current and also *.sh files i have found the command for files 15 days or more older find . -type f -mtime +15 -exec ls -ltr {} \; but how to implement the logic to avoid directory Current and also... (3 Replies)
Discussion started by: ali560045
3 Replies

6. Shell Programming and Scripting

Deleting files older than 7 days

Hi Guys, I want to delete folder/files older than 7 days. Im using the command below. find /test/test1 -mtime +7 -print0 | xargs -0 rm -Rf /test/test1/* which works ok, but it deletes the test1 folder as well which i dont want. The test1 folder will have a list of sub-folders which in... (4 Replies)
Discussion started by: shezam
4 Replies

7. UNIX for Dummies Questions & Answers

Find files older than 5 days and remove tem after listing

need help with this ... Find files older than 5 days and remove tem after listing list "test" file older than 5 days and then remove them (1 Reply)
Discussion started by: ypatel6871
1 Replies

8. UNIX for Dummies Questions & Answers

How can I delete files older than 7 days?

I will like to write a script that delete all files that are older than 7 days in a directory and it's subdirectories. Can any one help me out witht the magic command or script? Thanks in advance, Odogboly98:confused: (3 Replies)
Discussion started by: odogbolu98
3 Replies

9. Shell Programming and Scripting

Listing files older than 2 months

A script or command to list files older than 2 months in a specified directory and remove it. (3 Replies)
Discussion started by: pbekal
3 Replies
Login or Register to Ask a Question