Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying only newer files 2

Status
Not open for further replies.

Einstein47

Programmer
Nov 29, 2001
737
US
I have two directories. One directory has files from one souce, and the other has "living" files that have been "underdevelopment". I need to move all the "living" files that are newer into the source directory. I don't want to move any files that don't exist in the source directory, nor do I want to move any files that are older than the source directory.

Any help would be appreciated - even just a point in the right direction.

Thanks, Einstein47
(How come we never see the headline, "Psychic Wins Lottery"?)
 
Perhaps something like ...
Code:
for fname in $(ls sourcedir)
do
  if [ devdir/$fname -nt sourcedir/$fname ]
  then
     cp devdir/$fname sourcedir/$fname
  fi
done

The -nt condition is satisfied if file1 exists and is newer than file2. Look at the man page for test for a full list of testable conditions. I find it one of the very useful man pages :)

Greg.
 
Thanks I will - I use the -f, -s, and -z conditions a lot for files, but I have never used -n or -t

You livesafer you. Einstein47
(How come we never see the headline, "Psychic Wins Lottery"?)
 
You can use

if -a filename
then
[do some action here]
fi

to see if a file exists

You can use the diff command to see
if files are different.
Something like.
VALUE=`diff -s file1 file2`
if [ ${#VALUE} -gt 0 ]
then
echo "files are different"
fi

I can't think of a command that tells if one file
is newer than another. (doesn't mean that there isn't one)
I know ftp has this option. newer as opposed to get.

You may have to have a script to convert the files date/time stamp format for comparison.

Robert
Robert G. Jordan
Unix Sys Admin
Sleepy Hollow, Illinois U.S.A.
sh.gif


FREE Unix Scripts
 
I tried the script idea and that failed miserably - grega's code worked like a charm. It was exactly what I wanted.

Funny that the test command would have that much power. I'm glad it does. Einstein47
(How come we never see the headline, "Psychic Wins Lottery"?)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top