LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 08-23-2011, 01:52 AM   #1
karnaf
Member
 
Registered: Dec 2005
Distribution: Linux Mint 14 (Cinnamon), Xubuntu 12.04, Ubuntu 10.04
Posts: 47

Rep: Reputation: 15
scp and filename manipulation


Hi all,

I'm writing a bash script which collects files from several servers into a single folder (e.g. logs). In order to avoid files with the same name but from different servers overriding each other, I want to prepend the server's name before them, e.g.

Code:
scp user@server1:/mylogs/filename.log /targetlogs/server1-filename.log
however, my source filename isn't known in advance, and there may be more than one. i.e. I'm using

Code:
for server in "${SERVERS}" do
   eval "scp user@$server:/mylogs/* /targetlogs/$server-<HERE IS WHERE YOUR HELP IS NEEDED>"
done
How can I get the source filename to the target location?

Currently, I'm thinking using a workaround (copy to a tmp folder, rename it there locally, and then move it to its final destination

Thanks!
karnaf
 
Old 08-23-2011, 04:03 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
The scp command does not offer any option to add a prefix to the filename, hence you need some scripting. A simple loop fed by the result of the ls command on the remote machine should be enough:
Code:
for server in "${SERVERS}"
do
  while read file
  do
    scp -p user@$server:/mylogs/\'"$file"\' ./$server-"$file"
  done < <(ssh user@$server "ls /mylogs")
done
Note the escaped single quotes in the scp command: they are useful if the file name contains blank spaces: they don't prevent the substitution of $file in the local server because they are treated literally and not a a special character. On the other hand they prevent the word splitting onto the remote server. Hope this helps.

Last edited by colucix; 08-23-2011 at 04:05 AM.
 
Old 08-23-2011, 05:41 AM   #3
karnaf
Member
 
Registered: Dec 2005
Distribution: Linux Mint 14 (Cinnamon), Xubuntu 12.04, Ubuntu 10.04
Posts: 47

Original Poster
Rep: Reputation: 15
Funny, I ended up using a similar solution

Code:
for server in "${SERVERS[@]}" 
do
        files=`ssh $SOURCE_USER@$server "ls $SOURCE_PATH/*.$SOURCE_SUFFIX" 2>&1`
        
        for file in $files
        do
                cmd="scp $SOURCE_USER@$server:$SOURCE_PATH/$file $TARGET_PATH/$server-$file"
                eval "$cmd"
        done
done
cmd is kept in a variable for debugging purposes

Thanks for the help!
karnaf

Last edited by karnaf; 08-23-2011 at 05:46 AM.
 
  


Reply

Tags
bash scripting, scp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SCP a file to diff directory/change filename using PHP or call sh script fm PHP nuebelhor Red Hat 1 11-05-2009 08:03 AM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 09:29 PM
How do I scp a file with spaces in the filename? glenn69 Linux - General 14 11-01-2007 07:18 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM
Filename string manipulation jango77 Linux - Newbie 2 03-27-2005 06:58 PM


All times are GMT -5. The time now is 08:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration