moving the first N files from a folder script Bash

sometimes you find yourself in the position were you want to gradually move files from one location to another (to be processed for instance) you don’t to move the files all at once and you don’t have a certain criteria to limit your find command with. I’d have expected find to have an argument that’d would control the number of files returned but it doesn’t instead you have to use a counter and for loop to force the script to exit after moving certain number of files.

I’ve written a rather simple script to do that and here it is, I’m sure there is a more elegant way to do this however I wrote this as a quick and dirty fix.

 

i=0;
for file in `ls -t`
do
i=$(($i+1));
mv $file tempdir;

if [ “$i” -eq  6 ]
then
exit 0;
fi

done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s