Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have about 9000 files in a directory and I want to mv them into 90 files in 100 directories by filename order, ignoring any remainder. In Copy multiple files from filename X to filename Y?, Caleb wrote in a comment:

If the object is just to batch them, there is a MUCH easier way! ls | xargs -iX -n20 cp X target_folder/ would run cp on files in batches of 20 until they were all done.

So based off of using xargs, how could I switch the target_folder to create a new folder and loop the command 100 times?

share|improve this question
2  
U&L is a wiki, not a message board: please rewrite your question as a question, removing the reference to a community member and including a link to the post your reference. See the FAQ for help on asking questions –  jasonwryan Jun 7 '12 at 22:46
add comment

2 Answers

That command doesn't look close to what you're trying to do. xargs can help, but it's cumbersome to use unless you know your file names do not contain any whitespace or quoting character. Here's a shell loop that dispatches files into newly-created subdirectories, 90 per directory.

set -- *
# Set args to "$1" "$2" ... "$90"
i=1 args=
while [ $i -le 90 ]; do
  slice="$slice \"\${$i}\""
  i=$((i+1))
done
# Move files 90 at a time
i=0
while [ $# -ge 90 ]; do
  mkdir part$i
  eval "mv $slice part$i"
  shift 90
done
# 0 to 89 files remain in the current directory
share|improve this answer
    
xargs also has an option -0 which turns off the whitespace and quote processing. But I agree this kind of script is the best approach. –  James Youngman Jul 7 '12 at 17:12
    
@Gilles There's an error in the script: $slice should be $args –  daniel kullmann Aug 7 '12 at 7:26
add comment

In bash, try the following code :

#!/bin/bash

c=0

for f; do
    if ! ((c % 100)); then
        folder=folder_$(printf "%03d\n" $c)
        mkdir -p $folder
    fi

    [[ -d "$f" ]] || mv "$f" "$folder"
    ((c++))
done

Run the script like that :

./script.bash *
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.