Listing Files and Sizes on FTP server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Listing Files and Sizes on FTP server
# 1  
Old 04-17-2013
Code Listing Files and Sizes on FTP server

Need assistance in getting File size for the List of files using perl script . I have writtern 2 codes. One of them gives me the list of files and 2nd one give me the size for only 1 file.

I dont know how to club both of them to get the list of files with its size .

Code:
#!/usr/bin/perl -w
use Net::FTP;
$ftp = Net::FTP->new("hostname");
$ftp->login('userid', 'password');
$ftp->cwd("/pub");
#$ftp->binary;
#my $files=$ftp->ls();

my @filenames=$ftp->ls();
foreach (@filenames)
{
  print "$_\n" ;
}


Code:
#!/usr/bin/perl -w
use Net::FTP;
my $host="hostname";
my $user="userid";
my $pw = "password";
my $path="/pub";
my $file="Filename ";
my $ftp = Net::FTP->new($host);

$ftp->login($user, $pw);
$ftp->cwd($path) , $ftp->message;
$ftp->binary;
print "FTP $file :  Size = [", $ftp->size($file), "] \n";
$ftp->quit();

# 2  
Old 04-17-2013
$ftp->size($file) seems to be the thing which gets the size, so plug $_ into it for each thing in that loop in your first program.
# 3  
Old 04-17-2013
Can you please modify the code and reply . Will appreciate your help

---------- Post updated at 12:13 PM ---------- Previous update was at 12:03 PM ----------

Code:
print "FTP size = [", $ftp->size($_), "] \n";

This returns

Code:
Use of uninitialized value in print at ./ftpconnect2.pl line 17.
FTP size = []

# 4  
Old 04-17-2013
I'm not really sure why it's failing there. Maybe a cd failed or something earlier and it's confused about paths. But this by itself(with names set of course) works:

Code:
$ftp->login($user, $pw);
# $ftp->cwd($path) , $ftp->message;
$ftp->binary;

my @files=$ftp->ls();

foreach(@files)
{
        print $_, " ", $ftp->size($_), "\n";
}

$ftp->quit();

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-17-2013
Code

Finally it works .

Final code :

Code:
#!/usr/bin/perl -w
use Net::FTP;
$ftp = Net::FTP->new("Hostname");
$ftp->login('username', 'password');
$ftp->cwd("<PATH>");
$ftp->binary;

my @filenames=$ftp->ls();
foreach (@filenames)
{
#  print "$_\n" ;
 print $_, " File Size: ", $ftp->size($_), "\n";
}
$ftp->quit();

This User Gave Thanks to ajayram_arya For This Post:
# 6  
Old 04-18-2013
Code

Can anybody help me in getting the line count of the list of files displayed

Shell

Code:
ls -l |wc -l

Perl
Code:
 ?

# 7  
Old 04-18-2013
Why count the lines displayed when you can just measure the thing being displayed, the array itself?

If you have an array @arr, $#arr gives you the number of the last valid index in the array. An array 3 in size would have a last index of 2. So $#arr+1 is the number of elements.

Code:
my @arr=("a","b","c");
print $#arr + 1, "\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

FTP-ing files from Windows server to UNIX server

I need to transfer files from a Windows server to the Unix server and have to run some shell script on it to get the required output. Is it possible to transfer files from Windows server to unix server through any shell script? If so can you please help me with the details. Thanks in... (8 Replies)
Discussion started by: ssk250
8 Replies

2. Shell Programming and Scripting

FTP multiple files from one server to one server

Hi, I'm new to shell script..I have one requriement like - In one server have more than one files,I want to ftp those files to some otehr server.. Ex : test1.pdf test2.pdf Please suggest me how to do (3 Replies)
Discussion started by: venkaswa
3 Replies

3. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

4. Shell Programming and Scripting

script for to take files from FTP server to UNIX server and Unzipped that files

script for to take files from FTP server to UNIX server and Unzipped that files (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

5. UNIX for Dummies Questions & Answers

Long listing of files using find command on remote server via SSH

Hi , I am trying to find some files on a remote machine using the find command. >ssh -q atukuri@remotehostname find /home/atukuri/ -name abc.txt /home/atukuri/abc.txt The above command works fine and lists the file, but if I want to do a long listing of files (ls -l) its not working . ... (2 Replies)
Discussion started by: atukuri
2 Replies

6. Filesystems, Disks and Memory

Not able to FTP the files to a FTP server

Hi , We are facing a weird problem in our project. we need to send some xml & audio files to a remote FTP server from a Linux box, we are doing this in Perl script using Net::FTP->. Issue here is.. when FTPed the files using Perl scripts, only empty files ( 0 byte) are getting created on the... (2 Replies)
Discussion started by: kishorepotta
2 Replies

7. AIX

listing files on remote server

I am writing a script where in i have to log into a remote machine and check for necessary file by typing (ls -ltr *200505) (this gets all 05month of 2008 yr files) and if files are found get them to the local machine. If not found print a message saying no files on local machine. When i was... (3 Replies)
Discussion started by: vasuarjula
3 Replies

8. UNIX for Dummies Questions & Answers

Directorie listing in Human form for data sizes

I have seen it done at my job before, there is a command that will make a notepad and show the directorie path, subfolders, and size of the subfolders? But i dont want it to go lower than 2 levels for example: folder_01 10 GB subfolder_02 10 GB subfolder_03 10 GB... (4 Replies)
Discussion started by: JUSSAN007
4 Replies

9. UNIX for Dummies Questions & Answers

Listing all files on FTP Server

Hello, I am the definition of UNIX newbie so please bare with me. I am wondering the best way to list all files on a ftp server. If I use curl -l ftp://user:password@ftp_server This shows me the root of the ftp server, but I would like to get a listing of everything in the subfolders ... (7 Replies)
Discussion started by: Dallasbr
7 Replies

10. Shell Programming and Scripting

FTP multiple files from remote server to local server

Hi, I am facing a weired problem in my FTP script. I want to transfer multiple files from remote server to local server everyday, using mget * in my script. I also, want to send an email for successful or failed FTP. My script works for file transfer, but it don't send any mail. There is... (2 Replies)
Discussion started by: berlin_germany
2 Replies
Login or Register to Ask a Question