LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-21-2003, 01:58 AM   #1
gsbarry
Member
 
Registered: Jan 2003
Posts: 42

Rep: Reputation: 15
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?
 
Old 02-21-2003, 04:01 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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.
 
Old 02-21-2003, 04:36 AM   #3
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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.
 
Old 02-22-2003, 06:15 AM   #4
gsbarry
Member
 
Registered: Jan 2003
Posts: 42

Original Poster
Rep: Reputation: 15
Thanks very very much, your's methods are very useful
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
apache 2, rootdirectory must be a direcotry...? jollyjoice Linux - Networking 2 05-21-2005 08:40 AM
authenicate MySQL with Active Direcotry ken80 Linux - Networking 4 03-12-2005 11:04 PM
apache default public html direcotry true_atlantis Linux - Software 1 10-23-2004 07:46 PM
Removing files wihtout removing containing Direcotry caps_phisto Linux - General 2 10-07-2004 08:16 AM
how to copy and paste between files and within files Bheki Linux - General 1 02-05-2002 05:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration