Home > bash > How to copy only certain files and subdirectories

How to copy only certain files and subdirectories

May 2Hits:1

Hi let's say I have a directory like this:

/     my_work                /dir1                     keepdir1.ddd                     keepdir2.ddd                     file.cfg                     (lots of files and directories I don't want)                /dir2                     (same layout as dir 1) 

So I want to copy the directory "my_work" and all of the "dir1", "dir2", etc sub directories. But within each of those I want to keep only certain files, and certain directories. In the directories I keep I want to recursively include everything.

Here's what I've tried so far but nothing seems to copy:

rsync -rl --include-from=~/rsync_include /my_work ~/backup 

And the contents of rsync_include are:

+ *file.cfg + *keepdir*/* - * 


Unless you forgot to provide important information, the following script will work.

#!/usr/bin/env bash

for DIR in my_work/* ; do
    mkdir -pv dest/"$DIR"
    cp -av "$DIR"/*.cfg "$DIR"/keepdir* dest/"$DIR"

Sample output, with non-applicable wildcards producing messages:

my_work/dir1/file.cfg -> dest/my_work/dir1/file.cfg
my_work/dir1/keepdir1.ddd -> dest/my_work/dir1/keepdir1.ddd
my_work/dir1/keepdir2.ddd -> dest/my_work/dir1/keepdir2.ddd
my_work/dir2/file.cfg -> dest/my_work/dir2/file.cfg
cp: my_work/dir2/keepdir*: No such file or directory
cp: my_work/dir3/*.cfg: No such file or directory
cp: my_work/dir3/keepdir*: No such file or directory

Tags:bash, rsync

Related Articles

Copyright (C) 2018 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.727 s.