Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to take the first 3 files from a directory starting with letter b in alphabetical order to another directory. I've come up with this command:

find /users/students/ejackson/A3-ejackson-55688-114906/CS282in \
  -name "b*" | sort | head -3

And it produces the proper files, but I can't do anything with them. Ideally I'd just pipe the output of the above command to mv, but I don't think that's possible. I've been messing around with this for hours, but I've gotten nowhere. Any help is much appreciated.

P.S. I have to do this without grep, sed or awk.

share|improve this question
@qbi I actually did try that with mv, and just now with cp, but both times I got the message "Illegal variable name." Could the problem be that I'm working in tcsh and not bash? – Evan Feb 23 '13 at 22:40
Yes, tcsh is in some sense completely different than bash. – qbi Feb 23 '13 at 22:45
@qbi Okay, so I've tried set file = 'find /users/students/ejackson/A3-ejackson-55688-114906/CS282in \ -name "b*" | sort | head -3', then echo $files, and I get the message find: No match. What now? Thanks for your help btw. – Evan Feb 23 '13 at 22:48
I changed my answer. So it fits to tcsh. – qbi Feb 23 '13 at 22:54
@qbi Tried your new answer and it gives the message "cp: No match." Hmm... – Evan Feb 23 '13 at 23:03

You can take a list of file and process them through a for loop in a shell script, or if you on the command line itself (it will just be harder to reader)

Check out this article at nixCraft. Another tutorial as well.

You should be able to do something like

for f in `find /users/students/ejackson/A3-ejackson-55688-114906/CS282in -name "b*" | sort | head -3`
    cp $f [target]
share|improve this answer
The same logic can be applied in other shells or perl, etc. – Eric G Feb 25 '13 at 3:51

You mentioned that you are using tcsh. The family of C-Shells is not compatible with Bourne shells. This command might work:

set THREE_FILES=`find …`

The first set command makes a new enviroment variable called THREE_FILES and saves the output into that variable. The cp takes the three files and copies them to the NEW_DIRECTORY.

share|improve this answer

Your Answer


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.