Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP: rename more than one file at once!! 1

Status
Not open for further replies.

Yanndewael

Programmer
Mar 14, 2003
12
GB
Hello,

I am a beginner in UNIX programmation and I want to do the following:
- I connect from the UNIX server to an FTP server
- I take all the .dat files from a particular directory
- and here is my problem: I want to rename the files I took into .dat.done for example. But it seems that I cannot rename all the files at once (if I understood well, the rename command need the exact filename to rename). Since I don't know the name of the files (these are ref number), I cdon't know how to do. Is it possible to perform a loop in an FTP session??

Thank you for your help

Y
 
I'd suggest looking into ftp's "nmap" directive - do 'man ftp' and search for 'nmap'.

Here's the snippet from the 'man ftp':

nmap [ inpattern outpattern ]
Set or unset the filename mapping mechanism. If no
arguments are specified, the filename mapping
mechanism is unset. If arguments are specified,
remote filenames are mapped during mput commands
and put commands issued without a specified
remote target filename. If arguments are speci-
fied, local filenames are mapped during mget com-
mands and get commands issued without a specified
local target filename.

For example, given inpattern $1.$2 and the remote
file name mydata.data, $1 would have the value
mydata, and $2 would have the value data.
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You could do it in two steps, and use shell script to go through the file listing and generate the rename commands, like so:

[tt]#!/bin/ksh

ftp -nv localhost << HERE
user anonymous someone@somewhere.com
cd incoming
prompt
mget *.dat
ls testfile* lsfile
HERE

(
echo user anonymous someone@somewhere.com
echo cd incoming
for f in `cat lsfile`
do
echo rename $f $f.done
done
) | ftp -nv localhost

rm lsfile[/tt]

Obviously replace localhost with the hostname of the system you're getting the files from.
Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top