LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-30-2007, 05:55 AM   #1
sri1025
LQ Newbie
 
Registered: Jul 2007
Posts: 1

Rep: Reputation: 0
Question Copying files and sub-directories of a directory except the directories named ".abc"


How to copy all the files and sub-directories of a directory except the directories named ".abc"?

Thank you!
 
Old 07-30-2007, 08:12 AM   #2
khaan
LQ Newbie
 
Registered: Feb 2007
Distribution: ubuntu
Posts: 24

Rep: Reputation: 15
You may perhaps copy everything recursively and then delete similary what you did not want:
cp -r srcPath destPath
rm -r `find destPath -name '.abc'`



But to avoid copying and then deleting (for example if ".abc" are large files), I don't know if there are easier solutions, but I would make a shell script. A once-usable version (which could be improved by handling parameters) would look like this:

Code:
#!/bin/sh

# use find to list all files from the source location
# and grep -v to remove those called ".abc"
# and store the list into a variable: 
flist=`find mySrc/path/ | grep -v '\<\.abc\>'`

# foreach item we want to copy:
for fname in $flist; do
    # we compute the corresponding destination path:
    destname=`echo " "$fname | sed 's/ mySrc\/path\// dest\/location\//'`
    if [ -d $flist ] ; then
        # if the item is a directory, we create it:
        mkdir $destname
    else
        # if not a directory, we copy it:
        cp $fname $dest
    fi
done
I put in blue what you may want to change (or use parameters instead of). Don't forget to escape with a backslash every slash and dot in the grep and sed commands as I did.
The "\<" and "\>" around "\.abc" just means that the name of the file or directories to skip is ".abc" and not just any name containing ".abc" somewhere inside.
Once your script done, you can launch it in the usual ways: either use sh myScriptName or make your script executable chmod u+x myScriptName (only once) and then launch it with ./myScriptName provided you are in its directory.
Also remember that if you use relative paths in the blue zones of the script, they will be relative to the path you are launching your script from.
 
Old 08-24-2010, 09:53 AM   #3
sledgeas
LQ Newbie
 
Registered: Dec 2007
Posts: 6

Rep: Reputation: 0
Wink

Sorry for digging old grave. But did not find anything relevant elsewhere.

I have updated this script into a working and a universal one:

Code:
USAGE: sh ./selective-recursive-copy.sh SRCDIR DESTDIR EXCEPT_PATTERN

Where EXCEPT_PATTERN is a grep -E style pattern. I.e. to avoid copying directories, which contain abc and 123, use:
sh ./selective-recursive-copy.sh from/path to/my/path "abc|123"
Code:
#!/bin/sh

# use find to list all files from the source location
# and grep -Ev to remove those called EXCEPT_PATTERN (in 3rd cmdline argument)
# and store the list into a variable: 
flist=`find $1 | grep -Ev $3`

# dealing with slashes in paths:
src=$(echo $1 | sed 's/\//\\\//g')
dest=$(echo $2 | sed 's/\//\\\//g')

# dealing with spaces in files:
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# foreach item we want to copy:
for fname in $flist; do
    # we compute the corresponding destination path:
    destname=$(echo " "$fname | sed "s/ $src/$dest\//g")
    if [ -d $fname ] ; then
        # if the item is a directory, we create it:
        #echo "mkdir $destname"
        mkdir $destname
    else
        # if not a directory, we copy it:
#        echo "cp $fname $destname"
        cp $fname $destname
    fi
done
IFS=$SAVEIFS
Thanks to khaan for starting this!

--&nbsp;
sledge
 
  


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
Advice? Best way to move files daily to a daily "date" named directory ziphem Linux - Newbie 2 04-15-2007 09:03 AM
Samba: "homes" share, cannot create directories, can create files Herg Linux - Software 1 09-14-2006 09:48 AM
K3b: - Howto re-dock "Directories" and "Contents" windows back into the main window? hagies Linux - Software 4 04-26-2006 09:38 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM
Why doesn't a wildcard chmod change "dot" files/directories? jht2k Linux - General 1 08-09-2004 03:31 PM

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

All times are GMT -5. The time now is 05:49 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