Cris
Cris

Reputation: 796

Move first "n" files sorted by name with Bash Script or Ubuntu command line

I need a command line or a bash script to move the first 80 files (name sorted) in a folder (which contains 30000 files) into new folders which will store this chunk of 80 files for individual processing with Imagemagick. I have tried with ls pathtofolder/Pictures/* | head -80 | xargs -I{} cp {} pathtofolder/OutputFolder and other similar codes but the files (named by Pictures%d.jpg) are copied in weird orders (like 1 to 5, then 10 to 16, then 100 to 160, and so on, completing 80 files in total).

The easiest way I found was with the use of convert image-%d.jpg[1-5], as says this page, but It seems that it doesn't work (I tried with convert -delay 3.33 -loop 0 pathtofolder/Pictures%d.jpg[100-180] pathtofolder/Test.gif), throws this error:

zsh: no matches found: /home/naldrek/Videos/Pictures/Pictures%d.jpg[100-180]

I tried other things too, and I read a lot over the internet. Can't make it work.

Upvotes: 1

Views: 1463

Answers (1)

Hang
Hang

Reputation: 395

How about straightforward solution like

for F in $(ls -U | sort | head -80); do 
    cp $F /path/to/target
    convert /path/to/target/$F
done

Upvotes: 3

Related Questions