user3263699
user3263699

Reputation: 51

Recursive copying the files

I have a directory structure like

# parent directory
dir/

Under this directory there are three sub directories:

dir1/dirA/dirB/
dir2/dirC/dirD/
dir3/dirE/dirF/

I wish to copy a file recursively into the first subdirectory only dir1/ dir2/ dir3/ only and not to its subdirectories dirA/ dirC/ dirE/ and so on i am using

find . -type d -exec cp filename.txt {} \;

but it is also copying the files into the 'dir2' and 'dir'. I used the --min-depth parameter but no success. Any idea?

Upvotes: 0

Views: 170

Answers (2)

Konzulic
Konzulic

Reputation: 1763

ls -b --file-type | egrep "/$" | xargs -n 1 cp filename.txt

To explain, egrep is filtering directories from output, and then you pass it as parameter to the cp command. Of course keep in mind that filename.txt is in the CWD, or modify the path.

EDIT: This solution deals with spaces in the directory names.

Upvotes: 1

memnoch_proxy
memnoch_proxy

Reputation: 1954

If you have a file in /dir/ and you want to copy it recursively to the tree of directories in /dir/dir1, you need to change the source directory in your find command to be /dir/dir1, like so:

find dir -type d -mindepth 1 -maxdepth 1 -exec cp /dir/filename.txt {} \;

If your pattern for copying files gets more difficult, instead of copying right from find, you could print out the possible variations with -printf. For example:

find dir -type d -mindepth 1 -maxdepth 1 -printf "cp %p\n"

Hope this helps.

Or, using bash syntax, you could skip find and say:

cd $dir; for d in dir1 dir2 dir3 ; do
  cp filename.txt $d
done

Or just use three copy statements.

Upvotes: 0

Related Questions