Is it possible to copy a single file to multiple directories using the cp command ?
I tried the following , which did not work:
cp file1 /foo/ /bar
Wildcards also work with Roberts code
echo ./fs*/* | xargs -n 1 cp test
Suppose you want to copy fileName.txt
to all sub-directories within present working directory.
Get all sub-directories names through ls
and save them to some temporary file say, allFolders.txt
ls > allFolders.txt
Print the list and pass it to command xargs
.
cat allFolders.txt | xargs -n 1 cp fileName.txt
To use copying with xargs
to directories using wildcards on Mac OS, the only solution that worked for me with spaces in the directory name is:
find ./fs*/* -type d -print0 | xargs -0 -n 1 cp test
Where test
is the file to copy
And ./fs*/*
the directories to copy to
The problem is that xargs sees spaces as a new argument, the solutions to change the delimiter character using -d
or -E
is unfortunately not properly working on Mac OS.
If you need to be specific on into which folders to copy the file you can combine find with one or more greps. For example to replace any occurences of favicon.ico in any subfolder you can use:
find . | grep favicon\.ico | xargs -n 1 cp -f /root/favicon.ico