Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


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

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.

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

for server in "${SERVERS}" do
   eval "scp user@$server:/mylogs/* /targetlogs/$server-<HERE IS WHERE YOUR HELP IS NEEDED>"
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

Old 08-23-2011, 04:03 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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:
for server in "${SERVERS}"
  while read file
    scp -p user@$server:/mylogs/\'"$file"\' ./$server-"$file"
  done < <(ssh user@$server "ls /mylogs")
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
Registered: Dec 2005
Distribution: Linux Mint 14 (Cinnamon), Xubuntu 12.04, Ubuntu 10.04
Posts: 49

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

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

Thanks for the help!

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


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

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 ( afx2029 Linux - Software 4 08-17-2007 06:07 AM
Filename string manipulation jango77 Linux - Newbie 2 03-27-2005 06:58 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:56 AM.

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