LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   copy direcotry but ignore some files (https://www.linuxquestions.org/questions/linux-general-1/copy-direcotry-but-ignore-some-files-46458/)

gsbarry 02-21-2003 01:58 AM

copy direcotry but ignore some files
 
I need to copy a directory to other place, but inside the directory, I don't need to copy some type of files(e.g. *.txt, *.mp3), how to do?

Mik 02-21-2003 04:01 AM

I don't see any options in cp to get anything done like that. Here is a way you can do it with only copying files in one directory. I was trying to find a way to get it done recursively for each child directory but the only way I could think of was to write a script which recursively calls a copy for each directory.

Anyways here is one way of doing it for just the files in one directory.

find $SRCDIR -maxdepth 1 -type f ! -iregex ".*\.mp3$" ! -iregex ".*\.txt$" | xargs cp --target-directory $DESTDIR

Make sure to swap the $SRCDIR and $DESTDIR with the appropraite fields.

There really must be an easier way to get this done. But I think I'm too braindead right now to find a better solution.

Mik 02-21-2003 04:36 AM

Hmmm okay well I had to find a way to get it done so I wrote the following script:
Code:

#!/bin/bash

if [ $# -lt 2 ]
then
  echo "usage: cpit <src_dir> <dest_dir>"
  exit 1
fi

src_dir=`echo $1 | sed -e 's#/\$##'`
dest_dir=`echo $2 | sed -e 's#/\$##'`
ignore='! -iregex ".*\.txt$" ! -iregex ".*\.mp3$"'

find $src_dir -type f $ignore | while read file
do
  destfile=`echo $file | sed -e "s#${src_dir}/##"`
  mkdir -p ${dest_dir}/`dirname $destfile | sed -e "s/^.$//"`
  cp $file ${dest_dir}/$destfile
done

Hope you manage to use it.

gsbarry 02-22-2003 06:15 AM

Thanks very very much, your's methods are very useful


All times are GMT -5. The time now is 05:53 PM.