# if file exist and has mod date of... (unix).



## cgjoker (Aug 13, 2003)

Im trying to execute this but its not working.. any ideas?

I want to search to see if a file exists and if it does, does it have modified date of 1, and if so, then do copy of that file to a different directory.

for file in `find ./ -type f -name "1*" `
do	
if ( $file && $file in `find ./ -type f -mtime 1 `)
then
echo " checked and good..copying" >> /test/processing.log 
mv $f /test/.
else
echo " No files found equal to 1 day, continuing processing." >> /test/processing.log 
fi
done


----------



## tsunam (Sep 14, 2003)

well first off the echo command you do is doing a copy of the echo to your test/processing.log..there's no copying of the file anywhere in your script.

The only thing there is a mv...That's a for starters of whats going on thats wrong.

#!/bin/bash
for file in `find ./ -type f -name "1*" `
do
if ( $file && $file in `find ./ -type f -mtime 1 `)
then
cp $file /test/
else
echo "no files found equal to 1 day, continuing processing" >> /test/processing.log
fi
done

that should work, under the assumption that the test casing works in the first place.


----------



## cgjoker (Aug 13, 2003)

filemod=`find ./ -type f -mtime 1 `
filename=`find ./ -type f -name "1*.*" `

if [ $filemod ] && [$filename]
then
echo " found" >> /test/processing.log
else
echo " not found" >> /test/processing.log 
fi


----------



## cgjoker (Aug 13, 2003)

thanks tsunam.... i know my script wasn't copying, i removed that command just left the echo command for simplicity sake, just so i can test that the rest of the script is right. ill try out your code and let you know... thanks.


----------



## cgjoker (Aug 13, 2003)

nope, didnt work.

here is the return syntax... (im sure i tried what you typed previously and got the same error.)

./1file1.dat: 0403-057 Syntax error at line 1 : `(' is not expected.
./1file2.dat: 13728: not found.
./1file2.dat[2]: 10646: not found.
./1file2.dat[3]: 10745: not found.
./1file2.dat[4]: 10610: not found.
./1file2.dat[5]: 10459: not found.
./1file2.dat[6]: 72984: not found.
./1file2.dat[7]: 0403-057 Syntax error at line 7 : `(' is not expected.

I have two files in the directory that this script is looking at:

1file1.dat mod date = 03/08/2005
1file2.dat mod date = same


----------



## cgjoker (Aug 13, 2003)

heres another i tried with no luck

if ( -f in `find ./ -type f -name "1*" `&& -f in `find ./ -type f -mtime 1 `)
then
cp $file /test/
echo "files found equal to 1 day, continuing processing" >> /test/processing.log
else
echo "no files found equal to 1 day, continuing processing" >> /test/processing.log
fi


----------



## tsunam (Sep 14, 2003)

so you have a directory with a bunch of 1file[number].dat files and you want to see which files have a certain date? Can you explain exactly how and what you are doing so that I can understand fully what you are trying to do and then work on a script to help out as well?


----------



## Squashman (Apr 4, 2003)

tsunam said:


> so you have a directory with a bunch of 1file[number].dat files and you want to see which files have a certain date? Can you explain exactly how and what you are doing so that I can understand fully what you are trying to do and then work on a script to help out as well?


Yeah, I would like to know as well. Doesn't seem like you are going about this the right way. Are you just trying to find files with a certain modified date or files that were modified today?


----------



## cgjoker (Aug 13, 2003)

a bunch of files with a 1file[number].dat.

there is also other files in that same directory with other file names.

i want to copy only the files named 1file[number].dat that have a modified date of 1 day old.


----------



## Squashman (Apr 4, 2003)

So, do you want files modified exactly 24 hours ago or files that were modified the previous day.


----------



## cgjoker (Aug 13, 2003)

the code below works if i substitute between the two first commands so I know the find statements work. I just need a way to join the two commands together so that if both statements are true, then the copy will be performed.

#for file in `find ./ -type f -name "1*.*" `
#for file in `find ./ -type f -mtime +1 `
do	
if [ $file ] 
then
echo "files found equal to 1 day, continuing processing" >> /test/processing.log
cp $file /test/
else
echo "no files found equal to 1 day, continuing processing" >> /test/processing.log
fi
done

I tried this but no luck....

for ([file in `find ./ -type f -name "1*.*" `] && [file in `find ./ -type f -mtime +1 `])
do
if [ $file ]
then
echo "files found equal to 1 day, continuing processing" >> /test/processing.log
cp $file /test/
else
echo "no files found equal to 1 day, continuing processing" >> /test/processing.log
fi
done


----------



## Squashman (Apr 4, 2003)

find ./ -type f -mtime +1 | grep 1*.* | cp /test


----------



## cgjoker (Aug 13, 2003)

here is the solution... thanks for your help.

for file in `find ./ -type f -name "1*.*" && find ./ -type f -mtime +1 ` 
do
if [ $file ]
then
echo "files found equal to 1 day, continuing processing" >> /test/processing.log
cp $file /test/
else
echo "no files found equal to 1 day, continuing processing" >> /test/processing.log
fi
done


----------



## cgjoker (Aug 13, 2003)

sorry guys.. its still not working properly. im getting all the files, regardless of the front leading numeric value.

so i have files

1file1.dat
2file2.dat,

both files are being copied.

i tried both codes

for file in `find ./ -type f -name "1*.*" && find ./ -type f -mtime +1 ` 
do
if [ $file ]
then
echo "files found equal to 1 day, continuing processing" >> /test/processing.log
cp $file /test/
else
echo "no files found equal to 1 day, continuing processing" >> /test/processing.log
fi
done

AND

find ./ -type f -mtime +1 | grep 1*.* | cp /test


----------



## cgjoker (Aug 13, 2003)

Also, to answer Squashman's date question, I would like it to actually be by date, and not the 24 clock.


----------



## Squashman (Apr 4, 2003)

I am working on a different approach using the date command.

date --date='1 day ago'


----------



## cgjoker (Aug 13, 2003)

sorry.... didn't realize there was a page two and i thought you guys might have thought this was closed.

thanks.

any luck so far?

i tried this and it somewhat works but i noticed that the file is returned as

./1file1.dat rather than 1file1.dat so therefore it is not getting copied.

for file in `find ./ -type f -mtime +1 `
do 
if [ -f $file ] 
then 
case $file in 
1*.*) 
cp $file /mdm_work/Campaign_Downloads/test
echo "$file copied" >> /test/processing.log
;; 
*) 
echo "$file: appears to be other" >> /test/processing.log
;; 
esac 
fi 
done


----------



## Squashman (Apr 4, 2003)

This should do it.


```
date1=`date +%Y-%m-%d -d "yesterday"`
for filename in `find "/home/jfriberg/test" -type f -name "1*.*"`
do
grep1=`ls --full-time $filename | grep -c $date1`
if [ $grep1 = "1" ]; then
cp -p $filename /home/jfriberg/backup/
fi
done
```


----------



## cgjoker (Aug 13, 2003)

didn't work... i got the following error

iw113:user:/test]./find_mod.sh
date: 0551-402 Invalid character in date/time specification.
Usage: date [-u] [+"Field Descriptors"]
find: 0652-010 The starting directory is not valid.


----------



## Squashman (Apr 4, 2003)

Works fine on my box. Post back the exact script you used and I will replicate the paths on my box. (Copy and Paste it!)

Which version of Linux\Unix\BSD are you running this on?
Which Shell are you using?


----------



## cgjoker (Aug 13, 2003)

if i copy and paste, as per below:

date1=`date +%Y-%m-%d -d "yesterday"`
for filename in `find "/EBCC" -type f -name "2*.*"`
do
grep1=`ls --full-time $filename | grep -c $date1`
if [ $grep1 = "1" ]; then
cp -p $filename /test/
fi
done

I get the following error:

iw113:cgalindo:/affinium/Campaign/Std_recur2_Validation/script]./mod.sh
date: 0551-402 Invalid character in date/time specification.
Usage: date [-u] [+"Field Descriptors"]
ls: Not a recognized flag: -
ls: Not a recognized flag: -
Usage: ls [-1ACFLNRabcdefgilmnopqrstux] [File...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] -e pattern_list...
[-f pattern_file...] [file...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] [-e pattern_list...]
-f pattern_file... [file...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] pattern_list [file...]
./mod.sh[17]: test: 0403-004 Specify a parameter with this command.

I should have mentioned we are actually on AIX 5.1.


----------



## Squashman (Apr 4, 2003)

And what shell are you using. Scripts are dependent on what shell you are using. If you can install bash on their it should run. But you then need to put the interpreter at the top of the script.

#!/bin/bash ( or wherever bash is installed on your system.)

We probably shouldn't even really use *test* as a directory name because it is an acutal command. You might have to put the $Date1 variable in quotes.

You might have to look at the manual pages for your shell environment to figure out how to get the full-time in the LS command and figure out how to get yesterday's date to output with those variables. I dont know what shell you are using so it is hard to say. If you have bash installed this script will run.


----------



## cgjoker (Aug 13, 2003)

sorry,... korne shell. I should have known that is what you were asking for.

i dont think we have bash installed.

this is the error if in quotes:

iw113:user:/test]./mod.sh
date: 0551-402 Invalid character in date/time specification.
Usage: date [-u] [+"Field Descriptors"]
ls: Not a recognized flag: -
ls: Not a recognized flag: -
Usage: ls [-1ACFLNRabcdefgilmnopqrstux] [File...]
iw113:user:/test]


----------



## Squashman (Apr 4, 2003)

cgjoker said:


> sorry,... korne shell. I should have known that is what you were asking for.
> 
> i dont think we have bash installed.


Do you think you could check. Try just typing bash at a shell prompt. If it is installed you will switch to a bash shell. If it is installed put this line at the top of your script.

#!/bin/bash (or wherever bash is installed)

Or do a find for it.


----------



## Squashman (Apr 4, 2003)

I just checked IBM's website. You can run bash on AIX. I dont have Korn installed on my box at home so I cannot test anything right now. I can loaded it up later tonight if I have time but I usually dont go on my computers during the weekend.


----------



## Squashman (Apr 4, 2003)

http://aixpdslib.seas.ucla.edu/packages/bash.html


----------



## cgjoker (Aug 13, 2003)

I think I have it installed already because I do recall running a bash script I created a long time ago. I guess I can just try a simple script to see if it works.

I still don't know why though the reason id get these errors.. if you tried it with bash and I had it installed, it should run then shouldn't it?


----------



## Squashman (Apr 4, 2003)

cgjoker said:


> I still don't know why though the reason id get these errors.. if you tried it with bash and I had it installed, it should run then shouldn't it?


Because your default shell is Korn and you dont have the interpreter defined at the top of the script. You have to put this at the top of the script.

#!/path/to/bash (replace the path to bash with where it is on your system)


----------



## cgjoker (Aug 13, 2003)

Ill try this out. Thanks so much for all your time in trying to help me figure this out.


----------

