Visit Our UNIX and Linux User Community


Script to move one folder to multiple folder...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to move one folder to multiple folder...
# 1  
Old 08-14-2013
Script to move one folder to multiple folder...

Hi All,
I have to requirement to write a shell script to move file from one folder (A) to another five folder (B,C,D,E,F) and destination folder should be blank. In not blank just skip.
This script will run as a scheduler every 2 minutes. It will check number of files in folder A and move 1 to each destination folder.
For example:
if Folder A has one file, it will move to Folder B and quit.
If folder A has 2 files, move to B and C and quit.
If folder A has 5 files, move to one in each folder. (Each destination folder shoud have unique file)
In case folder A has more than 5 files, it will move 5 and quit, and next time when this script kick-off, it will take...
and so on....
Can anyone help with this please?
# 2  
Old 08-14-2013
School work?
# 3  
Old 08-14-2013
Quote:
Originally Posted by peekuabc
Hi All,
I have to requirement to write a shell script to move file from one folder (A) to another five folder (B,C,D,E,F) and destination folder should be blank. In not blank just skip.
This script will run as a scheduler every 2 minutes. It will check number of files in folder A and move 1 to each destination folder.
For example:
if Folder A has one file, it will move to Folder B and quit.
If folder A has 2 files, move to B and C and quit.
If folder A has 5 files, move to one in each folder. (Each destination folder shoud have unique file)
In case folder A has more than 5 files, it will move 5 and quit, and next time when this script kick-off, it will take...
and so on....
Can anyone help with this please?
Let me get this straight.
  1. If you don't have any files in directory A, the script should just quit.
  2. If it has 1 file (say file1), move it to directory B and exit.
  3. If it has 2 files (say file1 and file2), move them to directory B and directory, respectively, and so forth, so
  4. if it contains 6 files (say file1, file2, file3, file4, file5 and file6) the script should move them to directory B, directory C, directory D, directory E and directory F, respectively, leaving only file6 in directory A, am I correct?
  5. Then when the script runs the next time, since directory A only contains file6, it should move it to directory B, then exit?
That being said, your forgot to mention what OS you're using and which shell. Also, it would be helpful to see what you've already tried.
# 4  
Old 08-15-2013
Hi gacanepa ,

Thanks for your mail...Your assumption is correct.
Point 4 and 5 are correct ... to make it simple, we may increase the number of destination folder in future in number of files arrives in folder A are more. and script should be smart enough for it, we can paremetrize it...This is for UNIX box and shell script....

What i have been thinking....

Count the number of files in folder A,

run through loop on count...and check if destination folder is blank, if yes, move the file...
and exit whatever come first, loop ends if number of files less than destination folder or if more try loop once again and exit....

i am not written any shell script so will share once i write something...any help will be highly appreciated....
# 5  
Old 08-15-2013
Quote:
Originally Posted by peekuabc
...This is for UNIX box and shell script...
Unix, you mean plain Unix? Good.
And as to the shell, if you're using Unix you should have bash available.
I'll get back to you later with something.
This User Gave Thanks to gacanepa For This Post:
# 6  
Old 08-15-2013
Script to do the job, did only basic testing.
Code:
#!/bin/bash

root_dir=a
dest_dir=(b c d e f)

test $( ls -lrt $root_dir/ | wc -l ) -le 1 && echo "Nothing to do..." && exit 0

i=0
for dir in ${dest_dir[@]}
do
        if [ $( ls -lrt $dir/ | wc -l) -le 1 ]; then
                good_dir[$i]=$dir
                ((i++))
        fi
done

test $i -le 0 && echo "Destination directories are not empty" && exit 1

i=0
for file in $root_dir/*
do
        test $i -ge ${#good_dir[@]} && break

        printf "Copying $file to ${good_dir[$i]}"
        mv $file ${good_dir[$i]} >/dev/null 2>&1
        test $? -ne 0 && printf " : Error in copying\n" || echo
        ((i++))
done


Scheduling can be done via crontabs.
Code:
echo "*/2 * * * * /bin/bash /path/to/your/script >/path/to/your/log 2>&1" | crontab

--ahamed

Last edited by ahamed101; 08-15-2013 at 09:45 AM..
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 08-17-2013
Thanks gacanepa . You got it right, plain unix... Please let me know in case any other information is required...

---------- Post updated at 08:21 PM ---------- Previous update was at 08:20 PM ----------

Thanks ahamad for response..i'll test and let you know....

Previous Thread | Next Thread
Test Your Knowledge in Computers #496
Difficulty: Easy
Many programming languages assign special meaning to keywords such as for, if, and while (for example) that are used to define various control structures.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Script to move files and Creation of folder structure

We are receiving few zipped files in one location say : apple/oranges/incoming All .zip files are placed here in incoming folder. So few of the files are password encrypted. There are only 10 zipped files, so we are planning to create a script which will pick that zip file from incoming... (1 Reply)
Discussion started by: Sidhant
1 Replies

3. Shell Programming and Scripting

Move files from Space Folder to other folder

I want to move a folder with spaces from one folder to another. I have two folders like this, 1).RT_032-222 -4444-01/ 2). RT_032-555 -7777-01/ I want to move files from 2 to 1 through shell script.Here I want to assign this like a user defined variable like as Source branch... (2 Replies)
Discussion started by: kannansoft1985
2 Replies

4. Shell Programming and Scripting

To move multiple files to a new folder everytime

Hi , Below is the scenario A.txt B.txt C.csv .......... i want to move all the above files in to a new path & new folder .This folder is created based on date(for ex: today's fodler name will be 20120222).for Everyday move a new folder based on date has to be created & this folder... (1 Reply)
Discussion started by: jagadeeshn04
1 Replies

5. Linux

Create folder by script & move files in it

Hi Team, I have over 1 lakh pdf files. I want to create folders like Disk-1, Disk-2 ..... & want to move 3000 pdfs per folder. Can i do it by script? Please help me. Thanks & Regards Parag Nehete (4 Replies)
Discussion started by: paragnehete
4 Replies

6. Shell Programming and Scripting

want to move set of file from one folder to another folder

Hi all, let me explain my requirments i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead each 5 folders contain 15 files i want to move 10 files to some other folder, remain 5 files should be there in the same folder. give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies

7. Shell Programming and Scripting

Script to move files with similar names to folder

I have in directory /media/AUDIO/WAVE many .mp3 files with names like: my filename_01of02.mp3 my filename_02of02.mp3 Your File_01of06.mp3 Your File_02of06.mp3 etc.... In the same directory, /media/AUDIO/WAVE, I have many folders with names like 9780743579490 9780743579491 etc.. Inside... (7 Replies)
Discussion started by: glev2005
7 Replies

8. Shell Programming and Scripting

FTPS Script to move a file to Unix Folder

Dear Experts, I need to connect to a FTPS Server and move the files from FTPS folder "/SAP/Out" to Unix directory "/SAP/In". I need to run this script on Unix directory...Script should get the files from FTPS folder and place that in specified Unix Directory. Thanks In Advance. (1 Reply)
Discussion started by: phani333
1 Replies

9. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

10. Shell Programming and Scripting

Move the file from one folder to another folder

Hi, I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755 If the permission is otherthan 755 we need to come out of the loop I will appreciate your help Thanks Soll (1 Reply)
Discussion started by: sollins
1 Replies

Featured Tech Videos