Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a bunch of huge files (4GB+ each) with the same extension in a directory structure in a network drive, but am only interested in the first few bytes of each, so I'd like to copy them all to my local drive, with the same filenames and maintaining the same folder structure.

I've tried:

$ find . -type d -exec mkdir -p ~/Desktop/heads/{} \;
$ find . -type f -name "*.ext" -exec head -c 128 {} > ~/Desktop/heads/{} \;

But it doesn't work, since they are all put in a file called '{}', probably because the '>' operator is not being interpreted as part of the -exec argument, and if I escape it with '\>', then it is escaped down to the head argument, which doesn't work either.

share|improve this question
    
Given OSX 10.7 isn't publicly released just yet - you might consider just saying you're on OSX in the question line, so that people not on 10.7 don't ignore the question on account of never having used it before. –  Doc Jul 11 '11 at 20:08
    
It's tagged Snow Leopard, but that's version 10.6. Do you mean yet-to-be-released Lion 10.7, or Snow Leopard 10.6? –  Noel M Jul 11 '11 at 20:09
    
I meant 10.6.7, I changed it to snow leopard for simplicity. Thanks. –  user2986 Jul 11 '11 at 20:13
    
I just removed the version altogether, it doesn't really matter that much. –  slhck Jul 11 '11 at 20:16
add comment

2 Answers 2

up vote 1 down vote accepted

You've correctly observed that the problem is the > redirect. Two solutions:

Either use something like this:

find . -type f -name "*.ext" -exec sh -c "head -c 128 '{}' > ~/Desktop/heads/'{}'" \;

Or you can also pass the '{}' as an argument to the subshell like so:

find . -type f -name "*.ext" -exec sh -c 'head -c 128 $1 > ~/Desktop/heads/$1' -- {} \;

The latter works because -- {} passes the filename as an argument to sh -c, which can be accessed by $1. Note that you'd now have to use single quotes ' instead of double quotes.


Update: I've actually found a Stack Overflow question that covers the underlying problem of yours, namely using > within xargs or similar commands:

How to use > in an xargs command?

I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. [...]

share|improve this answer
    
Damn! I was barely too late. –  bahamat Jul 11 '11 at 20:45
add comment

Your redirect is breaking it. Change to this:

$ find . -type d -exec mkdir -p ~/Desktop/heads/{}
$ find . -type f -name "*.ext" -exec sh -c "head -c 128 {} > ~/Desktop/heads/{}" \;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.