3

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.

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

11
  • @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, 2013 at 22:40
  • Yes, tcsh is in some sense completely different than bash.
    – qbi
    Feb 23, 2013 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, 2013 at 22:48
  • I changed my answer. So it fits to tcsh.
    – qbi
    Feb 23, 2013 at 22:54
  • @qbi Tried your new answer and it gives the message "cp: No match." Hmm...
    – Evan
    Feb 23, 2013 at 23:03

3 Answers 3

1

Instead of parsing find output (very bad idea), you can take advantage of the fact that for loops over files in an ordered way:

i=0; for b in /path/to/files/b*; do (( ++i < 4 )) && echo mv -v -- "$b" /path/to/destination; done

Remove echo after testing to actually move the files.
Here it is with ugly comments:

# set a variable to 0 so we can increment it
i=0
# glob for the files starting with b
for b in /path/to/files/b*; do 
   # test how many times the loop has been run and if it's less than 4...
   (( ++i < 4 )) && 
     # ... then move the files*  
     echo mv -v -- "$b" /path/to/destination
done

*This won't happen until you remove echo from the start of the line - instead it will echo out which files will be moved and where (expand the variables for each iteration)

I don't know if this works in tcsh, but it works in bash, so might help other Ubuntu users who mainly use bash

0

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`
do
    cp $f [target]
done
2
  • The same logic can be applied in other shells or perl, etc.
    – Eric G
    Feb 25, 2013 at 3:51
  • parsing the output of find is not a good idea though
    – Zanna
    Apr 9, 2017 at 20:36
0

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 …`
cp $THREE_FILES NEW_DIRECTORY

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .