Nepal12
Nepal12

Reputation: 593

Bash copy all directory with content that matches a pattern

Is there some way to copy the directories including the contents using bash script. For example

// Suppose there are many directory inside Test in c as,
   /media/test/
        -- en_US
                -- file1
                -- file 2 
        -- de_DE 
               -- file 1
               -- SUB-dir1
                  -- sub file 1
               -- file 2
               .....
               .....
        -- Test 1
              --  testfile1
              -- folder
                   --- more 1  
         ............

NoW i want to copy all the directories (including sub-directory and files)
to another location which matches the pattern.
--> for example , in above case I want the directories en_US and de_DE to be copied in another
location including sub-directories and files. 

So Far I have done/ find out :

1) Needed Pattern as , /b/w{2}_/w{2}/b

2) I can list all the directories as ,

$MYDIR="/media/test/"
DIRS=`ls -l $MYDIR | egrep '^d' | awk '{print $10}'`
for DIR in $DIRS
do
echo  ${DIR}
done

Now I need help in combining these together so that the script can copy all the directory(including sub contents) that matches the pattern to another location.

Thanks in advance.

Upvotes: 0

Views: 2775

Answers (4)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

Here is your starter for 10:
You will have to add the extra checks and balances that you require but it should give you a flying start.

#!/bin/bash
# assumes $1 is source to search and $2 to destination to copy to
subdirs=`find $1 -name ??_?? -print`
echo $subdirs
for x in $subdirs
do
        echo $x
        cp -a $x $2
done

Upvotes: 1

Kaz
Kaz

Reputation: 58588

To selectively copy an entire directory structure to a similar directory structure, while filtering the contents, in a general way your best bet is to archive the original directory and unarchive. For instance, using GNU Tar:

$ mkdir destdir
$ tar -c /media/test/{en_US,de_DE} | tar -C destdir -x --strip-components=1

In this example, the /media/test directory structure is partially recreated under destdir, excluding the /media prefix (thanks to --strip-components=1).

The left side tar archives just the directories/paths which match the pattern that we specified. The archive is produced on that command's standard output, which is piped to the decoding tar on the right hand side. The -C tells it to change to the destination directory. It extracts the files there, removing a leading path component.

$ ls destdir
test
$ ls destdir/test
en_US de_DE

Of course, your specific example test case is quite easily handled with cp -a:

$ mkdir destdir
$ cp -a /media/test/{en_US,de_DE} destdir

If the pattern is complicated, involving multiple selections of subtree material at deeper and/or different levels of the source directory hierarchy, then you need the more general approach, if you wish to do the copy in a single batch command which just specifies source patterns.

Upvotes: 2

soumyaranjan
soumyaranjan

Reputation: 101

Please check if this is what you wanted. It searches for directories with format xx_yy/ab_cd/&&_$$ (2char_2char) and copies the content to a new directory .

usage : ./script.sh

cat script.sh

#!/bin/bash

MYDIR="/media/test/"
NEWDIRPATH="/media/test_new"
DIRS=`ls -l $MYDIR | grep "^d" | awk '{print $9}'`
for DIR in $DIRS
do
        total_characters=`echo $DIR | wc -m`
        if [ $total_characters -eq 6 ]; then
                has_underscore=`echo "$DIR" | grep "_"`
                if [ "$has_underscore" != "" ]; then
                        echo "${DIR}"
                        start_string_count=`echo $DIR | awk -F '_' '{print $1}' | wc -m`
                        end_string_count=`echo $DIR | awk -F '_' '{print $2}' | wc -m`
                        echo "start_string_count => $start_string_count ; end_string_count => $end_string_count"
                        if [ $start_string_count -eq 3 ] && [ $end_string_count -eq 3 ]; then
                                mkdir -p $NEWDIRPATH/"$DIR"_new
                                cp -r $DIR $NEWDIRPATH/"$DIR"_new
                        fi
                fi
        fi
done

Upvotes: -1

Thomas Erker
Thomas Erker

Reputation: 348

I'm not sure about your environment, but I guess you try to do this:

cp -r src_dir/??_?? dest_dir

Upvotes: 2

Related Questions