0

I am trying to list the file of remote host using Net::FTP Perl module. Later I want to download it using put command.

Will it possible to list the particular file using ls command

Syntax I am using is:

$remote_dir = "/home/user/test_dir/";
$ftp->cwd($remote_dir);
$file_name = "test.txt";
print $ftp->ls($file_name);

This file is exists in remote server when I tried checking it manually. But listing is not happening.

ftp->ls(); is used for only listing the directory contents or can it be used to list the particular file from a directory as I mentioned above?

vkk05
  • 3,137
  • 11
  • 25
  • You need to determine where the FTP server's root directory is, and establish the path from that root to the file you are interested in. It is unlikely the FTP root is the filesystem root directory, so it is unlikely a remote path of `/home/user/test_dir/` would be valid. – Jim Garrison May 31 '19 at 06:07

1 Answers1

0

ftp->ls(); is used for only listing the directory contents or can it be used to list the particular file from a directory

From the documentation:

ls ( [ DIR ] )
Get a directory listing of DIR, or the current directory.

Thus, it is clearly documented to list a directory, not a specific regular file.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • So that means after putting a directory contents into an array, I should pick the file?```@files = $ftp->ls(); foreach ( @files ) { print "Found : $file_name\n" if( $_ =~ /$file_name/); }``` – vkk05 May 31 '19 at 05:53
  • @vinodk89: correct, if you want to know if a file exists in a directory then you need to get a listing of the directory and look for the file. – Steffen Ullrich May 31 '19 at 05:56
  • > Thus, it is clearly documented to list a directory, not a specific regular file. This is simply not true as I just tested with my own ftp server. So there must be another issue. – Skeeve May 31 '19 at 06:22
  • 3
    @Skeeve: `ls` is implemented in terms of the FTP `NLST` command. This command is defined in [RFC 959](https://tools.ietf.org/html/rfc959) as *"The pathname should specify a __directory or other system-specific file group descriptor__;"*, i.e. a directory or something similar (OS dependent). A specific FTP server might actually also support a regular file name there but this case is out of the specification so you should not rely on this and you should not generalize on what you see with a single server to what you can expect from other servers. – Steffen Ullrich May 31 '19 at 06:31
  • 1
    @Skeeve Steffen is right. For similar questions (for other languages), see for example [FTP directory partial listing with wildcards](https://stackoverflow.com/q/9282753/850848) or [Checking file existence on FTP server](https://stackoverflow.com/q/10482204/850848). – Martin Prikryl May 31 '19 at 06:54
  • Thanks to Martin Prikryl and also @Steffen Ullrich for clarifying. – Skeeve May 31 '19 at 06:58
  • Thanks @SteffenUllrich. Now I am able to list the files from a particular directory but not all the files. Actually directory is having almost 1500+ files but array(`@files`) is giving count 900+. Why its not giving the exact count of files which it has in the directory. Is there any restrictions in listing ? – vkk05 May 31 '19 at 15:10
  • @vinodk89: There is neither a restriction in Net::FTP nor is such restriction in the standard. It is not clear from your description what exactly is behaving wrong here - it might be that the data connection breaks but it might also be that the server does not send the information about all the files. – Steffen Ullrich May 31 '19 at 16:45
  • @SteffenUllrich I have trouble retrieving files from remote server using `get` command of `Net::FTP` module. With `Net::SFTP` its working. So any guesses what would be the reason? – vkk05 Jun 01 '19 at 09:44
  • 1
    @vinodk89: please don't ask new questions in a comment. Ask a new question instead with sufficient details (code, debug output). Apart from that Net::SFTP is using a completely different protocol: SFTP is based on SSH not FTP. – Steffen Ullrich Jun 01 '19 at 10:05