I'm trying to replicate select files from a directory structure as symlinks in a duplicate directory, eg
from a directory structure such as this:
../1/a/data1.txt
../1/a/file1.txt
../1/a/b/file2.txt
make this:
../2/a/file1.txt -> ../../1/a/file1.txt
../2/a/b/file2.txt -> ../../2/a/b/file1.txt
eg keep the dir tree and skip files matching various patterns.
when I use GNU cp from the target directory (../2/a) , replacing relative paths, everything works like I want it, except that it copies data1.txt:
gcp -rs $(cd ../..; pwd)/1/a/* $PWD/
but when I use this in find + xargs to exclude certain files,
find -X -f $(cd ../..; pwd)/1/a \! -name '*data*' -type f | xargs -t -n1 -I % gcp -s -t $PWD/ %
I get this:
../2/a/file1.txt -> ../1/a/file1.txt
../2/a/file2.txt -> ../1/a/b/file2.txt
eg the directory structure is not being replicated. I can imagine tedious work-arounds but I must be missing something essential in the syntax of xargs. (I want to use find because files before and after certain dates get different treatments.)
my solution was to call subshell, construct paths, make directories, make links...roughly (eg. not tested using the example here):
find $(cd ../..; pwd) -type f \! -name '*data*' -print0 | xargs -0 bash -c
'while [ -n "$1" ]; do
TMP="${1%$(basename $1)}";
mkdir -p "${TMP/1/2}";
gcp -s "$1" "${1/1/2}";
shift;
done;' "bash"
since this was called inside a script, before the command I exported the relevant path names ("1" and "2" here were actually $PATH1, $PATH2; confusing in this example b/c of $1 vs "1" and "2").