1

I'm trying to build a scrip that compare a file inside different 2 dir,then copy them from dir1 to dir2 is it's not exists in the dir2, or overwrite it if its already existed.

I know that i can use diff dir1 dir2 but then how do copy the result over to dir2. because diff command give me result like only in dir1: file1.txt, from my understand that isn't a file itself(right?)

For the overwrite part, i decide to use [ -ot ] command, but again, from my understand the [ -ot ] only can compare with file but not dir?

2

2 Answers 2

2

rsync works locally and does tests on both file timestamps and file sizes.

rsync --archive /path/to/source/ /path/to/destination/

You may also use rsync on individual files.

11
  • -u is the right option for timestamps comparison ? Commented Sep 28, 2017 at 8:41
  • @PrinPuyakul -u will skip files that are newer on the destination end. Without it, rsync will transfer all files whose timestamps (or sizes) are different. Commented Sep 28, 2017 at 8:45
  • I see -a, --archive is what i'm looking for, but I just wonder how can I printout the result from rsync after it successfully execute (like a file name) @Kusalananda Commented Sep 28, 2017 at 13:52
  • @PrinPuyakul You may use -v or --verbose to see what it's doing, and --progress to see it work on bigger files. --stats will give you a summary at the end. Commented Sep 28, 2017 at 13:53
  • I did try --archive, but it will copy everything over to a new dir every tho the file has an older timestamp, what did i do wrong ?_?. from what I understand it NOT suppose to copy older timestamp file over the new one right? Commented Sep 29, 2017 at 6:52
1

Why not cp?

\cp -f /path/to/dir1/* /path/to/dir2/
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.