Help using Base SAS procedures

Get directory listing on linux

Reply
Super Contributor
Posts: 400

Get directory listing on linux

Hello,

I'm trying to get the directory listing of shape files on Linux.  When I do it on windows this code works, but I can't find the equivalent for Linux.

Filename filelist pipe "dir /b /s c:\temp\*.shp";

                                                                                 

data shapefiles ;                                      

  infile filelist truncover ;

  input filename $100. ;   

run ;

I know linux uses ls for listing, but I'm not sure how to use it within the filename statement or do I have to use a different approach?

I'm using SAS 9.3 (TS1M2) on linux

Thank you

Super User
Posts: 11,343

Re: Get directory listing on linux

Posted in reply to jerry898969

The appropriate LS command should replace "dir /b/s c:\temp\*.shp"; Obviously use a Unix/Linux path in lieu of C:\temp

Super Contributor
Posts: 400

Re: Get directory listing on linux

Hi ballardw,

Thank you for your help.  I was able to figure it out taking this approach.

rsubmit ;

data temp (keep=fname) ;

  rc=filename("filelist", "~/shape/") ;

  did=dopen('filelist') ;

  nf=dnum(did) ;

  do i=1 to nf;

    fname=dread(did, i);

      ext=scan(fname,-1) ;

      if ext = 'shp' then output ;

  end;

  rc=dclose(did);

run ;

endrsubmit ;

Do you see any issues by taking this approach?

Thank you,

Jerry

Super User
Posts: 11,343

Re: Get directory listing on linux

Posted in reply to jerry898969

There is absolutely nothing wrong with using the external file information functions. The PIPE options are really more useful when you have to run another program that directs output to standard output.

Super User
Super User
Posts: 7,043

Re: Get directory listing on linux

Posted in reply to jerry898969

I would be a little more careful in determining the extension.  Unix files could have no extension or even not have a dot at all. Or could have the dot as the first character.

  if 1 < index(fname,'.') < length(fname) then ext=scan(fname,-1,'.');

  else ext=' ';

Super Contributor
Posts: 400

Re: Get directory listing on linux

Thank you both for your help.  I appreciate it.

Tom, thank you for pointing that out. 

Super User
Posts: 7,782

Re: Get directory listing on linux

Posted in reply to jerry898969

The UNIX ls option for recursive listing of subdirectories is -R. You might also look at using the find command for your purpose, as it is much more powerful.

Keep in mind that using wildcards (? or *) in the command on UNIX may produce unexpected results, as the wildcards are expanded by the shell (unsing the contents of the current working directory) before handing off the commandline options to the command itself. Using the find command and -name \*.shp may be better. (the backslash prevents the shell from expanding the wildcards)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 6 replies
  • 2005 views
  • 7 likes
  • 4 in conversation