Why does xargs with cp fail for recursion?
Matthew Martinez
I am trying to copy files from one directory into a whole collection of other directories, such that the set of files in the source directory will be present in every one of the destination directories.
I am trying to use cp with xargs to achieve this:
# bash on Ubuntu
$ ls -d exercises/*/test/vendor/ | xargs cp ../Unity-2.5.0/src/unity*I understand this command to mean:
- list all the directories that match
exercises/*/test/vendor/ - for each of those directories listed
- copy into the directory, the files matching
../Unity-2.5.0/src/unity*
- copy into the directory, the files matching
The directory ../Unity-2.5.0/src/unity* does not contain any sub-directories, only files
The directories exercises/*/test/vendor/ do not contain any sub-directories, only files
Testing each of the parts of this command separately acts as intended.
Yet when run, the command outputs cp: -r not specified; omitting directory 'exercises/{exercise-name}/test/vendor/' for every one of the destination directories.
Why? Where is it seeing any directory recursion in what I am asking it to do?
To be clear, I am not asking for alternative methods to achieve my aim - I want to understand why this method does not work and how I can adjust this method to work (if possible).
1 Answer
D'oh! Should have read man xargs. It's not immediately clear from a basic understanding of xargs (that I have), but xargs tries to be clever and use multiple arguments at a time.
To prevent this I need to add -n1 argument to tell xargs to use at most 1 argument per iteration.
# bash on Ubuntu
$ ls -d exercises/*/test/vendor/ | xargs -n1 cp ../Unity-2.5.0/src/unity* 1