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 this huge folder with thousands of unordered files. Is it feasible to move the first 5000s to a subfolder via the mv command? For now I move files with

 mv *some_pattern* ./subfolder1/

As for now, I move images quite randomly, it's not really important if there aren't exactly 5000 files in each subfolder. Is there a better way to do it?

share|improve this question
    
add comment

3 Answers

With zsh:

mv -- *(D.oN[1,5000]) ./subfolder1

To move up to 5000 regular files in the order they are in the directory.

For the first 5000 in the lexicographically sorted list:

mv -- *(D.[1,5000]) ./subfolder1

If you get an error about arg list too long. You can use zsh's buitin mv command by issuing:

zmodload zsh/files

first.

POSIXly:

set --
for f in .* *; do
  [ "$#" -lt 5000 ] || break
  [ -f "$f" ] || continue
  [ -L "$f" ] && continue
  set -- "$@" "$f"
done
mv -- "$@" subfolder1/
share|improve this answer
    
The POSIX snippet is a gem –  1_CR Dec 13 '13 at 18:55
add comment
mv `ls | head -500` ./subfolder1/
share|improve this answer
5  
(assuming none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and assuming subfolder1 itself does not show up in that list.) –  Stephane Chazelas Dec 13 '13 at 17:05
add comment

You might need to do something like this:

x=1
for file in *
do
    if [ "X$x" = "X#####" ]; then
        break
    fi
    mv $file <destination>
    x=`expr $x + 1`
done

This script works in bash, ksh, sh and multiple UNIX variants.

share|improve this answer
1  
(provided none of the filenames contain space, tab, newline, star, open square bracket, question mark characters or start with - or . and provided destination itself does not show up in that list.) –  Stephane Chazelas Dec 13 '13 at 17:25
    
@StephaneChazelas True. This is not a complete solution just a method of dealing with the problem. –  Karlson Dec 13 '13 at 17:39
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.