Find and move files parsed from cvs file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and move files parsed from cvs file
# 1  
Old 12-05-2011
Find and move files parsed from cvs file

I need help with a bash script.
We have a directory of files which need to be renamed and moved to another directory based on filename information in a cvs file.

The contents of the cvs file are as follows:
Code:
A102345,abc123
A102347,dfg475

Where dfg475 is the basename without extension

Our source folder is:
Code:
Old/A/ABC/abc123.ai
Old/A/ABC/abc123.pdf
Old/D/DFG/dfg475.ai
Old/D/DFG/dfg475.pdf

We need to find abc123.ai and abc123.pdf...
and move them to New directory while renaming them:
A102345_abc123.ai and A102345_abc123.pdf

The resulting destination folder should look like:
Code:
New/A102345_abc123.ai
New/A102345_abc123.pdf
New/A102347_dfg475.ai
New/A102347_dfg475.pdf

I'm stuck at the first step of trying to find the files by parsing the csv file.

Last edited by Lloyd Boyette; 12-05-2011 at 05:02 PM.. Reason: Code tags!
# 2  
Old 12-05-2011
Can you explain the logic to construct the new prefix (A102345/A102347)?
# 3  
Old 12-05-2011
The old files "abc123" are referenced in a new database solution which assigns them a new name "A102345". We need to rename the file appending the new prefix while retaining the old name so we will be able to search the file system for either the new prefix or the old name. The old name consisted of logical expression of customer "abc" and order "123". The new forced name "A102345" lacks any such intelligence.
# 4  
Old 12-05-2011
If the construction of the new name is assigned by a database solution, how can we provide you a solution if we do not know it?
# 5  
Old 12-05-2011
I think they're just obtained from the CSV file.

---------- Post updated at 03:30 PM ---------- Previous update was at 03:24 PM ----------

Try this:
Code:
while IFS="," read NEWNAME OLDNAME
do
        UPPER=$(echo "$OLDNAME" | tr '[a-z]' '[A-Z]')
        for EXT in ai pdf
        do
                if [ -f Old/${UPPER:0:1}/${UPPER:0:3}/${OLDNAME}.${EXT} ]
                then
                        echo mv Old/${UPPER:0:1}/${UPPER:0:3}/${OLDNAME}.${EXT} New/${NEWNAME}_${OLDNAME}.${EXT}
                fi
        done

        fi
done < file.csv

Remove the 'echo' once you know it does what you want.
# 6  
Old 12-05-2011
Quote:
Originally Posted by Shell_Life
If the construction of the new name is assigned by a database solution, how can we provide you a solution if we do not know it?
The database generates the cvs file which we need to parse line by line.
The prefix to the new name is contained in the first field of the cvs file.
The second field in the cvs file is the basename of the original files minus extension.

Below is my novice and vain attempts thus far:
Code:
#!/bin/bash
INPUT=~/New_Names.csv
OLDIFS=$IFS
IFS=","
[ ! -f $INPUT ] & while read new old
do
#echo $old
find ~/Desktop/Arts/ -name "$old*" >> Results.txt
#previous command dose not produce results.
#would rather pipe search results to xargs & mv instead of outputing to .txt
###
#following command is an attempt to mv the files from the search results to New directory renaming
#them appending $new field data of csv to beginning of file names.
for f in `cat Results.txt`; do mv ./"$f" "./New/${$new%f}";
done <$INPUT
IFS=$OLDIFS


Last edited by Franklin52; 01-28-2012 at 05:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 12-05-2011
Here is one possible solution:
Code:
#!/usr/bin/ksh
typeset -u mFirst
typeset -u mPref
IFS=','
while read mFld1 mFld2; do
  mFirst=$(echo ${mFld2} | cut -c1)
  mPref=$(echo ${mFld2} | cut -c1-3)
  mNewFN=${mFld1}'_'${mFld2}
  mv /Old/${mFirst}/${mPref}/${mFld2}.ai /New/${mNewFN}.ai
  mv /Old/${mFirst}/${mPref}/${mFld2}.pdf /New/${mNewFN}.pdf
done < Inp_File

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

3. Shell Programming and Scripting

Find and Move files

I have the below command to delete all .xml files older than 90 days find . -type f -name '*.xml' -mtime +90 -exec rm {} \; What will be the command to move all the .xml files older than 90 days to this folder -> "/tmp/my_bk" My OS: SunOS my-pc 5.10 Generic_150400-17 sun4v sparc... (4 Replies)
Discussion started by: mohtashims
4 Replies

4. UNIX for Dummies Questions & Answers

Find a list of files in directory, move to new, allow duplicates

Greetings. I know enough Unix to be dangerous (!) and know that there is a clever way to do the following and it will save me about a day of agony (this time) and I will use it forever after! (many days of agony saved in the future)! Basically I need to find any image files (JPGs, PSDs etc)... (5 Replies)
Discussion started by: Clyde Lovett
5 Replies

5. Shell Programming and Scripting

Please help list/find files greater 1G move to different directory

I have have 6 empty directory below. I would like write bash scipt if any files less "1000000000" bytes then move to "/export/home/mytmp/final" folder first and any files greater than "1000000000" bytes then move to final1, final2, final3, final4, final4, final5 and that depend see how many files,... (6 Replies)
Discussion started by: dotran
6 Replies

6. Shell Programming and Scripting

find command to move the files to other folder - ksh 88

Hi , I've learnt that the following command will remove the files from the given folder for given no.of days find /home/etc -type f -atime -10 -exec rm -f {} \; But how can I change the above command that will move the files to another specified directory instead of removing the... (1 Reply)
Discussion started by: smile689
1 Replies

7. Shell Programming and Scripting

Find and Move Files up One Level

Hi All, So I have another question. I'm trying to search for files with a certain extension and then move all of them up one level in the folder hierarchy. So something like this: original: /path/to/file/test.txt after: /path/to/test.txt I had some great help recently with another... (4 Replies)
Discussion started by: ideal2545
4 Replies

8. Shell Programming and Scripting

find top 100 files and move them

i have some 1000 files in my dir and i want to find top 100 files and move them to some other location: below the 2 commands i used, but it is not working ls -ltr | grep ^- | head -100 | xargs mv destination - _________>not working ls -ltr | grep ^- | head -100 | xargs mv {}... (3 Replies)
Discussion started by: ali560045
3 Replies

9. UNIX for Advanced & Expert Users

CVS: move part of repository (branch) to SVN server

Hi all, We have CVS server running for few years with absolutely no problems. Because of conditions not depending on me, I'm forced to move some part of our repository to another server (SVN). I need to move ONLY one branch from CVS server to SVN server. The branch must be moved with all... (7 Replies)
Discussion started by: +Yan
7 Replies

10. Shell Programming and Scripting

Find, Append, Move & Rename Multiple Files

Using a bash script, I need to find all files in a folder "except" the newest file. Then I need to insert the contents of one text file into all the files found. This text needs to be placed at the beginning of each file and needs a blank line between it and the current contents of the file. Then I... (5 Replies)
Discussion started by: Trapper
5 Replies
Login or Register to Ask a Question

Featured Tech Videos