Reputation: 5291
Here is what I am trying to do - assume a source directory:
/xyz/2/file.txt
/xyz/2/crapfile.txt
/xyz/3/file.txt
/xyz/3/crapfile.txt
and I want to copy file.txt into the following directory:
/z/2
/z/3
I like to do something like this, but this doesn't work:
cp -r /xyz/*/file.txt /z/*/
to avoid copying things separately - which I can do with the following:
cp -r /xyz/2/file.txt /z/2/
cp -r /xyz/3/file.txt /z/3/
Upvotes: 1
Views: 101
Reputation: 113924
This command copies from xyz
to z
excluding all files named crapfile.txt
:
rsync -a --exclude=crapfile.txt xyz/ z
With the -v
(verbose) option, we can see its work in progress:
$ rsync -va --exclude=crapfile.txt xyz/ z
sending incremental file list
./
2/
2/file.txt
3/
3/file.txt
As you can see, file.txt
was copied but crapfile.txt
was skipped.
rsync
is a very powerful utility with many many options. For more info, see man rsync
or any of the rsync
tutorials on the web.
Upvotes: 1
Reputation: 7689
Based on my understanding of the the question , the below should do it:
[za]$ cp -rv xyz/{dir_1/,dir_2}/* target_dir/
`xyz/dir_1//file3_dir1.txt' -> `target_dir/file3_dir1.txt'
`xyz/dir_1//file4_dir1.txt' -> `target_dir/file4_dir1.txt'
`xyz/dir_2/file1.txt' -> `target_dir/file1.txt'
`xyz/dir_2/file2.txt' -> `target_dir/file2.txt'
`xyz/dir_2/file3.txt' -> `target_dir/file3.txt'
`xyz/dir_2/file4.txt' -> `target_dir/file4.txt'
[za]$ cp -rv xyz/{dir_1/,dir_2}/*.txt target_dir/
`xyz/dir_1//file3_dir1.txt' -> `target_dir/file3_dir1.txt'
`xyz/dir_1//file4_dir1.txt' -> `target_dir/file4_dir1.txt'
`xyz/dir_2/file1.txt' -> `target_dir/file1.txt'
`xyz/dir_2/file2.txt' -> `target_dir/file2.txt'
`xyz/dir_2/file3.txt' -> `target_dir/file3.txt'
`xyz/dir_2/file4.txt' -> `target_dir/file4.txt'
other ways depending on what you are trying to achive.
1)
[za temp_dir]$ cp -rv xyz/* target_dir/
`xyz/dir_1' -> `target_dir/dir_1'
`xyz/dir_1/file3_dir1.txt' -> `target_dir/dir_1/file3_dir1.txt'
`xyz/dir_1/file4_dir1.txt' -> `target_dir/dir_1/file4_dir1.txt'
`xyz/dir_2' -> `target_dir/dir_2'
`xyz/dir_2/file1.txt' -> `target_dir/dir_2/file1.txt'
`xyz/dir_2/file2.txt' -> `target_dir/dir_2/file2.txt'
`xyz/dir_2/file3.txt' -> `target_dir/dir_2/file3.txt'
`xyz/dir_2/file4.txt' -> `target_dir/dir_2/file4.txt'
2)
[za temp_dir]$ for x in $(find . -type f) ; do cp -v $x target_dir/ ; done
`./target_dir/dir_1/file3_dir1.txt' -> `target_dir/file3_dir1.txt'
`./target_dir/dir_1/file4_dir1.txt' -> `target_dir/file4_dir1.txt'
`./target_dir/dir_2/file1.txt' -> `target_dir/file1.txt'
`./target_dir/dir_2/file2.txt' -> `target_dir/file2.txt'
`./target_dir/dir_2/file3.txt' -> `target_dir/file3.txt'
`./target_dir/dir_2/file4.txt' -> `target_dir/file4.txt'
`./xyz/dir_1/file3_dir1.txt' -> `target_dir/file3_dir1.txt'
`./xyz/dir_1/file4_dir1.txt' -> `target_dir/file4_dir1.txt'
`./xyz/dir_2/file1.txt' -> `target_dir/file1.txt'
`./xyz/dir_2/file2.txt' -> `target_dir/file2.txt'
`./xyz/dir_2/file3.txt' -> `target_dir/file3.txt'
`./xyz/dir_2/file4.txt' -> `target_dir/file4.txt'
3)
[za temp_dir]$ rsync -rav xyz/* target_dir/
sending incremental file list
dir_1/
dir_1/file3_dir1.txt
dir_1/file4_dir1.txt
dir_2/
dir_2/file1.txt
dir_2/file2.txt
dir_2/file3.txt
dir_2/file4.txt
sent 414 bytes received 134 bytes 1096.00 bytes/sec
total size is 0 speedup is 0.00
[za temp_dir]$
4)
[za]$ find xyz/ | while read ; do cp -v $REPLY target_dir/ ; done
cp: omitting directory `xyz/'
cp: omitting directory `xyz/dir_1'
`xyz/dir_1/file3_dir1.txt' -> `target_dir/file3_dir1.txt'
`xyz/dir_1/file4_dir1.txt' -> `target_dir/file4_dir1.txt'
cp: omitting directory `xyz/dir_2'
`xyz/dir_2/file1.txt' -> `target_dir/file1.txt'
`xyz/dir_2/file2.txt' -> `target_dir/file2.txt'
`xyz/dir_2/file3.txt' -> `target_dir/file3.txt'
`xyz/dir_2/file4.txt' -> `target_dir/file4.txt'
sent 414 bytes received 134 bytes 1096.00 bytes/sec
total size is 0 speedup is 0.00
Upvotes: 0
Reputation: 2354
Use a for
loop with an ls
:
for I in `ls /xyz`; do mkdir /z/$I; cp -r /xyz/$I/file.txt /z/$I/ ; done
(Or if /z/2 and /z/3 is already existing skip mkdir
!)
Upvotes: 0
Reputation: 9
Instead of using a wild card, you could do something similar to:
for x in $(ls /xyz)
do
mkdir /z/$x
cp /xyz/$x/file.txt /z/$x/
done
If the folders under z do not already exist, otherwise remove the mkdir line above
Upvotes: 0