-2

How do I use the below properly: I want to find all files belonging to user and copy them in a folder.

find / -user Joe | xargs -I {} cp {} /home/Joe/Folder

The commands hangs and on ctrl C it prints a 0 that represents something.

Tajinder
  • 2,248
  • 4
  • 33
  • 54

1 Answers1

1
find / -user Joe -exec cp {} /home/Joe/Folder \;

This will also copy for instance filenames with whitespace in them.

If you insist on using xargs as well:

find / -user Joe -print0 | xargs -0 -I{} cp {} /home/Joe/Folder

find's option -print0 will make it terminate filenames with a nul, while for xargs -0 will make it accept (only) filenames ending in nul. The effect of this is to ensure that for instance filenames with whitespace in them will also be seen correctly by xargs, and will be acted upon accordingly (in this case copied elsewhere).

Finally, please be aware that your cp command as is will not copy directories or their contents; you would need the additional cp options -dR. See man 1 cp.

  • Thank your very much for the explanation and example – Jermain Singleton Jul 22 '19 at 16:38
  • I am running into an issue sometimes when using the above command find / -user Joe -exec cp {} /home/Joe/Folder \; No disk space left on drive. I am guessing I need to make sure the partition is very large, there is 8Gb. Also when I run the find command by itself it pulls like 200 files but when I run the command in full it pulls way more then intended. Curious why? – Jermain Singleton Aug 09 '19 at 11:47