LinuxQuestions.org
Help answer threads with 0 replies.
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 07-26-2006, 04:23 AM   #1
Luke_C
LQ Newbie
 
Registered: Jul 2006
Location: Devon UK
Distribution: RHE and Fedora maninly
Posts: 24

Rep: Reputation: 15
Excluding Folders with CP


Hi All,
I am in the middle of writing a little script that will cp some data to a other location for a temp back up. But I have a folder in there I dont want moved or copied is there a command for CP that will let me exclude this folder.

Any help would be greatful.
 
Old 07-26-2006, 04:30 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
The only way to work around that would be to get a list of the files, exclude
the directory and then feed that list to a cp ... or just use a find.


Cheers,
Tink

P.S.: Who would the help be grateful to? :}
 
Old 07-26-2006, 04:53 AM   #3
Luke_C
LQ Newbie
 
Registered: Jul 2006
Location: Devon UK
Distribution: RHE and Fedora maninly
Posts: 24

Original Poster
Rep: Reputation: 15
Any thing else dude as some folders may get added or taken away and I want this to be in a cron that runs at night.

And I would be greatful of course.
 
Old 07-26-2006, 05:18 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
well,
for i in $(find . | egrep -v '^./path/to/excluded/dir/' ); do
cp "$i" "/path/to/backup/dir/$i"; done
 
Old 07-26-2006, 05:34 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
please post a sample of a list of directories then tell us the directory or directories you want to exclude.

in the format

/home/user/a/b/c
 
Old 07-26-2006, 05:41 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
find -type f -not -regex "/dir/to/exclude/.*' -exec cp -p {} /path/to/target \;

Untested, but you get the idea.


Cheers,
Tink
 
Old 07-26-2006, 05:47 AM   #7
Luke_C
LQ Newbie
 
Registered: Jul 2006
Location: Devon UK
Distribution: RHE and Fedora maninly
Posts: 24

Original Poster
Rep: Reputation: 15
Ok the folder I want to miss out is

/srv/www/htdocs/mod/object/repository

and I want to scan the /srv/www directory.

Still cant get it working the thing I came up with is this

ls -Rl /srv/www > test | grep test * --exclude 'repository' > test2

which works but takes forever.

Last edited by Luke_C; 07-26-2006 at 06:33 AM.
 
Old 07-26-2006, 06:42 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
i don't know which one of the following works. i'm not in linux so pls just try them.

Code:
find /srv/www -path /srv/www/htdocs/mod/object/repository -prune -o print
OR

Code:
find /srv/www -regex ".*htdocs/mod/object/repository$" -prune -o -print
Edit: btw in /srv/www which is the parent directory to copy? src or www?

play around with cp --parent to copy the full path. example if the parent directory's www, then cd /srv, cp --parent -a www/some/directory /new/srv/

and one more thing i'd love to make a script for you but i kinda have a headache today. you can study my previous post. it's got a similar purpose:

http://www.linuxquestions.org/questi...d.php?t=465904

good luck there

Last edited by konsolebox; 07-26-2006 at 06:59 AM.
 
Old 07-26-2006, 07:13 AM   #9
Luke_C
LQ Newbie
 
Registered: Jul 2006
Location: Devon UK
Distribution: RHE and Fedora maninly
Posts: 24

Original Poster
Rep: Reputation: 15
Right this is what I have now gone with and works fine for what I want it for.

Thanks for the help dudes.

Code:
find /srv/www -wholename './htdocs/mod/object/repository' -prune -o -print

Last edited by Luke_C; 07-26-2006 at 07:47 AM.
 
Old 07-26-2006, 08:06 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by Luke_C
Code:
find /srv/www -wholename './htdocs/mod/object/repository' -prune -o -print
just curious. how will you use this statement to copy files? are you going to use a for loop or find's -exec?
 
Old 07-26-2006, 08:33 AM   #11
Luke_C
LQ Newbie
 
Registered: Jul 2006
Location: Devon UK
Distribution: RHE and Fedora maninly
Posts: 24

Original Poster
Rep: Reputation: 15
-exec and that works fine no dude. : )
 
Old 07-26-2006, 09:57 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
ok here's a script. check it first before you use it.

xcp.sh
Code:
#!/bin/bash
#

# xcp.sh (c) 2006 konsolebox
# exclusive copier script for Luke_C
# version 1.0
# GPL license

# parent directory
PARENTDIR=www
# containing source directory
CSOURCEDIR=/src
# containing target directory
CTARGETDIR=/tmp/src
# directory to exclude
EXCLUDEDIR=www/htdocs/mod/object/repository

VERBOSE=1

pushd "${CSOURCEDIR}" > /dev/null
IFS=$'\n'

for a in $(find "${PARENTDIR}" -path "${EXCLUDEDIR}" -prune -o -print); do
	[ "${VERBOSE}" ] && echo "${PARENTDIR}/$a"
	if [ -d "$a" ]; then
		# make the directory
		mkdir -p "${CTARGETDIR}/$a"
	else
		# copy the file
		cp --parent "$a" "${CTARGETDIR}"
	fi
done

IFS=$' :\t\n'
popd > /dev/null
regards
 
Old 07-27-2006, 02:36 AM   #13
Luke_C
LQ Newbie
 
Registered: Jul 2006
Location: Devon UK
Distribution: RHE and Fedora maninly
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks for your time to write this but I have already made a simple one. But again many tahnks for all your help.
 
Old 07-27-2006, 03:34 AM   #14
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
Luke_c, I know you asked about cp, but it seems
rsync is better at what you want.
--exclude "foo/" would exclude any directory called foo
 
Old 07-27-2006, 04:07 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
i was just thinking that if you have a list like:
Code:
www/htdocs/mod/object
www/htdocs/mod/object/repository
www/htdocs/someotherdirs
and exclude 'www/htdocs/mod/object/repository', then doing cp --parent -a to the list will still include www/htdocs/mod/object/repository if you hit www/htdocs/mod/object

but it's up to you anyway

you can also use rsync as Emmanuel_uk said.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
excluding directories from commands David the H. Linux - General 5 04-06-2009 04:21 PM
Excluding specific folders from backup karloslambchop Ubuntu 7 07-21-2006 09:10 PM
Samba can create new files and folders but access denied in any new folders k.king Linux - Networking 2 01-15-2006 07:14 AM
tar and excluding files murshed Linux - Newbie 7 03-15-2003 03:32 PM
DHCP excluding IP addresses AndyJ Linux - Networking 8 03-03-2003 04:09 PM

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

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration