Rename file while scp'ing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file while scp'ing
# 1  
Old 03-15-2013
Rename file while scp'ing

Hi,
I am scp'ing set of files listed in file list with the following code:

Code:
 
    for i in `cat ${SCPFILELISTDIR}/${SCPFILELIST}`; do
        scp ${LOCALDIR}/$i ${REMOTESRVR}:${REMOTEDIR}
        if [[ $? == 0 ]]; then 
            echoen "${LOCALDIR} transferred to ${REMOTESRVR}:${REMOTEDIR} Successfully!!"
        elif [[ $? != 0 ]]; then
            echoen "Transfer failed for file ${LOCALDIR} to ${REMOTESRVR}:${REMOTEDIR} "
            exit -10
        fi
    done

If the file name that is being scp'ed is say like
Code:
 
"abc_xsy_201302_likl.txt"

When ever a "year & month" comes, I would like remove it from the file name and the destination file should look like follows:
Code:
 
"abc_xsy_likl.txt"

Appreciate help to do this rename while scp'ing.
# 2  
Old 03-15-2013
That is a useless use of cat and dangerous use of backticks, you should rewrite your loop as

Code:
while read i
do
...
done < inputfile

As for renaming the file, you can do this:

Code:
scp "${LOCALDIR}/$i" "${REMOTESRVR}:${REMOTEDIR}/${i/_[0-9][0-9][0-9][0-9][0-9][0-9]_/_}"

Also, you almost never need to use $? in an if-statement, you can plug in the scp directly:

Code:
if scp ...
then
        echo "success"
else
        echo "failure"
fi

# 3  
Old 03-15-2013
Is the destination local or remote? Is the source local or remote?
Find files, sed into scp lines, pipe to shell.

For file renaming, sftp might be easier, unusually enough (one session).

However, if you can link the files to the right names in a clone tree locally, they you can scp the subtree.
# 4  
Old 03-15-2013
Code:
scp "${LOCALDIR}/$i" "${REMOTESRVR}:${REMOTEDIR}/${i/_[0-9][0-9][0-9][0-9][0-9][0-9]_/_}"


The above is not working.

It is giving following error:
Code:
 
 
scpscript.ksh[2]: "${REMOTESRVR}:${REMOTEDIR}/${i/_[0-9][0-9][0-9][0-9][0-9][0-9]_/_}": bad substitution

# 5  
Old 03-15-2013
You must be using an ancient ksh88, or perhaps pdksh. Does your system have a more modern ksh or bash available?
# 6  
Old 03-15-2013
Quote:
Originally Posted by Corona688
You must be using an ancient ksh88, or perhaps pdksh. Does your system have a more modern ksh or bash available?

We might not have the latest ksh. Is there a way I could do with awk or sed.

Apprecaite help.
# 7  
Old 03-15-2013
As a quick fix it will do, but you really should get a better shell, since this is a quite inefficient practice.


Code:
NEWNAME="`echo $OLDNAME | sed 's/_[0-9][0-9][0-9][0-9][0-9][0-9]_/_/'`"

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Permission error when "touch"ing file with different user

Hi, There are 2 users (T886072 & T864764) that need to be provided full (rwx) access to a directory. I made the changes to the directory permissions using chmod and setfacl : root@digidb2:# chmod 700 /u02/ftpfiles/MFRS16/discount_rates/ root@digidb2:# setfacl -s... (3 Replies)
Discussion started by: anaigini45
3 Replies

2. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

3. Shell Programming and Scripting

Grep'ing information from a log file on SUN OS 5

Hi Guys, I'm trying to write an script that will be launched by a user. The script will look at a log file and check for alerts with the date (supplied by user) and a machine's hostname (also supplied by the user). I'm trying to get the output formatted just like the log file. The logfile looks... (5 Replies)
Discussion started by: illgetit
5 Replies

4. Shell Programming and Scripting

grep'ing dot history file

Hi, I tried to grep ".sh_history" (DOTsh_history) file and did not return anything though I found the word in .sh _history file through vi editor in Linux. Then I tried to grep ".profile" to check if it is the prob with hidden files and I got results. Then I verified the same with my friend... (4 Replies)
Discussion started by: bobbygsk
4 Replies

5. Shell Programming and Scripting

FTP'ing the zipped file

Hi, I need to have a shell script that FTP's a zipped file from a particular location. I have some path and inside that path i will have folders like x_timestamp and inside x_timestamp there may many folders based upon events like y_111,y_222,y_333.Inside each event there will be another... (3 Replies)
Discussion started by: weknowd
3 Replies

6. Shell Programming and Scripting

grep'ing a file until a certain message appears

Hello, I'm writing a script that will automate the launch of some services on my AIX machine. However, some services are dependent on the successful startup of others. When I start these services manually, I usually just check a log file until I see a message that confirms a successful... (3 Replies)
Discussion started by: pallak7
3 Replies

7. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

8. Shell Programming and Scripting

size of a file that iam ftp ing

Hi, I want to compare the size of the files in my local machine before ftp ing it and the size of the file in the remote machine i.e after ftping . SO that the ftp is successful . (3 Replies)
Discussion started by: dineshr85
3 Replies

9. UNIX for Advanced & Expert Users

Dynamically ftp'ing a file

Hi, I am having unix script that passes argument value to script. The script finds the file and keeps it in a directory. I need to ftp this file to another server. Please guide me how to acieve this. I am able to connect to ftp server but i am not able to use the unix argument in the ftp... (0 Replies)
Discussion started by: pyaranoid
0 Replies

10. Programming

Ideas: 'select'ing file descriptors

Hey everyone, I'm writing a threaded database server application and need to communicate the results of several worker threads to a single multiplexed communication output thread. Basically, the communication output thread sits in a select statement waiting for its client sockets to dole out... (2 Replies)
Discussion started by: DreamWarrior
2 Replies
Login or Register to Ask a Question