3

At the simplest, if I execute

find . -type f -exec cp {} /new/path/{}

The path that is expanded is /new/path/./path/to/file. I would like to remove that ./ that is prefixed by the find command before I use {} in the exec.

I am using the builtin Freebsd find, but I do have access to gnufind if that will help (though I do not normally use gnufind).

lbutlr
  • 414
  • 6
  • 18

1 Answers1

0

Where you will have a problem is when find descends into subdirectories, and it tries to exec something like cp ./foo/bar.txt /new/path/./foo/bar.txt and "/new/path" has no subdirectory "foo" -- you might want to:

  • specify -maxdepth 1 so you do not descend into subdirs

    find . -maxdepth 1 -type f -exec cp {} /new/path/{} \;
    
  • just use a directory destination for cp, so the files end up in a single dir (will suffer from collisions if you have "./foo/bar.txt" and "./qux/bar.txt")

    find . -type f -exec cp -t /new/path {} +
    
  • use tar to copy the whole tree: this will preserve directory structure

    tar cf - . | ( cd /new/path && tar xvf - )
    
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    I stripped the example down as much as possible, but none of these solutions work for what I need. I need to eliminate the './' prefix that find places in {}. The only way I can see to do it is to do something like `for i in find....; do ; ; done ` but that is less than ideal for my needs. If it's not possible, that's fine, I'll figure out something else. – lbutlr Mar 13 '15 at 01:39
  • @lbutlr You stripped down the question to the point where it's unclear. `find . -type f -printf '%P\0' | xargs -0 ...` might work, but I really have no idea – Reinstate Monica Please Mar 14 '15 at 20:28
  • @lbutlr Also `for i in $(find ...)` is extremely error prone, if looping you want something like `find ... -print0 | while IFS= read -r -d $'\0' i; do ; ; done` – Reinstate Monica Please Mar 14 '15 at 20:36