I have tried to copy a file test.txt
to multiple directories with one command:
cp ~/test.txt ~/folder1 ~/folder2
But I didn't succeed. Is there a way to do that in one command so I can copy a file or even a folder to multiple directories?
I have tried to copy a file test.txt
to multiple directories with one command:
cp ~/test.txt ~/folder1 ~/folder2
But I didn't succeed. Is there a way to do that in one command so I can copy a file or even a folder to multiple directories?
cp
can copy from multiple sources, but can't copy to multiple destinations. Seeman cp
for more info.The only bash command that I know which can copy/save to multiple destinations is
tee
.You can use it in your case as follows:
Note that
tee
also writes the input to the standard output (stdout). So if you don't want this, you can prevent it by redirecting standard output to/dev/null
as follow:Another way to achieve a copy to multiple locations is the following command :
If dir1 or dir2 have sub-directories that you don't want the file copied into, add
-maxdepth 0
option :Note that this will overwrite every file in
dir1
anddir2
withfile.txt
's contents, in addition to copying it. To only copyfile.txt
without affecting other files in these directories, tellfind
to only act on directories:The command
tries to copy two files (
~/test.txt
and~/folder1
) to the destinationfolder2
. (And if~/folder2
exists and is a directory you will have an "omitting directory" warning).If you want to make multiple copies of the file
test.txt
, you have to use a loop or multiple commands...(...and be careful if you have spaces embedded in the file names, you'll need quoting).
To copy whole directories you have to use the
-r
option:this will create
~/folder1/folder3
and~/folder2/folder3
with all the files included.You can create a help script , or you can do it with
xargs
and a print function (in this case,echo
):This will make each directory as an argument to the
cp
function , using test file as a parameter.After a long search this work like a Charm also !
This will copy file.txt to every directory in your current location in terminal.
This will copy a folder to every sub directory in your current location in terminal.
I share it hope it helps others too .
If you want to copy the file test.txt in every directory in /tmp/target/ ...
create a test environment:
copy it:
Just thought to give a variation to the answer of
Sylvain Pineau
where
dir1
anddir2
are not in your current directory.find ./ -maxdepth 2 -type d -name dir1 -exec cp file.txt {} \;
here find will look for
dir1
two levels deep or you can leave out-maxdepth
parameter to finddir1
in all folders in current directory and below it.For files only without preserving attributes. Great if you're copying huge files and only want to read them once:
For files or directories
I needed to post html index files in the home directory of 10 newly created sub-domains. I created a file with the markup called
sites-index
and placed it in my working directory and another calledsites-home
that contained a list of full paths to the copied files on each line (eg/var/path/to/site1/public_html/index.html
).After a bit of searching here I lighted on the following bash script:
The script loops through the list, copies the html file at each iteration and sets each line in the list as the new path to which the html is copied.
To run in terminal, type
sudo -i
to start a root shell (enter your password when prompted). Execute the script.Alternatively, you can save the command in a text file, for example
index.sh
and invoke it by typing at the terminal prompt,sudo bash index.sh
.If you run the script without
sudo
you'll see a list of the files that weren't created due to insufficient permission. If it executes correctly your files will be created without notice.