Register
It is currently Wed Apr 23, 2014 1:39 pm

Script to move files to specific dir depending on filename


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Sun Sep 14, 2008 5:34 pm   

Joined: Sun Sep 14, 2008 5:20 pm
Posts: 6
Hi,

I have been searching the net for a script start to help me on my way to a script I want to make.
I have created bash scripts before (as a Mather of fact I have one I want to reuse) but now I am trying to accomplish something and am not sure how to go about it.

This is basically what I need.
I have a process that copies files from one server to another.
The file name will be stored in a variable $1 that can be passed to the next process.
Now I have a bash script that takes this file, checks if the internal network is available and has to move this file to the correct directory.
The directories are named like the user the file sis targeted to.
The problem is that the file always has the user name in it but other info also (like date etc.).
How can I make the next script use this $1 with the file name check the relevant part of the file name (the user name) and decide to which target dir the target should be set?
Files are usually named like this alex_fontana.jun2208.txt or Fontana.Alex.062208.txt
The directory would be:
Alex Fontana
If anyone could help me on my way it would be greatly appreciated!


Top
 Profile  
 PostPosted: Mon Sep 15, 2008 7:58 am   

Joined: Sun Sep 14, 2008 5:20 pm
Posts: 6
nobody? :o :'(


Top
 Profile  
 PostPosted: Mon Sep 15, 2008 8:58 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
Quote:
Files are usually named like this alex_fontana.jun2208.txt or Fontana.Alex.062208.txt
The directory would be:
Alex Fontana


Of course we cannot be expected to give you code to cover things that are not named in that "usual" manner lol

Assuming the above 2 formats are the only 2 that will be used and that their formats are...

firstname_surname.date.txt
surname.firstname.date.txt

Code:
filename='alex_fontana.jun2208.txt'
if [ $filename =~ /_/ ]
then
  firstname=$(echo $filename | awk -F. '{ print $1 }' | awk -F_ '{ print $1 }')
  surname=$(echo $filename | awk -F. '{ print $1 }' | awk -F_ '{ print $2 }')
else
  surname=$(echo $filename | awk -F. '{ print $1 }')
  firstname=$(echo $filename | awk -F. '{ print $2 }')
fi
dirname="$firstname $surname"


If you're using an older version of bash then the pattern matching in the "if" line may not work, the following does similar...

Code:
if echo $filename | grep -q _


I was going to continue but sadly we have an emergency here at $work so maybe you'll be able to extrapolate from here :)


Top
 Profile  
 PostPosted: Mon Sep 15, 2008 12:38 pm   

Joined: Sun Sep 14, 2008 5:20 pm
Posts: 6
Hi Thanx!

I have thought of this one myself but I was hoping there would be a more dynamic way.
Now I will have to use alot of if loops because it is not only alex fontana but also "pierce brosner" "alain delon" "marcelo mastroiani" "jean paul belmondo" and so on.

I am now using this to select the files and copy them but it only selects the files for the relevant users and copies them to a general directory instead of doing the correct subdirectoy targeting.

Code:
Line[1]="alex fontana"
Line[2]="sidney pointier"
Line[3]="silvester stalone"
Line[4]="al pacino"



for index in 1 2 3 4 # 4 lines
       
        do
            awker=`printf "${Line[index]}" | awk '{print $1}'`
             movedfile=`find ${downdir} -iname "*${awker}*"`
             find ${downdir} -iname "*${awker}*"  -exec mv {} ${updir}"${Line[index]}"/ \;
                       
             if [ -n "$movedfile" ]
                     then
                      echo "**" `date` "**" "File:" $movedfile "moved to target dir" ${updir}"${Line[index]}"/ >>${log}
                      printf "\n" >>${log}
                      else
                      echo "**" `date` "**" "Nothing here to copy for" "${Line[index]}" ", going to loop." >>${log}     
                      printf "\n" >>${log}
             fi               
         done



Top
 Profile  
 PostPosted: Tue Sep 16, 2008 3:07 am   

Joined: Sun Sep 14, 2008 5:20 pm
Posts: 6
Nobody has ideas for different approaches?

All tips are appreciated!


Top
 Profile  
 PostPosted: Tue Sep 16, 2008 8:35 am   

Joined: Sun Sep 14, 2008 5:20 pm
Posts: 6
DarthWavy wrote:
Quote:
Files are usually named like this alex_fontana.jun2208.txt or Fontana.Alex.062208.txt
The directory would be:
Alex Fontana


Of course we cannot be expected to give you code to cover things that are not named in that "usual" manner lol

Assuming the above 2 formats are the only 2 that will be used and that their formats are...

firstname_surname.date.txt
surname.firstname.date.txt

Code:
filename='alex_fontana.jun2208.txt'
if [ $filename =~ /_/ ]
then
  firstname=$(echo $filename | awk -F. '{ print $1 }' | awk -F_ '{ print $1 }')
  surname=$(echo $filename | awk -F. '{ print $1 }' | awk -F_ '{ print $2 }')
else
  surname=$(echo $filename | awk -F. '{ print $1 }')
  firstname=$(echo $filename | awk -F. '{ print $2 }')
fi
dirname="$firstname $surname"


If you're using an older version of bash then the pattern matching in the "if" line may not work, the following does similar...

Code:
if echo $filename | grep -q _


I was going to continue but sadly we have an emergency here at $work so maybe you'll be able to extrapolate from here :)




Hi,

I get this error:

bash: [: =~: binary operator expected

Not sure why.

Aparently it expects a binary operator but I do not think it is being used.
I have googled for this error and it seems to be a infamous bash error.

Any ideas?


Top
 Profile  
 PostPosted: Wed Sep 17, 2008 11:26 pm   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
Quote:
I get this error:

bash: [: =~: binary operator expected

Not sure why.

Aparently it expects a binary operator but I do not think it is being used.
I have googled for this error and it seems to be a infamous bash error.


This is almost certainly because you're using an older version of bash, i believe the regex match feature was introduced in bash v3

instead you replace the "if" line in the script with the alternative i provided...

Code:
if echo $filename | grep -q _


Top
 Profile  
 PostPosted: Thu Sep 18, 2008 5:52 pm   

Joined: Sun Sep 14, 2008 5:20 pm
Posts: 6
DarthWavy wrote:
Quote:
I get this error:

bash: [: =~: binary operator expected

Not sure why.

Aparently it expects a binary operator but I do not think it is being used.
I have googled for this error and it seems to be a infamous bash error.


This is almost certainly because you're using an older version of bash, i believe the regex match feature was introduced in bash v3

instead you replace the "if" line in the script with the alternative i provided...

Code:
if echo $filename | grep -q _


Hi,

Thanx, I had done that and it works like a charm.
Thanx!

When I have the script cleaned up I will post it here for review and tips.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP