move first 1000 files

Hi i have a folder with 27,000 images. I need to process each image using imagemagick. I have got image magick working. I don't want to attempt to process all 27,000 images in one go and thought i would try doing it with 1000 images at a time. Is there some way i can list only the first 1000 entries and move them to a new folder for processing

cd /folder
for i in `ls -1`
do
    mv $i  ../temp/$i
done

That sort of thing but only do 1000

Hi.

Try:

ls | head -1000 | xargs -I{} mv {} ../temp

thanks ill give that a try

Hello ,

I have used the above command to write a script that can move the first thousand files.
However , the script is eating all the files including iteslf in the directory from where it is run.
Pls correct the below script. Thanks in advance.

#!/bin/bash
PATH=$PATH:/usr/bin:/usr/sbin
# Variables
LOGFILE=/tmp/r_bkp_log.out
U_PATH=/source/In7_backlog
# MAIN
while true
do
  DATE=$( date +"%a %d-%m-%Y %H:%M:%S" )
  UR=$( ls /source/In7_bkp| wc -l )
  if [ $UR -eq 0 ]
  then
    /usr/bin/cd $U_PATH
    /usr/bin/echo /usr/bin/pwd
    ls | head -1000 | xargs -I{} mv {} ../In7_bkp
    echo "Moving next 1000 files " >> $LOGFILE
  fi
  /usr/bin/echo "" >> $LOGFILE
  /usr/bin/echo "$DATE: loader:\t$UR" >> $LOGFILE
  sleep 60
done

Hi.

It's hard to say what exactly your script is doing.

It's representing paths using an odd mix of variables, hard-coded absolute paths and hard-coded relative paths - all seemingly to the same location.

If the directory you are running the script from contains the files you are moving, then either qualify what files you are moving to exclude the script, using ls, or using grep -v to exclude it.

Actually , I am trying run this script from a /source/tools where I have lot of other scripts available.

Now the above said script needs to check one dir called /source/In7_bkp and see if the count there is zero.

Incase it is then it needs to move files from /source/In7_backlog to the above said directory location.

There is a process that handles the files from In7_bkp. So I am trying check the count every 1 min and see if the process had finished the 1000 files that we put.
Incase it does then , next 1000 files are moved.

However, this script has eaten all the files in tools dir as well.
I dunno if I implemented the logic properly for my requirement.

Hi.

I made a couple of changes to your script, and hope it helps :slight_smile:

#!/bin/bash
# Variables

LOGFILE=/tmp/r_bkp_log.out
M_PATH=/source               # Main directory
S_PATH=$M_PATH/In7_backlog   # Source directory
T_PATH=$M_PATH/In7_bkp       # Target directory

cd $S_PATH

# MAIN
while true
do
  DATE=$( date +"%a %d-%m-%Y %H:%M:%S" )
  UR=$( ls $T_PATH | wc -l )
  if [ $UR -eq 0 ]
  then
    echo $PWD   # I didn't know what "/usr/bin/echo /usr/bin/pwd" was meant to be doing!
    ls | head -1000 | xargs -I{} mv {} $T_PATH
    echo "Moving next 1000 files " >> $LOGFILE
  fi
  echo >> $LOGFILE
  echo "$DATE: loader:\t$UR" >> $LOGFILE
  sleep 60
done
1 Like

Thank you very much!:slight_smile:

Its now working well. I guess I messed up with the paths , thanks again!!