1

I was thinking if using a BASH script is possible without manually copying each file that is in this parent directory

 "/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk
/System/Library/PrivateFrameworks"

So in this folder PrivateFrameworks, there are many subfolders and in each subfolder it consists of the file that I would like to copy it out to another location. So the structure of the path looks like this:

-PrivateFrameworks  
     -AccessibilityUI.framework 
        -AccessibilityUI <- copy this
     -AccountSettings.framework
        -AccountSettings <- copy this  

I do not want the option of copying the entire content in the folder as there might be cases where the folders contain files which I do not want to copy. So the only way I thought of is to copy by the file extension. However as you can see, the files which I specified for copying does not have an extension(I think?). I am new to bash scripting so I am not familiar if this can be done with it.

2 Answers 2

2

To copy all files in or below the current directory that do not have extensions, use:

find . ! -name '*.*' -exec cp -t /your/destination/dir/ {} +

The find . command looks for all files in or below the current directory. The argument -name '*.*' would restrict that search to files that have extensions. By preceding it with a not (!), however, we get all files that do not have an extension. Then, -exec cp -t /your/destination/dir/ {} + tells find to copy those files to the destination.

To do the above starting in your directory with the long name, use:

find "/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/PrivateFrameworks" ! -name '*.*' -exec cp -t /your/destination/dir/ {} +

UPDATE: The unix tag on this question has been removed and replaced with a OSX tag. That means we can't use the -t option on cp. The workaround is:

find . ! -name '*.*' -exec cp {} /your/destination/dir/ \;

This is less efficient because a new cp process is created for every file moved instead of once for all the files that fit on a command line. But, it will accomplish the same thing.

MORE: There are two variations of the -exec clause of a find command. In the first use above, the clause ended with {} + which tells find to fill up the end of command line with as many file names as will fit on the line.

Since OSX lacks cp -t, however, we have to put the file name in the middle of the command. So, we put {} where we want the file name and then, to signal to find where the end of the exec command is, we add a semicolon. There is a trick, though. Because bash would normally consume the semicolon itself rather than pass it on to find, we have to escape the semicolon with a backslash. That way bash gives it to the find command.

Sign up to request clarification or add additional context in comments.

9 Comments

hi thanks for your help. But apparently I have the illegal option when i use -t
@user2541163 OK. Macs don't have cp -t. See updated answer for workaround.
Hi really appreciate your help here. It works for me :). the explanation also helped me to understand better. Just 1 more question, what is the difference with cp {} and and i notice that the end has been replaced by a '\'
@user2541163 I added the explanation for the \;.
hi, just coming back to ask, is there a way to copy the files along with the parent directories they are in?
|
0

sh SCRIPT.sh copy-from-directory .extension copy-to-directory

FROM_DIR=$1
EXTENSION=$2
TO_DIR=$3

USAGE="""Usage: sh SCRIPT.sh copy-from-directory .extension copy-to-directory

- EXAMPLE: sh SCRIPT.sh PrivateFrameworks .framework .
- NOTE: 'copy-to-directory' argument is optional
"""

## print usage if less than 2 args
if [[ $# < 2 ]]; then echo "${USAGE}" && exit 1 ; fi

## set copy-to-dir default args
if [[ -z "$TO_DIR" ]] ; then TO_DIR=$PWD ; fi

## DO SOMETHING...
## find directories; find target file; 
## copy target file to copy-to-dir if file exist
find $FROM_DIR -type d | while read DIR ; do
    FILE_TO_COPY=$(echo $DIR | xargs basename | sed "s/$EXTENSION//")

    if [[ -f $DIR/$FILE_TO_COPY ]] ; then
        cp $DIR/$FILE_TO_COPY $TO_DIR
    fi
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.