1

I am looking for a way to move folders that are not already present in the target directory on my linux server.

Example:

Target directory

folder1
folder2
folder3
folder4

Source directory

folder1
folder2
folder5
folder6
folder7

I do not want folder1 and folder2 from the source directory to overwrite the folders in the target directory. The contents within folder1 and folder2 in the source directory may be different than in the target directory but I don't want these changes copied.

This is on a CentOS 7 server and any tool needed for the job is accepted.

The list of folders not to move is dynamic so I don't want to set it up statically.

2
  • I think you may have to do this programmatically (bash or python script, or whatever).
    – Halfgaar
    Aug 19, 2015 at 19:31
  • @Halfgaar - That's also fine as it's also written in the question any tool needed for the job is accepted.
    – Frederik
    Aug 19, 2015 at 19:39

1 Answer 1

1

You can do this by setting up a loop through each directory in the source and if there's no directory in the target by that name, it moves it: bourne snippit:

for dir in `cd "$sourcedir" ; ls -1F | grep '/'`
do
  if [ ! -d "${targetdir}/${dir}" ]
  then
    mv "$dir" "${targetdir}/"
  fi
done

Note that this will either break or overwrite a file if you have a file in the target with the same name as a directory in the source. If you want it robust, you should add some additional checks.

1
  • 2
    This will fail on directories with spaces. I would use find -print0 and xargs.
    – Halfgaar
    Aug 20, 2015 at 6:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .