Move multiple files 4rm Source to different target folders based on a series num in the file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move multiple files 4rm Source to different target folders based on a series num in the file content
# 8  
Old 11-25-2016
Upper case, lower case, or mixed case file names? However, try
Code:
for FN in *.dat; do read X SERIES X <"$FN"; echo mv "$FN" "Target_$SERIES/$FN"; done
mv Claim25112016222930.dat Target_789101/Claim25112016222930.dat
mv Debit25112016222930.dat Target_789101/Debit25112016222930.dat
mv Refund25112016222930.dat Target_789101/Refund25112016222930.dat

Remove the echo command if happy with the result.
This User Gave Thanks to RudiC For This Post:
# 9  
Old 11-29-2016
Hi RudiC

I am new to unix and not sure how to execute this. My requirement is to
  1. Script should check for .dat files in Source folder.
  2. Check for specific text (645559 or 508659) in first line of each file.
  3. If 645559 exists, move the files to folder SUN_ID_645559
  4. If 508659 exists, move the files to folder SUN_ID_508659

I am able to achieve this using java for windows platform. In jave we can hard code source and target directories. Write if conditons to search for the number in the first line of a file and if exists we can call a method to move this file to its corresponding folder.

I am trying to achive the same using unix as the requirement is to move the files in Unix and prefered solution is to use unix script. Not sure how to set variables for source and target folders? If condition for check for specific text in the first line and move the file to specific folders accordingly.

Code:
  package com.test;
  
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.FilenameFilter;
 import java.io.IOException;
  
 public class MoveSunIDFiles {
  
     /**
      * @param args
      * @throws IOException
      */
     public static void main(String[] args)  {
         // TODO Auto-generated method stub
  
         File src = new File("C:\\Users\\akellap\\RND\\Test\\Source");
  
         // create new filename filter for .dat files
  
         File[] result = src.listFiles(new FilenameFilter() {
             public boolean accept(File dir, String name) {
                 if (name.lastIndexOf('.') > 0) {
                     // get last index for '.' char
                     int lastIndex = name.lastIndexOf('.');
  
                     // get extension
                     String str = name.substring(lastIndex);
  
                     // match path name extension
                     if (str.equals(".dat")) {
                         return true;
                     }
                 }
                 return false;
             }
         });
  
         if (result != null && result.length > 0) {
             for (int i = 0; i < result.length; i++) {
                 BufferedReader in = null;
                 try {
                     in = new BufferedReader(
                             new FileReader(result[i]));
                 } catch (FileNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
  
                 String line = "";
  
                 try {
                     while ((line = in.readLine()) != null) {
                         if (line.contains("645559")) {
                             move645559(result[i]);
                             break;
                         }
  
                         if (line.contains("508659")) {
                             move508659(result[i]);
                             break;
                         }
                     }
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
  
             }
         } else if (result == null) {
             System.out.println("Invalid filename or folder");
         } else {
             System.out.println("No files matching the criteria");
         }
  
     }
  
     private static void move508659(File result) {
         // TODO Auto-generated method stub
  
         if (result.renameTo(new File(
                 "C:\\Users\\akellap\\RND\\Test\\Target\\SUN_ID_508659\\"
                         + result.getName()))) {
             System.out.println("File:" + result + " moved successfully");
         } else {
             System.out.println("File " + result.getName() + " not moved");
         }
     }
  
     private static void move645559(File result) {
         // TODO Auto-generated method stub
         if (result.renameTo(new File(
                 "C:\\Users\\akellap\\RND\\Test\\Target\\SUN_ID_645559\\"
                         + result.getName()))) {
             System.out.println("File:" + result + " moved successfully");
         } else {
             System.out.println("File " + result.getName() + " not moved");
         }
     }
 }


Last edited by rbatte1; 11-29-2016 at 05:40 AM.. Reason: Converted to formatted number-list
# 10  
Old 11-29-2016
Sorry?
Where and how does my proposal not fulfill your requirement in post#1?
# 11  
Old 11-29-2016
Hi

I have copied below lines of code into a notepad and saved as Script.sh. Placed the file in /Source folder.

Code:
for FN in *.dat; do read X SERIES X <"$FN"; echo mv "$FN" "SUN_ID_$SERIES/$FN"; done
mv Claim25112016222930.dat SUN_ID_789101/Claim25112016222930.dat
mv Debit25112016222930.dat SUN_ID_789101/Debit25112016222930.dat
mv Refund25112016222930.dat SUN_ID_789101/Refund25112016222930.dat

Placed around 100 .dat files in Source folder. Executed the script. I got below error.
Code:
-bash: Script.sh: command not found

Does below code means this script will handle only these 3 files?

Code:
mv Claim25112016222930.dat SUN_ID_789101/Claim25112016222930.dat
mv Debit25112016222930.dat SUN_ID_789101/Debit25112016222930.dat
mv Refund25112016222930.dat SUN_ID_789101/Refund25112016222930.dat

# 12  
Old 11-29-2016
Make sure the script does not contain any DOS <CR> (^M, 0x0D, \r) line terminators.

And, the mv ... lines were the result of the one line script - remove them.
# 13  
Old 11-29-2016
Hi RudiC

Thank You. Does this mean below code in unix

Code:
for FN in *.dat; do read X SERIES X <"$FN";
mv "$FN" "SUN_ID_$SERIES/$FN";
done

Will check for .dat files in Source folder.
Check for specific text (645559 or 508659) in first line of each file.
If 645559 exists, move the files to folder SUN_ID_645559
If 508659 exists, move the files to folder SUN_ID_508659
# 14  
Old 11-29-2016
Yes - if "specific text (645559 or 508659)" is in the second white space separated field of the first line of the respective files.

That's why I added the echo - it shows what would happen without doing any possible harm.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i move folders and its content if folder is older than 1,5 days and keep subdirs in bash?

Hello all, do you know any way i can i move folders and its content if folder is older than 1,5 days in bash? I tried: find /home/xyz/DATA/* -type d -ctime +1.5 -exec mv "{}" /home/xyz/move_data_here/ \;All i got was that Files from DATA /home/xyz/DATA/* ended messed up in... (1 Reply)
Discussion started by: ZerO13
1 Replies

2. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Script to move files in multiple folders

Hello all, I would appreciate any help to write a script. I have folder A which contains over 30 thousands xml files, I would like create multiple folders and move those files (500 in each folders). Thank you (1 Reply)
Discussion started by: mmsiddig
1 Replies

4. Solaris

Move files into different folders based on its month

Hi All, I want to move the files in to different folders based on the files month in the file timestamp. For example All the september files in the directory should moves into the folder "sep_bkp_files" , August files in to aug_bkp_files folder... Please help me to achive the above... (10 Replies)
Discussion started by: velava
10 Replies

5. Shell Programming and Scripting

move set of files to the target path with different extension

I have the following files in the dir /home/krishna/datatemp abc.xml cde.xml asfd.txt asdf_20120101-1.xml asdf_20120101-2.xml asdf_20120101-3.xml asdf_20120101-4.xml Now I need to move the files having the pattern asdf_20120101-*.xml to the dir /home/krishna/dataout with the extn as... (1 Reply)
Discussion started by: kmanivan82
1 Replies

6. Shell Programming and Scripting

Archive files to different target folders based on criteria

Hi All, I am creting archive script in which i need to split the source file's to different target folder's based on the input file name first character. Input1.txt -- will contains file names that are needs to be Archive. Input1.txt A1213355 B2255666 C2254555 A6655444 C5566445 ... (2 Replies)
Discussion started by: kmsekhar
2 Replies

7. Shell Programming and Scripting

Move all files from source to destination directory based on the filename

Move all files starting with a specific name to different directory. This shell script program should have three parameters File Name Source Directory Destination Directory User should be able to enter ‘AB_CD*' in file name parameter. In this case all the files starting with AB_CD will... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

8. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

9. UNIX for Dummies Questions & Answers

how to move files into different folders based on filename

I need to move a bunch of files into folders that have the same name. I wanted to either do this with some filter command or some type of batch file that I could save that would already include all of the mv commands since I will have to do this process often. Whatever method you think is easier. ... (7 Replies)
Discussion started by: italia5
7 Replies
Login or Register to Ask a Question