I have a directory named dir1 which has about 800 sub-directories named disp-001, disp-002, ... disp-800. I get the list of sub-directories that does not contain a file stdout based on solution given here as

$ find . -type d \! -exec test -e '{}/stdout' \; -print

I would now like to copy the sub-directories that do not contain the file into another directory dir2. I use xargs to accomplish this as

$ find . -type d \! -exec test -e '{}/stdout' \; -print |xargs -I cp -r {} ../dir2

However this throws me an error xargs: {}: No such file or directory

How can I copy these sub-directories to another directory


No need for xargs; find can handle that:

find . -mindepth 1 -type d \! -exec test -e '{}/stdout' \; -exec echo cp -r '{}' ../dir2 \;

or with a modern version of find, \+ will make find do an xargs-like thing (or, the horror, 800 forks!)

  • You need -mindepth 1 to exclude the ./ folder which otherwise would copy all the sub-directories.
  • The -exec command contains a debug echo option to see the process to be carried out before you perform the copy
  • They are directories so I added -r after cp. It prints the correct output but the copy does not seem to happen. Can you verify if it works for you? – rambalachandran Sep 22 '15 at 15:27
  • There's a debugging echo, which is handy to verify in advance how you're about to destroy your filesystem. – thrig Sep 22 '15 at 15:30
  • My bad. There is an issue, since the output also produces the folder ./ which means it will perform cp -r ./ ../dir2 which in turn will copy all the sub-folders irrespective of presence of the file – rambalachandran Sep 22 '15 at 15:35
  • -mindepth 1 should exclude ./ – thrig Sep 22 '15 at 16:01
  • Great that works. – rambalachandran Sep 22 '15 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.