The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
runnning a shell script in multiple folders melearlin Shell Programming and Scripting 2 05-19-2009 03:36 AM
Shell script to move certain files on scheduled time abhishek27 Shell Programming and Scripting 7 05-07-2009 01:09 AM
Move folders containing certain files xavix UNIX for Dummies Questions & Answers 3 02-21-2009 06:43 AM
how to move files into different folders based on filename italia5 UNIX for Dummies Questions & Answers 7 08-23-2006 11:04 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-14-2009
sighK sighK is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 13
while [ "$COUNTER" != "0" ]


it never reaches 0, so it has nothing left to do but a continuous loop of nothing, you need to break at somepoint

It should be in a for loop instead of while.

change

COUNTER=`ls -1 | wc -l`
while [ "$COUNTER" != "0" ]
to

COUNTER=`ls -1 | wc -l`
for (( $i=0; $i < "$COUNTER" ; i++ )); do
  #2 (permalink)  
Old 07-14-2009
wretchedmike wretchedmike is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 5
You're right, thanx.
However, this for loop is still missing the fact that variable COUNTER needs to decrease by 3 in every loop, or maybe add 3 to $i, What do you think?
  #3 (permalink)  
Old 07-14-2009
sighK sighK is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 13
try

for (( i=$COUNTER; $i > 0; )); do
echo $i
let i-=1
done

I hope this helps
  #4 (permalink)  
Old 07-15-2009
wretchedmike wretchedmike is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 5
I solved it!! Thanx for all sighK. This is the code for my script. It is used to move files (in my particular case were tiff images) from a folder to 3 different folders distributing the files among them. For example, if source folder has 100 files, the 3 target folders have to end up with 34, 33 and 33 files respectively. Hope this could be helpful to someone.


Code:
#!/bin/bash

COUNTER=`ls -1 *.TIF | wc -l`

while [ $COUNTER -gt 0 ]
do
        ARRAY=( $(ls *.TIF | head -n 3) )
        mv ${ARRAY[0]} folder1/
        mv ${ARRAY[1]} folder2/
        mv ${ARRAY[2]} folder3/
        COUNTER=`expr $COUNTER - 3`

done

You can change file extension or just delete it, folder paths and of course, the number of folders, just remember to change 'head -n' where n is the number of files to list first that will end up inside each folder. You also have to change the number in `expr $COUNTER - 3` to match the number of folders.
  #5 (permalink)  
Old 07-15-2009
tkleczek tkleczek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 34
The following is a bit shorter and allows an arbitrary number of folders

Code:
#!/bin/bash
i=1
for f in *.TIF; do
	mv "$f" "folder$i/"
	((i=i%3+1))
done

Reply

Bookmarks

Tags
file, folder, move, script, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0