Reputation: 2510
Example: merge dirA/ dirB/ dirDEST/
All existing files (not directories) in dirA and in dirB must be moved to dirDEST. If two files with the same name appear in both dirA and dirB, the file moved to dirDEST must be the most recent modified one, the other must remain in the source directory.
When I have the list files from dirA and dirB (e.g find /dirA -type f
and find dirB/ -type f
) how can I compare e select the right files and move with mv?
For compare modification time of files I could use:
if [ "$file1" -ot "$file2" ]; then
...
fi
Upvotes: 0
Views: 153
Reputation: 23826
First create a union of all files and then go through the list and check for the three cases
This is an example without find
just using globing. This means it works only for one directory level.
#! /bin/bash
union()
{
local f
for f in dir{A,B}/*; do
basename "$f"
done |
sort -u
}
for f in $(union); do
a="dirA/$f"
b="dirB/$f"
if [ -e "$a" ]; then
if [ -e "$b" ]; then
# file exists in dirA and dirB
if [ "$a" -nt "$b" ]; then
mv "$a" dirC/.
else
mv "$b" dirC/.
fi
else
# file exists in dirA but not in dirB
mv "$a" dirC/.
fi
else
# file exists in dirB but not in dirA
mv "$b" dirC/.
fi
done
Upvotes: 1