How can I make two directories with some file in them, and copy the file between them?
If those files have the same name then an error should be thrown.
How can I make two directories with some file in them, and copy the file between them?
If those files have the same name then an error should be thrown.
Without error notification:
mkdir test1 test2
cp --no-clobber test1/* test2/
cp --no-clobber test2/* test1/
if those files has same name show error
part of the question.
– Jeff Schaller
Jan 31 '18 at 16:47
With error notification:
mkdir test1 test2
for i in `ls test1/`; do
if [ ! -e "test2/$i" ] ; then cp "test1/$i" test2/
else echo "ERROR: test2/$i already exists" >&2
fi
done
for i in `ls test2/`; do
if [ ! -e "test1/$i" ] ; then cp "test2/$i" test1/
else echo "ERROR: test1/$i already exists" >&2
fi
done
Take the list of files from 2 both directories and save it in 2 files
Directory1_files.txt,Directory2_files.txt.
Let us assume you need to copy the files from Directory1 to Directory2. It should copy only the files from Directory 1 which is not present in Directory 2
find First_directory_path -maxdepth 1 -type f | awk -F "/" '{print $NF}' > Directory1_files.txt
find Second_directory_path -maxdepth 1 -type f | awk -F "/" '{print $NF}' > Directory2_files.txt
awk 'NR==FNR {a[$1];next}!($1 in a) {print $1}' Directory2_files.txt Directory1_files.txt >Files_need_to_copy_to_directory_2
awk '{print "cp" " " "directory1path/"$1 " " "directory2path"}' Files_need_to_copy_to_directory_2| sh